Search⌘ K
AI Features

Assignment Statements

Explore how to use assignment statements in Python, including the standard syntax and operation shortcuts. Understand the walrus operator introduced in Python 3.8, which enables assigning and returning a value in a single expression to write more concise code.

We'll cover the following...

Syntax

Assignment statements consist of a variable, an equal sign, and an expression.

Here’s an example:

Python 3.5
x = 1 # Assigning the value 1 to a variable x
print("x:", x)
y = 5 # Assigning the value 5 to a variable y
print("y:", y)
z = x + y # Assigning the value of the addition between x and y to z
print("z:", z)

Assignment shortcuts

You can also use shortcuts when assigning values. This is done in Python by using an ...