Assigning Variables in Python
Assigning Variables in Python
Variable Basics
- A variable is a name associated with a specific value or object.
- Variables provide a way to store and manipulate data using meaningful names.
- Variables are assigned using the equal sign (=).
Assigning Values
- Example:
python
x = 10
y = "Python is fun"
z = True
print(x)
print(y)
print(z)
- Assigning a variable itself does not produce output; the
print() function displays the variable's value.
Spacing
- Good practice: Use spaces around the equals sign for clarity.
p = 8 (works but less readable)p = 8 (more readable)
Reassigning Variables
- Variables can be reassigned to new values.
python
p = 8
print(p) # Output: 8
p = 10
print(p) # Output: 10
Using Variables in Calculations
- Variables can be used in calculations by substituting their assigned values.
Multiple Assignments
- Multiple variables can be assigned the same value:
python
n = m = 4 # Assigns 4 to both n and m
- Multiple variables can be assigned different values simultaneously using comma-separated lists and tuple unpacking.
python
x, y, z = 10, 20, 30
# x = 10, y = 20, z = 30
Tuple Unpacking
- Assigning values from a tuple to multiple variables at once.
- Tuples are a data structure in Python represented by a list of values within parentheses.
Swapping Variable Values
- Tuple unpacking can be used to easily swap the values of two variables.
python
x, y = y, x # Swaps the values of x and y
- Example:
python
x = 10
y = 20
x, y = y, x
print(x) # Output: 20
print(y) # Output: 10
Variable References and Memory
- In Python, assigning a variable creates a reference to an object in memory.
- Reassigning a variable switches the reference to a different object.
- If the object a variable refers to is altered, the variable's value reflects that change.
Immutable vs. Mutable Objects
- Immutable Objects: Cannot be changed after creation (e.g., basic data types like numbers and strings).
- Operations that appear to modify immutable objects actually create new objects in memory.
*Example:
x = "hello"
y = x
y = y.lower()
print(x) # Output: hello
print(y) # Output: hello
* `x` and `y` refer to different string objects after `y.lower()` is called.
- Mutable Objects: Can be changed in memory without creating a new object (e.g., lists).
- Changes to a mutable object affect all variables referencing that object.
Example:
x = [1, 2, 3]
y = x
y.append(4)
print(x) # Output: [1, 2, 3, 4]
print(y) # Output: [1, 2, 3, 4]
* Both `x` and `y` refer to the same list object, so appending to `y` also modifies `x`.
Implications of Mutable Objects
- Be cautious when working with mutable objects, as changes can have unintended side effects on other variables referencing the same object.
Variables in Data Analysis
- Variables are fundamental in programming and data analysis.
- Data objects are often assigned to variables, and functions are applied to these objects.
Tuples and Lists
- Tuples and lists are sequential data objects that can hold multiple values.
- Lists will be covered in detail in the next lesson.
Key Takeaways
- Variables are names associated with values or objects.
- Use the equals sign (=) to assign values to variables.
- Be aware of mutability when working with variables and objects.
- Tuple unpacking simplifies multiple assignments and value swapping.