Python Variables
Level: 1 | Version: v1.0 | Author: Meptrasoft
What is a Variable?
A variable is a name that refers to a value in a program. For beginners, you can think of it like a labeled box that helps you find and use data.
student_name = "Arun"
age = 21
print(age)
In this example:
- student_name holds the text value "Arun"
- age holds the number value 21
Output:
21
Why Do We Use Variables?
Variables allow us to store, reuse, and update data throughout our program.
price = 100
quantity = 5
total = price * quantity
print(total) # Output: 500
Without variables, we would have to type raw numbers repeatedly, making code hard to read and modify.
Creating a Variable
In Python, a variable is created automatically when you assign a value using the = (assignment) operator.
score = 90
Python detects data types automatically:
age = 21 # Integer (whole number)
price = 99.5 # Float (decimal number)
name = "Arun" # String (text)
is_admitted = True # Boolean (True/False)
Variable Naming Rules
Python enforces strict rules for variable names. If you break these rules, Python gives a syntax error.
| Rule | ✗ Invalid Name | ✓ Valid Name |
|---|---|---|
Must start with a letter or underscore (_) |
1student |
student1, _student |
| Can contain letters, numbers, and underscores | student@name |
student_name |
| Cannot contain spaces | student name |
student_name |
Cannot use special characters (!, @, #, -) |
student-name |
student_name |
Cannot use Python keywords (class, if, for) |
class |
class_name |
Python variable names are case-sensitive. This means age, Age, and AGE are three different names.
Case-Sensitivity Example
age = 21
Age = 25
AGE = 30
print(age) # 21
print(Age) # 25
print(AGE) # 30
age, Age, and AGE are three completely different variables in Python.
Dynamic Typing
In Python, you do not need to declare a variable's data type. A variable name can refer to a number first, and later refer to a string. This is called dynamic typing.
data = 10
print(data) # 10
data = "Python Programming"
print(data) # Python Programming
Output:
10
Python Programming
Swapping Two Variables
In Python, swapping the values of two variables is very simple:
a = 10
b = 20
# Swapping in one simple line
a, b = b, a
print("a =", a) # a = 20
print("b =", b) # b = 10
Variable Naming Styles
| Style Name | Example | Commonly Used For |
|---|---|---|
snake_case |
student_name |
Recommended for Python variables and functions |
camelCase |
studentName |
Used in languages like JavaScript and Java |
PascalCase |
StudentName |
Used for class names in Python |
UPPER_CASE |
MAX_LIMIT |
Used for constant values |
Always use descriptive snake_case names in Python:
# Good, clear variable name
total_marks = 450
# Bad, unclear variable name
x = 450
User Input and Output with Variables
Displaying Output with print() and f-strings
The print() function displays variables on screen. f-strings (formatted string literals) allow you to insert variable values directly inside text:
name = "Arun"
age = 21
# Using f-string for clear output
print(f"Student Name: {name}, Age: {age}")
Output:
Student Name: Arun, Age: 21
Reading Input with input()
The input() function reads input entered by the user.
Important:
input()always returns data as a string (str), even if the user types numbers.
user_name = input("Enter your name: ")
print(f"Hello, {user_name}!")
If you want to use the input as a number, convert it:
age = int(input("Enter your age: "))
print(f"Age: {age}")
Placement Quick Points
- A variable is a name that refers to a value.
=is the assignment operator used to store values.- Python uses dynamic typing (no explicit type declaration needed).
- Variable names are case-sensitive (
ageis different fromAge). - Use snake_case for naming Python variables (
student_name). - Swap variables easily using
a, b = b, a. input()function always returns user input as a string (str).- Use f-strings (
f"Name: {name}") to print formatted output easily.
Interview Questions
- What is a variable in Python?
- A name that refers to a value in a program.
- Is Python dynamically typed? Explain.
- Yes. A variable's data type is decided at runtime based on the value assigned to it, and can change over time.
- What is the difference between
=and==? =is used to assign a value to a variable;==is used to compare whether two values are equal.- Are Python variable names case-sensitive?
- Yes.
score,Score, andSCOREare three different variables. - What naming convention is recommended for Python variables?
snake_case(all lowercase letters separated by underscores).- How do you swap two variables in Python without using a temporary variable?
- By using tuple assignment:
a, b = b, a. - What data type does
input()return? input()always returns data as a string (str).
Practice
- Create variables to store your name, age, and college score. Print them using an f-string.
- Check which of these variable names are valid:
1st_name,_score,total-marks,user_age,class. - Convert
studentRegistrationNumberinto standard Pythonsnake_case. - Create two variables
x = 5andy = 15. Swap their values and print the result. - What happens if you run
2name = "Arun"? Fix the error. - Take a user's city name using
input()and display a welcome message.
End of Python Variables