Checking your session…
Module 02: Variables, Data Types & Operators

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.

PYTHON
student_name = "Arun"
age = 21
print(age)

In this example: - student_name holds the text value "Arun" - age holds the number value 21

Output:

TEXT
21

Why Do We Use Variables?

Variables allow us to store, reuse, and update data throughout our program.

PYTHON
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

variable name value in memory age refers to 25 int name refers to "Arun" str price refers to 99.5 float Assignment like age = 25 binds the name on the left to the value on the right.
A variable is a name that refers to a value stored in memory. Assignment binds the name to the value.

In Python, a variable is created automatically when you assign a value using the = (assignment) operator.

PYTHON
score = 90

Python detects data types automatically:

PYTHON
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

PYTHON
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.

PYTHON
data = 10
print(data)  # 10

data = "Python Programming"
print(data)  # Python Programming

Output:

TEXT
10
Python Programming

Swapping Two Variables

In Python, swapping the values of two variables is very simple:

PYTHON
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:

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:

PYTHON
name = "Arun"
age = 21

# Using f-string for clear output
print(f"Student Name: {name}, Age: {age}")

Output:

TEXT
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.

PYTHON
user_name = input("Enter your name: ")
print(f"Hello, {user_name}!")

If you want to use the input as a number, convert it:

PYTHON
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 (age is different from Age).
  • 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, and SCORE are 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

  1. Create variables to store your name, age, and college score. Print them using an f-string.
  2. Check which of these variable names are valid: 1st_name, _score, total-marks, user_age, class.
  3. Convert studentRegistrationNumber into standard Python snake_case.
  4. Create two variables x = 5 and y = 15. Swap their values and print the result.
  5. What happens if you run 2name = "Arun"? Fix the error.
  6. Take a user's city name using input() and display a welcome message.

End of Python Variables