Python Assign A Variable

In Python, assign a variable means to give a variable a value. A variable is really just a placeholder to hold a value, so this is a natural exercise.

Variables and assigning them values are one of the foundational building blocks of Python.

Here, let’s assign the variable ‘x’ a value of 5. You do this via a single equal sign, or assignment operator.

# Looks very simple, but this is assigning a varible
x = 5

Remember that a conditional is denoted by the double equals sign “==” and will not assign your variable a value.

In pseudo-code this means, “make the variable x have a value of 5”

Let’s take a look at a python assign a variable code sample


Python Assign A Variable

Variables are one of the building blocks of the python language. Variables hold values.

To give a variable a value, you must assign it a value. This is done via a single equals sign, "="

Here we are going to define the variables x and y to give them values.

In [2]:
x = "Fall Colors"
y = 10
In [3]:
print (x)
print (y)
Fall Colors
10

We can also update a variables value by assigning it another value

In [4]:
x = "Summer Beach"

print (x)
print (y)
Summer Beach
10

Link to code above

Check out more Python Vocabulary on our Glossary Page