Variable Assignment

Code Examples

Example 1

Creates a variable called name and stores the text "Alice" (a string) in it.

name = "Alice"

Example 2

Creates a variable named age and stores the integer value 12 in it.

age = 12

Example 3

Creates a variable named is_student and assigns the boolean value True to it. Boolean values in Python are capitalized: True and False.

is_student = True

Example 4

Assignment from other variables. First, x is assigned the value 10. Then y is assigned the value of x (which is 10). Changes to x later won't affect y.

x = 10
y = x

Example 5

Chained assignment. This assigns the same value (0) to multiple variables in one line. All three variables (a, b, and c) will reference the same value.

a = b = c = 0

Example 6

Assigning different values to multiple variables. a gets the value 10, b gets 20, and c gets 30. Number of variables must match number of values. Makes code more concise than writing three separate assignments. Also called "tuple unpacking" or "sequence unpacking".

a, b, c = 10, 20, 30