■ Python Variables – Notes
1. What is a Variable?
A container for storing data values. Think of it like a label attached to some information in memory.
Example:
x = 10 # integer
name = 'Rakhi' # string
pi = 3.14 # float
2. Variable Naming Rules
■ Allowed:
- Must start with a letter or underscore.
- Can contain letters, numbers, and underscores.
- Case-sensitive.
■ Not Allowed:
- Cannot start with a number.
- Cannot use keywords (class, for, if, etc.).
Examples:
my_name = 'Rakhi' # valid
_name = 'Hello' # valid
2name = 'Hi' # invalid
class = 'Python' # invalid
3. Data Types in Variables
a = 10 # int
b = 10.5 # float
c = 'Hello' # str
d = True # bool
e = [1, 2, 3] # list
4. Multiple Assignments
x, y, z = 1, 2, 3
print(x, y, z) # 1 2 3
a = b = c = 'Python'
print(a, b, c) # Python Python Python
5. Constants (by convention)
Python has no true constants. By convention, we write them in UPPERCASE.
PI = 3.14159
MAX_USERS = 100
6. Dynamic Typing
x = 5
print(type(x)) # int
x = 'Hello'
print(type(x)) # str
7. Type Casting
x = int('10') # string → int
y = float(5) # int → float
z = str(100) # int → string
8. Global vs Local Variables
x = 10 # global
def func():
x = 5 # local
print('Inside:', x)
func()
print('Outside:', x)
■ Quick Summary
- Variables = containers for data.
- Naming rules: start with letter/_ , no keywords, case-sensitive.
- Python is dynamically typed.
- Use uppercase names for constants.
- Variables can be global or local.