Variables in Python
Lecture Notes | Apna College
Source: Variables in Python – Full HD Tutorial
1. What is a Variable?
A variable is a container used to store data values in memory. Think of it like a named box in
RAM that holds a value you can use and change throughout your program.
The general syntax to create a variable is:
variable_name = value
Example assignments shown in the lecture:
x = 5
name = "Shradha"
age = 30
PI = 3.14
2. Variables as Containers in Memory
When Python executes a variable assignment, it allocates a slot in memory (RAM) and stores
the value there under the given name. The diagram below shows how two variables are
mapped to memory cells:
[ PI = 3.14 ] [ age = 30 ] [ ] [ ] ← memory slots
Key idea: when you write age = 30, Python finds a free memory slot, labels it age, and stores
the value 30 there.
3. Data Types Stored in Variables
Variables can hold different types of data. The three types introduced in this lecture are:
Type Example Description
String (str) name = "Shradha" Text — wrapped in quotes
Integer (int) age = 30 Whole numbers
Float PI = 3.14 Decimal / floating-point numbers
4. Printing Variables
Use the built-in print() function to display variable values. You can pass one or multiple variables
separated by commas:
# Print a single variable
print(name) # Output: shradha
# Print multiple variables
print(name, age, PI) # Output: shradha 35 3.14
# Print with a label string
print("my name is:", name) # Output: my name is: shradha
# Use variables in expressions
print("my age is:", age-5) # Output: my age is: 30
5. Identifiers (Variable Naming Rules)
An identifier is the name given to a variable (or function, class, etc.). Python enforces specific
rules for valid identifiers:
• Rule 1 — Letters: Can use English letters A–Z (uppercase) and a–z (lowercase).
• Rule 2 — Digits (0–9): Digits are allowed, but NOT as the first character. E.g., age2 ✓
but 2age ✗
• Rule 3 — Underscore (_): The only special character allowed. E.g., _name, my_age ✓
Examples demonstrated in the lecture:
_name = "shradha" # valid — starts with underscore
_age = 35 # valid
_PI = 3.14 # valid
print(_name) # Output: shradha
6. Important Python Properties
Indentation
Python uses indentation (whitespace at the start of a line) to define code blocks — unlike
languages such as Java or C++ that use curly braces {}. Incorrect indentation causes an
IndentationError.
Case Sensitivity
Python is case-sensitive. This means name, Name, and NAME are three different variables.
This is different from languages like SQL or HTML where keywords are case-insensitive (e.g.,
<body> and <BODY> are both valid HTML, but in Python, variable cases matter).
Comparison note shown in lecture:
# SQL / HTML: case-insensitive
<body> ← valid HTML
<BODY> ← also valid HTML
# Python: case-SENSITIVE
name = 'Alice'
Name = 'Bob' # completely different variable
NAME = 'Carol' # another different variable
7. Quick Reference Summary
Concept Key Point
Variable A named container that stores a value in memory
Assignment Use = to assign: x = 5
Data types str (text), int (whole number), float (decimal)
print() Displays values; multiple values separated by commas
Identifier rules Letters, digits (not first), underscore only
Indentation Defines code blocks — must be consistent
Case sensitivity name ≠ Name ≠ NAME in Python
— End of Notes —