-
Hajipur, Bihar, 844101
Variables are fundamental to programming in Python. They act as containers that store data, allowing you to manipulate, access and reuse information throughout your program. Understanding how to create and use variables is one of the first skills every beginner must master. In this chapter, we will explore what variables are, how to name them correctly, assign values, and work with different types of data. By the end, you’ll be able to confidently use variables in your Python programs to store and process information.
A variable is a name that refers to a value stored in your computer’s memory. Instead of repeating a value multiple times, you can store it in a variable and use the variable whenever needed. This makes your code cleaner, more readable, and easier to maintain.
Example:
age = 25
name = "Ananya"
Here, age holds the number 25, and name holds the string "Ananya". Anytime you use age or name, Python substitutes the stored value.
Assigning a value to a variable is done with the = operator. The variable name goes on the left, and the value goes on the right.
Example:
score = 90
city = "Delhi"
Python automatically determines the type of the variable based on the value you assign.
Python has specific rules for naming variables:
Names must start with a letter or an underscore (_).
They can contain letters, numbers, and underscores.
They cannot start with a number.
Names are case-sensitive (score and Score are different).
Keywords cannot be used as variable names (e.g., for, if, class).
Example of valid variable names:
student_name
_age
totalScore
Example of invalid names:
2score # starts with a number
class # reserved keyword
Python is dynamically typed. This means you don’t have to declare the type of a variable explicitly. Python determines the type based on the assigned value.
Example:
value = 10 # integer
value = "Ten" # string
Here, the same variable value can hold a number first and a string later. This flexibility is one of Python’s strengths.
Python allows assigning values to multiple variables in a single line.
Example:
x, y, z = 5, 10, 15
All three variables are assigned simultaneously, which makes the code concise. You can also assign the same value to multiple variables:
a = b = c = 0
Variables can be used in arithmetic and string operations. For example:
a = 10
b = 5
sum = a + b
print(sum)
This prints 15. Similarly, strings can be combined:
first_name = "Anika"
last_name = "Sharma"
full_name = first_name + " " + last_name
print(full_name)
This prints Anika Sharma.
Variables can be updated anytime in your program. Reassigning a new value overwrites the old one.
Example:
count = 1
count = count + 1
print(count) # Output: 2
This is useful for counters, accumulators, and tracking changes over time.
Use descriptive names that convey the purpose of the variable.
Avoid single letters except for counters or temporary variables.
Use lowercase letters with underscores for readability (total_score).
Keep names consistent throughout your program.
Good naming improves code readability, making it easier for you or someone else to understand your program later.
Variables store different types of data such as integers, floats, strings, and booleans. Python determines the type automatically, but understanding it helps avoid errors when performing operations.
Example:
age = 20 # integer
price = 99.99 # float
name = "Riya" # string
is_active = True # boolean
Python also allows combining different types carefully in operations. Mixing incompatible types, like adding a number and a string without conversion, will result in an error.
Variables are names that store data in Python, allowing you to reuse and manipulate information efficiently. You assign values using the = operator, follow naming rules, and can update values anytime. Python’s dynamic typing and multiple assignment features make variables flexible and easy to work with. By understanding and using variables correctly, you can write clean, readable, and maintainable Python programs.
What is a variable in Python, and why is it important for programming?
What are the rules for naming variables in Python?
How does Python’s dynamic typing affect variable assignment?
What is the difference between assigning the same value to multiple variables in one line versus multiple assignments with different values?
Why is it important to use descriptive variable names instead of single-letter names?
Create a variable called student_name and assign it your own name, then print it.
Assign the numbers 5, 10, and 15 to variables x, y, and z in a single line.
Update a variable count by adding 1 to its current value and print the result.
Combine two string variables first_name and last_name into a single variable full_name and print it.
Assign the value 100 to three variables a, b, and c in one line, then print all three.