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.
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 (such as C or Java), Python does not require the declaration of a variable type explicitly. Instead, it automatically detects the type based on the assigned value.
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:
@, $, % are allowed.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: In order to create clear code, start to 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.
Note the preceding ‘f’ before the string in double quotes.
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.
Internally, this will release the memory space that was allocated to the variable.
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.
In this lesson, we have covered the following concepts:
delIn the next lesson, we will explore best practices for naming variables in Python and how to write clean and readable code.
We noticed you're visiting from United Kingdom (UK). We've updated our prices to Pound sterling for your shopping convenience. Use United States (US) dollar instead. Dismiss
Not a member yet? Register now
Are you a member? Login now