Python ยท Chapter 5 of 45

Python Variables

A variable is a name that refers to a value. You create one by assigning: `x = 5`. Python figures out the type automatically.

Variable names must start with a letter or underscore and can contain letters, digits and underscores. They are case-sensitive.

Assignment

The `=` operator binds a name to a value. You can reassign the same name to a different type at any time.

Naming conventions

Use `snake_case` for variables and functions. Use `UPPER_CASE` for constants. Avoid single-letter names except for short loops.

Example 1 (python)
age = 21
name = "Ravi"
print(name, "is", age)
Output
Ravi is 21

Two variables of different types, printed together.

Example 2 (python)
x, y, z = 1, 2, 3
print(x + y + z)
Output
6

Multiple assignment in one line.

Key points

  • No type declaration โ€” the value determines the type.
  • Names are case-sensitive (`age` โ‰  `Age`).
  • Use `snake_case` for readability.
  • Reserved keywords like `if`, `for`, `class` cannot be names.
๐Ÿ’ก Note: Descriptive names (`user_email`) beat cryptic ones (`ue`) every time.

๐Ÿ“ Quick Quiz

1. Which name is a valid Python variable?

2. What does `x = 5` do?

3. The recommended convention for Python variable names is: