Python Variables
Introduction to Python Variables
Variables are one of the most fundamental concepts in Python programming. They are used to store data that can be accessed and modified throughout program execution.
In Python, you do not need to declare a variable’s data type explicitly. Python automatically determines the type based on the assigned value. This feature is known as dynamic typing.
In this tutorial, you will learn how to create variables, assign values, use multiple variable assignments, and understand dynamic typing and dynamic binding with practical examples.
1. What Are Variables in Python?
A variable is a named reference used to store and access a value in a Python program.
For example:
name = "Rahul"
age = 22
salary = 25000.50
print(name)
print(age)
print(salary)
Output:
Rahul
22
25000.5
In this example:
namestores a string value.agestores an integer value.salarystores a floating-point value.
Here, the = symbol is called the assignment operator. It assigns a value to a variable.
2. How to Create Variables in Python
Python variables are created when you assign a value to a name. There is no need to declare variables separately.
Syntax
variable_name = value
Example
a = 10
b = 20
print(a)
print(b)
Output:
10
20
You can also check the data type of a variable using the type() function.
a = 10
b = "Python"
print(type(a))
print(type(b))
Output:
<class 'int'>
<class 'str'>
3. Dynamic Typing in Python
Python is a dynamically typed programming language.
Dynamic typing means that you do not need to specify the data type of a variable when creating it. Python determines the type automatically based on the assigned value.
Example
a = 10
print(a)
print(type(a))
Output:
10
<class 'int'>
Now, assign a string value to the same variable.
a = "Python"
print(a)
print(type(a))
Output:
Python
<class 'str'>
In this example, the variable a first stores an integer and then stores a string.
Python automatically determines the data type based on the current assigned value.
4. Dynamic Binding in Python
Dynamic binding means that a variable name can refer to different objects during program execution.
In Python, variables are references to objects rather than fixed storage locations with permanently assigned data types.
Example
a = 10
print(a)
print(type(a))
a = "Hello"
print(a)
print(type(a))
Output:
10
<class 'int'>
Hello
<class 'str'>
In this example:
- Initially,
arefers to the integer object10. - Later,
arefers to the string object"Hello".
This demonstrates how a variable name can be associated with different objects during execution.
5. Multiple Variable Assignment in Python
Python allows you to assign values to multiple variables in a single statement.
This makes code shorter and easier to read.
Example
a, b, c = 10, 20, 30
print(a)
print(b)
print(c)
Output:
10
20
30
In this example, three variables receive three different values in a single assignment statement.
Assigning Multiple Values
You can assign values to multiple variables simultaneously.
name, age, city = "Rahul", 22, "Patna"
print(name)
print(age)
print(city)
Output:
Rahul
22
Patna
6. Assigning the Same Value to Multiple Variables
Python also allows you to assign the same value to multiple variables using a single statement.
Example
a = b = c = 100
print(a)
print(b)
print(c)
Output:
100
100
100
Here, all three variables are assigned the same value.
7. Variable Naming Rules in Python
Python has certain rules that must be followed when creating variable names.
- A variable name must start with a letter or an underscore.
- A variable name cannot start with a number.
- Variable names can contain letters, numbers, and underscores.
- Spaces are not allowed in variable names.
- Python keywords cannot be used as variable names.
- Variable names are case-sensitive.
Valid Variable Names
name = "Rahul"
student_age = 22
age1 = 23
_total = 500
Invalid Variable Names
1name = "Rahul"
student age = 22
class = "Python"
These examples are invalid because they violate Python’s naming rules.
8. Python Variable Naming Examples
Python allows both uppercase and lowercase letters in variable names.
However, variable names are case-sensitive.
Example
name = "Rahul"
Name = "Amit"
NAME = "Sanjay"
print(name)
print(Name)
print(NAME)
Output:
Rahul
Amit
Sanjay
All three are treated as different variables.
9. Practical Examples of Python Variables
Example 1: Store Student Information
name = "Rahul"
age = 22
course = "B.Tech"
marks = 85.5
print("Student Name:", name)
print("Age:", age)
print("Course:", course)
print("Marks:", marks)
Output:
Student Name: Rahul
Age: 22
Course: B.Tech
Marks: 85.5
Example 2: Calculate the Sum of Two Numbers
a = 10
b = 20
sum = a + b
print("Sum:", sum)
Output:
Sum: 30
Example 3: Swap Two Variables
Python allows you to swap two variables without using a temporary variable.
a = 10
b = 20
a, b = b, a
print("a =", a)
print("b =", b)
Output:
a = 20
b = 10
10. Practice Questions on Python Variables
Test your understanding with these multiple-choice questions.
Question 1: Which symbol is used to assign a value to a variable in Python?
A. ==
B. =
C. !=
D. >=
Answer: B. =
Question 2: What is meant by dynamic typing in Python?
A. Variables must be declared with a data type.
B. Variables cannot change their values.
C. Python determines the data type automatically.
D. Python does not support variables.
Answer: C. Python determines the data type automatically.
Question 3: What will be the output?
a = 10
a = "Python"
print(a)
A. 10
B. Python
C. Error
D. None
Answer: B. Python
Question 4: Which of the following is a valid variable name?
A. 1name
B. student age
C. student_age
D. class
Answer: C. student_age
Question 5: What will be the output?
a = b = c = 50
print(a, b, c)
A. 50 0 0
B. 50 50 50
C. 0 0 50
D. Error
Answer: B. 50 50 50
Question 6: Are name and Name different variables in Python?
A. Yes
B. No
C. Only on Windows
D. Only in functions
Answer: A. Yes