Codectionary / Developer documentation / Python

Variables & Assignment

Variables in Python are names bound to values, created the moment you first assign to them - there's no need to declare a type in advance, since Python is dynamically typed. A single variable can be reassigned to a completely different type later in the program. Python also supports several convenient assignment patterns, including assigning multiple variables at once and using augmented assignment operators.

Syntax

variable_name = value

Examples

Basic Assignment

Creating variables of different types without any type declarations.

name = "Fola"
age = 21
gpa = 3.8
is_student = True

print(name, age, gpa, is_student)

Multiple Assignment

Assigning several variables in a single line.

x, y, z = 1, 2, 3
print(x, y, z)  # 1 2 3

# Assign the same value to multiple variables
a = b = c = 0
print(a, b, c)  # 0 0 0

Swapping Variables

Python's tuple assignment makes swapping values simple, with no temporary variable needed.

a = 5
b = 10

a, b = b, a

print(a, b)  # 10 5

Augmented Assignment Operators

Shorthand operators that combine an operation with assignment.

score = 10
score += 5   # score = score + 5  -> 15
score -= 3   # 12
score *= 2   # 24
score //= 5  # 4
print(score)

message = "Hello"
message += " World"
print(message)  # Hello World

Best practices

  • Use snake_case for variable names (user_name, not userName or UserName) to follow Python conventions (PEP 8)
  • Choose descriptive names that explain what the variable holds, even if it makes them longer
  • Avoid reassigning a variable to a completely different type partway through a function - it makes code harder to follow
  • Use augmented assignment operators (+=, -=, etc.) for conciseness when updating a variable based on its own value
  • Constants (values that should never change) are conventionally written in ALL_CAPS, even though Python has no true constant enforcement

At a glance

Purpose
Scripting and general-purpose applications
File extension
.py
Runs in
Python interpreter
Usually used with
Python standard library and packages

Specifications & further reading

Related Python documentation

True
In Python, True is a built-in constant that represents the boolean value of true. Alongside False, True enables logical and conditional operations. It's a fundamental keyword used throughout Python programming for decision-making, loops, comparisons, and boolean logic. True plays a key role in if statements, while loops, and logical expressions.
False
False is Python's built-in constant representing the boolean value false. It's the logical opposite of True and is essential for conditional logic, loop control, and boolean operations. False is used to indicate negative conditions, failed validations, or when something is not the case. Understanding False is crucial for writing effective control flow in Python programs.
None
None is Python's built-in singleton value that represents the absence of a value or a null result. It is its own type (NoneType) and is commonly used as a default parameter value, a placeholder before a variable is assigned real data, or a function's implicit return value when nothing is explicitly returned. Unlike 0, an empty string, or an empty list, None specifically means 'no value here', not 'an empty value'.
print()
print() is Python's built-in function for writing output to the console. It can accept any number of values, automatically converts them to strings, and separates them with a space by default. Its sep and end keyword arguments give fine control over formatting, making print() useful for everything from simple debugging to building formatted console output.