Python Variables

Objectives: Python Variables

Python Variables - Complete Notes

Python Variables - Complete Notes

1. What is a Variable?

A variable in Python is a name that holds a value. Think of it like a label for data. Python is a high-level, interpreted language, so when you write a variable assignment, the Python interpreter converts it into bytecode, which the Python Virtual Machine (PVM) runs.

# Example: Assigning a single value
x = 10   # The interpreter stores the value 10 in memory and links it to the name 'x'
print(x)

2. Assign Multiple Values to Multiple Variables

Python allows assigning multiple values to multiple variables in one line. This is convenient and makes your code cleaner.

# Assigning multiple values
x, y, z = "Orange", "Banana", "Cherry"
print(x)  # Output: Orange
print(y)  # Output: Banana
print(z)  # Output: Cherry
Note: Make sure the number of variables matches the number of values. If they don't, Python will throw a ValueError.

3. Assign One Value to Multiple Variables

Sometimes, you want the same value to be stored in multiple variables. Python allows this in a single line.

# Same value to multiple variables
x = y = z = "Orange"
print(x)  # Output: Orange
print(y)  # Output: Orange
print(z)  # Output: Orange
Compiler Perspective: Python evaluates the value "Orange" first, then assigns the reference to all three variables. It's efficient because the string is stored once in memory.

4. Unpack a Collection

Python allows "unpacking" a collection (like a list, tuple, or set) directly into variables.

# Example: Unpack a list
fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print(x)  # Output: apple
print(y)  # Output: banana
print(z)  # Output: cherry
This is powerful because you can assign multiple elements of a collection to variables in a readable way. Python internally iterates over the collection and assigns each element to the corresponding variable.

5. Dynamic Typing in Python

Python is dynamically typed, meaning you don't need to declare the variable type. Python figures it out at runtime.

x = 10      # integer
x = "Hello" # now x is a string
print(x)    # Output: Hello
The interpreter updates the reference of x in memory to the new string value. Python is flexible but be careful with changing types in complex programs.

6. Variable Naming Rules

  • Names must start with a letter or underscore (_) and can contain letters, digits, and underscores.
  • Case-sensitive: myVar and myvar are different.
  • Cannot use Python reserved keywords like if, for, while.

7. Exercises

1. Assign the value 'Hello World' to three variables in one statement.

# Correct answer:
x = y = z = 'Hello World'

2. Unpack the list [1, 2, 3] into variables a, b, c and print them.

numbers = [1, 2, 3]
a, b, c = numbers
print(a, b, c)

8. Summary

  • Python variables store references to values in memory.
  • You can assign multiple values in one line or the same value to multiple variables.
  • Unpacking collections into variables makes your code cleaner.
  • Python dynamically decides the type of variable at runtime.

Reference Book: N/A

Author name: MWALA_LEARN Work email: biasharabora12@gmail.com
#MWALA_LEARN Powered by MwalaJS #https://mwalajs.biasharabora.com
#https://educenter.biasharabora.com

:: 1::