Curriculum
A variable in Python is a named container that stores data, allowing us to reference and manipulate it later. Variables are fundamental in programming because they help us store, retrieve, and modify data efficiently.
In this section, you will learn:
By the end of this lesson, you should understand how variables work and be able to use them in Python programs.
A variable is like a container or a label that holds a piece of data. You can think of a variable as a named box where we store information. Think of a named box like a wooden box with a label on it, into which you can place something.
Example: Storing and Using Variables
The following shows how you assign values to variables
name = "Alice"
age = 25
print(name) # Output: Alice
print(age) # Output: 25
Here, "Alice"
and 25
are stored in the variables called name
and age
. When you do a print, using the print
keyword, you can print out the content of a variable.
Reusability: Instead of writing the same data multiple times, we store it in a variable and use it anywhere.
Code Readability: Using meaningful variable names makes code easier to understand.
Dynamic Values: You can change the value stored in a variable, making programs more flexible.
print("Alice is 25 years old.")
print("In 5 years, Alice will be 30.")
name = "Alice"
age = 25
print(name, "is", age, "years old.")
print("In 5 years,", name, "will be", age + 5, ".")
If we change name = "Bob"
, the output automatically updates without modifying multiple lines!
In Python, creating a variable is as simple as assigning a value using the =
operator.
x = 10
y = 3.14
z = "Hello, Python!"
Explanation:
x
is assigned the integer 10
.y
is assigned the floating-point number 3.14
.z
is assigned the string "Hello, Python!"
.Unlike other languages (C, Java), Python does not require declaring a variable type explicitly—it automatically detects the type.
When you create a variable, Python stores the value in memory and gives it a reference (name).
a = 10
b = a # b now refers to the same value as a
a = 20 # Changing a does not affect b
print(a) # Output: 20
print(b) # Output: 10
Here, b = a
does not create a duplicate copy. Instead, both variables point to the same memory location until a
is reassigned.
Variables are mutable, meaning their values can be changed.
name = "Alice"
print(name) # Output: Alice
name = "Bob" # Changing the value of name
print(name) # Output: Bob
The value stored in name
changed from "Alice"
to "Bob"
.
Python has specific rules for naming variables:
Allowed:
_
.age
, Age
, and AGE
are different variables.Not Allowed: Variable names cannot start with a number.
No special characters like @, $, %
.
Cannot use Python keywords (if, while, def, class, etc.
).
# Valid Variables
student_name = "Alice"
_age = 20
PI = 3.14159 # Constants are usually written in uppercase
# Invalid Variables (will cause an error)
2name = "John" # Cannot start with a number
student-name = "Alice" # Cannot contain a hyphen
class = "Python" # 'class' is a reserved keyword
Best Practice: Use descriptive variable names like user_name
instead of x
.
Python allows multiple assignments in a single line.
# Assigning multiple variables in one line
x, y, z = 10, 20, "Hello"
print(x) # Output: 10
print(y) # Output: 20
print(z) # Output: Hello
? Assigning the same value to multiple variables:
a = b = c = 50
print(a, b, c) # Output: 50 50 50
You can print multiple variables using commas (,
) or f-strings.
name = "Alice"
age = 25
# Using commas (adds spaces automatically)
print("My name is", name, "and I am", age, "years old.")
# Using f-strings (recommended)
print(f"My name is {name} and I am {age} years old.")
Output:
My name is Alice and I am 25 years old.
f-strings (f"..."
) make printing variables cleaner and more readable.
To remove a variable from memory, use del
.
x = 100
del x # Deletes the variable x
print(x) # This will raise an error since x no longer exists
Warning: After deletion, trying to access the variable will cause an error.
This lesson has covered the following concepts
Variables store data and allow dynamic updates
Python automatically determines variable types (no need for explicit declaration)
Variable names must follow naming rules (no special characters, no reserved words)
Multiple variables can be assigned in a single line
Printing variables using f-strings makes output cleaner
Variables can be deleted using del
In the next lesson, 2.2 Naming Rules and Conventions for Variables, we will explore best practices for naming variables in Python and how to write clean, readable code.
Not a member yet? Register now
Are you a member? Login now