Introduction
In Python, variables are used to store, retrieve, and manipulate data. Whether you are building a simple calculator, a web application, a machine learning model, or a game, variables play a crucial role in managing information within your program.
One of the advantages of Python is its simplicity when working with variables. Unlike some programming languages, Python does not require you to declare the data type of a variable explicitly. The interpreter automatically determines the type based on the assigned value.
What are Python Variables?
A Python variable is a named memory location used to store data values.
Variables allow programmers to store information and reuse it throughout a program.
For example:
name = "John"
Here:
- name is the variable.
- John is the value stored in the variable.
Syntax of Python Variables
Creating a variable in Python is simple.
Syntax:
variable_name = value
Example:
name = "John"
Here:
- name is the variable name.
- = is the assignment operator.
- “John” is the assigned value.
Creating Variables
Python variables are created automatically when a value is assigned.
Example:
name = "John"
age = 25
salary = 50000
Display the values:
print(name)
print(age)
print(salary)
Output:
25
50000
Variable Data Types
Python variables can store different types of data.
String Variable
name = "John"
Integer Variable
age = 25
Float Variable
price = 99.99
Boolean Variable
is_logged_in = True
Example:
name = "John"
age = 25
price = 99.99
is_logged_in = True
print(name)
print(age)
print(price)
print(is_logged_in)
Output:
25
99.99
True
Checking Variable Type
Python provides the type() function to determine a variable’s data type.
Example:
name = "John"
print(type(name))
Output:
Another example:
age = 25
print(type(age))
Output:
Multiple Variable Assignment
Python allows assigning values to multiple variables in a single line.
Example:
name, age, city = "John", 25, "New York"
print(name)
print(age)
print(city)
Output:
25
New York
Assigning the Same Value to Multiple Variables
You can assign the same value to multiple variables.
Example:
x = y = z = 100
print(x)
print(y)
print(z)
Output:
100
100
Variable Naming Rules
Python has specific rules for naming variables.
Valid Rules
1. Must Start with a Letter or Underscore
Valid:
name = "John"
_age = 25
Invalid:
1name = "John"
2. Cannot Contain Special Characters
Valid:
user-name = "John"
Invalid:
user_name = "John"
3. Case-Sensitive
name = "John"
Name = "David"
These are two different variables.
4. Cannot Use Python Keywords
Invalid:
class = "Student"
Output:
Keywords are reserved words in Python.
Variable Naming Conventions
Although Python allows many naming styles, some conventions are recommended.
Snake Case (Recommended)
first_name = "John"
total_salary = 50000
Camel Case
firstName = "John"
Pascal Case
FirstName = "John"
Python developers generally prefer snake_case.
Changing Variable Values
Variables can be updated at any time.
Example:
age = 25
print(age)
age = 30
print(age)
Output:
30
The new value replaces the old value.
Dynamic Typing in Python
Python uses dynamic typing, this means a variable can change its data type.
Example:
x = 10
print(type(x))
Output:
Now assign a string:
x = "Python"
print(type(x))
Output:
Note: Python automatically adjusts the variable type.
Using Variables in Calculations
Variables are commonly used in mathematical operations.
Example:
num1 = 10
num2 = 20
sum = num1 + num2
print(sum)
Output:
Using Variables with User Input
Variables often store user-provided data.
Example:
name = input("Enter your name: ")
print("Welcome", name)
Sample Output:
Welcome John
Real-Life Example
Suppose a company needs to calculate employee salaries.
Example:
employee_name = "John"
basic_salary = 50000
bonus = 5000
total_salary = basic_salary + bonus
print(employee_name)
print(total_salary)
Output:
55000
Here, variables store employee information and salary details.
Advantages of Variables
1. Improved Readability
Variables make code easier to understand.
2. Reusability
Values can be reused throughout the program.
3. Easy Maintenance
Changing a value requires updating it in one place.
4. Better Organization
Variables help organize data logically.
5. Dynamic Programming
Programs can process user input and changing data.
Common Mistakes
1. Using Undefined Variables
Incorrect:
print(name)
Output:
Correct:
name = "John"
print(name)
Output:
2. Starting Variable Names with Numbers
Incorrect:
1name = "John"
Correct:
name1 = "John"
3. Using Keywords as Variable Names
Incorrect:
if = 10
Correct:
user_age = 10
4. Ignoring Case Sensitivity
Incorrect:
name = "John"
print(Name)
Output:
Correct:
name = "John"
print(name)
Output:
5. Using Special Characters
Incorrect:
user-name = "John"
Correct:
user_name = "John"
Conclusion
Python variables are essential building blocks of Python programming. They provide a way to store, manage, and manipulate data efficiently. Variables make programs more readable, maintainable, and flexible by allowing developers to assign meaningful names to data values.
Python’s simple variable syntax, dynamic typing, and support for multiple assignments make working with variables easy for beginners and professionals alike.
Python Variable – Interview Questions
Q 1: What is a variable in Python?
Q 2: Do we need to declare variable types in Python?
Q 3: How do you assign a value to a variable?
Q 4: Can a variable name start with a number?
Q 5: Are Python variables case-sensitive?
Python Variable – Objective Questions (MCQs)
Q1. Which of the following is the correct way to create a variable in Python?
Q2. What will be the output of the following code?
x = 10
y = "10"
print(x + int(y))
Q3. Which of the following variable names is invalid in Python?
Q4. What is the data type of the variable x in the code below? x = 3.5
Q5. Which statement is true about Python variables?