A variable is a name that refers to a value stored in memory. You can assign and reassign values to variables freely in Python.
Use the =
operator to assign a value to a variable.
x = 10
name = "John"
is_active = True
Python is dynamically typed, meaning variables can be reassigned to different data types:
x = 10 # integer
x = "Hello" # now a string
var
and Var
are different).for
, if
, else
).age = 55
_user_name = "John"
total_price = 150.75
is_logged_in = False
Assign multiple variables in one line:
a, b, c = 1, 2, 3
name, age = "John", 55
By convention, constants are written in uppercase:
PI = 3.14159
MAX_CONNECTIONS = 10