- Syntax
- Example 1: Creating a Variable
- Example 2: Variables Storing Different Data Types
- Example 3: Using Variables in Expressions
- Example 4: Updating a Variable
- Example 5: Using Variables in Calculations
- Example 6: Storing Text in Variables
- Example 7: Printing Multiple Variables
- Example 8: Variable Values Can Depend on Other Variables
- Example 9: Reading a Value into a Variable
- Example 10: Variables Improve Code Readability
- Common Mistakes
- Best Practices
- Key Points to Remember
A variable is a named location that stores data in memory. Variables allow a program to store information that can be used and modified during execution.
Unlike many programming languages, Python does not require you to declare the data type of a variable before using it. A variable is created automatically when a value is assigned to it.
Variables can store different types of data, such as:
- Numbers
- Strings
- Boolean values
- Lists
- Tuples
- Dictionaries
- Objects
Syntax #
variable_name = value
Components #
| Component | Description |
|---|---|
variable_name |
Name used to refer to the stored value. |
= |
Assignment operator used to assign a value to a variable. |
value |
The data that is stored in the variable. |
Example 1: Creating a Variable #
main.py
name = "Alice"
print(name)
Output #
Alice
Explanation #
- A variable named
nameis created. - The string
"Alice"is assigned to the variable using the assignment operator (=). print(name)displays the value stored in the variable.- The variable name itself is not printed; its value is printed.
Example 2: Variables Storing Different Data Types #
main.py
age = 25
salary = 35000.75
is_employee = True
print(age)
print(salary)
print(is_employee)
Output #
25
35000.75
True
Explanation #
agestores an integer.salarystores a floating-point number.is_employeestores a Boolean value.- Python variables can store different types of data without requiring explicit type declarations.
Example 3: Using Variables in Expressions #
main.py
price = 500
tax = 50
total = price + tax
print(total)
Output #
550
Explanation #
pricestores the value500.taxstores the value50.- The expression
price + taxis evaluated first. - The result (
550) is assigned to the variabletotal. - Finally, the value stored in
totalis displayed.
Example 4: Updating a Variable #
main.py
count = 10
print(count)
count = 20
print(count)
Output #
10
20
Explanation #
- Initially,
countstores the value10. - Later, a new value (
20) is assigned to the same variable. - The previous value is replaced by the new one.
- A variable can be updated as many times as needed.
Example 5: Using Variables in Calculations #
main.py
length = 12
width = 8
area = length * width
print(area)
Output #
96
Explanation #
- Two variables store the dimensions of a rectangle.
- The multiplication operator (
*) calculates the area. - The result is stored in the variable
area. print()displays the calculated value.
Example 6: Storing Text in Variables #
main.py
first_name = "John"
last_name = "Smith"
print(first_name)
print(last_name)
Output #
John
Smith
Explanation #
- Variables can store strings.
- Each variable contains a separate piece of text.
- Strings must always be enclosed in quotes.
Example 7: Printing Multiple Variables #
main.py
name = "Alice"
age = 22
city = "New York"
print(name, age, city)
Output #
Alice 22 New York
Explanation #
- Three variables are created.
- All three variables are passed to the
print()function. - By default, Python separates multiple values with a single space.
Example 8: Variable Values Can Depend on Other Variables #
main.py
a = 10
b = 20
sum = a + b
print(sum)
Output #
30
Explanation #
- The variables
aandbstore two numbers. - Their values are added together.
- The result is assigned to the variable
sum. - Variables can store the result of expressions.
Example 9: Reading a Value into a Variable #
main.py
name = input("Enter your name: ")
print("Hello", name)
Sample Input #
Enter your name: Alice
Output #
Hello Alice
Explanation #
input()reads data entered by the user.- The entered value is stored in the variable
name. - The variable is later used to display a personalized greeting.
Example 10: Variables Improve Code Readability #
main.py
radius = 5
area = 3.14 * radius * radius
print(area)
Output #
78.5
Explanation #
- The variable
radiusclearly indicates what the value represents. - Using descriptive variable names makes programs easier to read and understand.
- Variables also make programs easier to modify. Changing the value of
radiusautomatically updates the calculated area.
Common Mistakes #
1. Using a Variable Before Assigning a Value #
Incorrect
print(age)
age = 25
Output #
NameError: name 'age' is not defined
Reason
The variable age does not exist until a value has been assigned to it.
Correct
age = 25
print(age)
2. Forgetting Quotes Around Strings #
Incorrect
name = Alice
print(name)
Output #
NameError: name 'Alice' is not defined
Reason
Without quotes, Python assumes Alice is the name of another variable.
Correct
name = "Alice"
3. Confusing Assignment (=) with Equality (==) #
Incorrect
age == 25
Output #
NameError: name 'age' is not defined
Reason
=assigns a value to a variable.==compares two values for equality.
Correct
age = 25
4. Assuming a Variable Cannot Be Changed #
Incorrect Assumption
count = 10
count = 20
Output #
There is no error.
Reason #
Variables in Python can be assigned new values at any time unless special techniques are used to prevent modification.
Best Practices #
- Choose meaningful variable names.
- Store one logical value in each variable.
- Initialize variables before using them.
- Avoid using vague names such as
x,y, ortempunless they clearly represent mathematical values. - Use descriptive names that make the code self-explanatory.
Key Points to Remember #
- A variable is a named location used to store data.
- Variables are created automatically when a value is assigned.
- The assignment operator (
=) stores a value in a variable. - A variable can store numbers, strings, Boolean values, collections, and objects.
- Variables can be updated by assigning new values.
- Variables can be used in expressions and calculations.
- A variable must be assigned a value before it is used.
- Meaningful variable names improve code readability and maintainability.