assignment

In Python, an assignment is a fundamental operation that allows you to attach values to variables. By using the assignment operator (=), you can assign the result of an expression or a concrete value to a variable.

Python is a dynamically typed language, so don’t need to explicitly declare variable types. Instead, during an assignment, Python will dynamically figure out the variable type based on its value.

Understanding assignments is key to harnessing Python’s power, as it allows you to manipulate data efficiently.

Examples

Here are some examples of how assignment works in Python:

Python
>>> # Assigning an integer to a variable
>>> x = 10
>>> x
10

>>> # Assigning a string to the same variable
>>> x = "Hello, Python!"
>>> x
'Hello, Python!'

>>> # Assigning the result of an expression
>>> y = x + " How are you?"
>>> y
'Hello, Python! How are you?'

In these examples, you can see how a single variable, x, can hold values of different types at different points in the code.

Tutorial

Python's Assignment Operator: Write Robust Assignments

In this tutorial, you'll learn how to use Python's assignment operators to write assignment statements that allow you to create, initialize, and update variables in your code.

intermediate best-practices python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated April 29, 2025 • Reviewed by Leodanis Pozo Ramos