Python Variables Basics

What is a Variable?

A variable is a name that refers to a value stored in memory. You can assign and reassign values to variables freely in Python.

Variable Assignment

Use the = operator to assign a value to a variable.

x = 10
name = "John"
is_active = True

Dynamic Typing

Python is dynamically typed, meaning variables can be reassigned to different data types:

x = 10        # integer
x = "Hello"   # now a string

Variable Naming Rules

Examples

age = 55
_user_name = "John"
total_price = 150.75
is_logged_in = False

Multiple Assignment

Assign multiple variables in one line:

a, b, c = 1, 2, 3
name, age = "John", 55

Constants

By convention, constants are written in uppercase:

PI = 3.14159
MAX_CONNECTIONS = 10