beginnerBasics

Variables & Assignment

Understanding variables, naming rules, and assignment in Python.

01Creating Variables

In Python, variables are created when you assign a value to them. You do not need to declare a type — Python figures it out automatically (dynamic typing).

Python
1name = "Alice"
2age = 25
3height = 5.6
4is_student = True
5
6print(name, age, height, is_student)
Output
Alice 25 5.6 True

02Naming Rules

Variable names must start with a letter or underscore, can contain letters, numbers, and underscores, and are case-sensitive. Reserved keywords (like if, for, class) cannot be used as variable names.

Python
1# Valid names
2my_var = 10
3_private = 20
4camelCase = 30
5
6# Invalid names (will cause errors)
7# 2fast = 10 <- starts with number
8# my-var = 20 <- contains hyphen
9# class = 30 <- reserved keyword

03Multiple Assignment

Python allows you to assign values to multiple variables at once:

Python
1x, y, z = 1, 2, 3
2a = b = c = 0
3
4print(x, y, z) # 1 2 3
5print(a, b, c) # 0 0 0
Output
1 2 3
0 0 0