Variables & Constants

Variables

A variable in Python is a named reference to a value stored in memory. Python uses dynamic typing, meaning you
don’t need to declare a variable’s type—it’s determined at runtime based on the assigned value.

In simple terms, a variable is like a labeled box where you can store different types of information, such as numbers,
words, or lists.

x = 10      # Integer variable
name = "Alice"  # String variable
price = 99.99  # Float variable
is_valid = True  # Boolean variable

Python automatically assigns the correct data type to each variable based on the assigned value.

Python allows you to assign multiple variables in a single line:

name, age, is_student = "Bob", 30, True
print(name) # Output: Bob
print(age)  # Output: 30
print(is_student)  # Output: True

Naming Rules for Variables

  • Must start with a letter (a-z, A-Z) or underscore (_)
  • Cannot start with a number
  • Can contain letters, numbers, and underscores
  • Case-sensitive (name and Name are different variables)
  • Should not be a Python keyword (e.g., class, if, def, etc.)

Constants in Python

Python does not have built-in constant support, but by convention, constants are written in uppercase letters:

PI = 3.14159
GRAVITY = 9.8

By convention, we do not change the values of constants after they are defined.