Class 12 Python – Data and Variables (4 Pages Notes)
1. Introduction to Variables
A variable is a name given to a memory location where data is stored.
It acts like a container holding values that can be changed during program execution.
In Python, variables are dynamically typed, meaning you don’t need to declare their
type beforehand.
Example:
x = 10 # integer
name = "Ram" # string
pi = 3.14 # float
Rules for Naming Variables
1. Must begin with a letter or underscore (_).
2. Cannot start with a digit.
3. Can only contain letters, digits, and underscores.
4. Case-sensitive (e.g., Age and age are different).
5. Cannot use reserved keywords (for, while, class, etc.).
2. Data Types in Python
Python provides different built-in data types. They are categorized as:
A) Numeric Types
int → whole numbers (e.g., 10, -5, 1000)
float → decimal numbers (e.g., 3.14, -0.5)
complex → numbers with real and imaginary parts (e.g., 2+3j)
B) Sequence Types
str → text (e.g., "Hello World")
list → ordered, mutable collection (e.g., [1, 2, "Python"])
tuple → ordered, immutable collection (e.g., (10, 20, 30))
C) Set Types
set → unordered, mutable, unique items (e.g., {1, 2, 3})
frozenset → immutable version of a set
D) Mapping Type
dict → key-value pairs (e.g., {"name": "John", "age": 25})
E) Boolean Type
bool → represents True or False
F) None Type
None → represents no value or null
3. Variable Assignment
Python allows different ways of assigning values:
1. Single Assignment
x = 100
2. Multiple Assignment
a, b, c = 10, 20, 30
3. Same Value Assignment
x = y = z = 50
4. Swapping Values
x, y = y, x
4. Type Conversion (Type Casting)
Changing one data type to another:
int() → converts to integer
float() → converts to float
str() → converts to string
list() → converts to list
tuple() → converts to tuple
set() → converts to set
Example:
a = "123"
b = int(a) # 123 (integer)
c = float(a) # 123.0 (float)
5. Mutable vs Immutable Data
Mutable: Can be changed after creation (list, dict, set).
Immutable: Cannot be changed after creation (int, float, str, tuple).
Example:
x = [1, 2, 3]
x[0] = 100 # allowed (mutable)
y = "hello"
# y[0] = "H" → ❌ error (immutable)
6. Memory Management
Python uses automatic memory management (Garbage Collection).
Variables are references to objects stored in memory.
If no reference exists, memory is freed automatically.
Functions:
type(x) → returns type of variable
id(x) → returns unique memory ID
7. Input and Output with Variables
input() → takes user input (always as string)
print() → displays output
Example:
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print("Hello", name, "you are", age, "years old.")
8. Summary Table
Category Data Types Example
Numeric int, float, complex 5, 3.14, 2+3j
Sequence str, list, tuple "abc", [1,2], (3,4)
Set set, frozenset {1,2,3}
Mapping dict {"a":1, "b":2}
Boolean bool True, False
None Type None None
9. Example Questions
1. Write a program to swap two numbers using variables.
2. Demonstrate multiple assignment in Python.
3. Convert string "2025" into integer and float.
4. Create a dictionary to store student details.
5. Write a program to check if a variable is mutable or immutable.
10. Exam Tips
Be clear about mutable vs immutable data types.
Understand the difference between list and tuple.
Practice type casting questions.
Use small programs to demonstrate variable assignments.