Python Variables

Jakob Jenkov
Last update: 2022-03-02

Python variables are little "containers" in which you can store values (data) which can be varied over time. A Python variable thus has a name and a value. When referring to a variable you refer to it by its name. When referring to a variable you are essentially referring to the value the variable contains.

Declaring a Variable in Python

Declaring a variable in Python is done simply by writing the variable name and assigning a value to the variable. Here is how declaring a variable in Python looks:

my_var = 123

The name of this variable is my_var. The value assigned to it is the numeric value 123. The equals sign tells Python to assign the value to the right of it - to the variable named to the left of it.

Assigning Values to a Variable in Python

You assign a value to a variable in Python in the same way as shown in the paragraph about about declaring a variable. You write the name of the variable, followed by an equals sign, followed by the value you want to assign to the variable. Here is how assigning values to a variable in Python looks:

my_var = 456
my_var = "Hello World"
my_var = 45.89

Reading the Value of a Variable in Python

You read the value of a variable by referencing its name. When reading the value you typically have to read it into something. Typically, that is another variable, or a parameter value for a function. Here is how reading the value of a variable in Python looks:

my_var = 789
my_other_var = my_var

print(my_other_var)

Jakob Jenkov

Featured Videos

Java Generics

Java ForkJoinPool

P2P Networks Introduction



















Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next