Skip to Content
Nextra 4.0 is released 🎉
VariablesWhat are Variables?

What are Variables?

Variables are used to store data. This data can then be used throughout your code. Below is an example of how to create a variable in Python:

# Example of a variable in Python age = 28
  • age is the variable name, also known as the variable identifier.
  • 28 is the value assigned to the variable.
  • The = (equals sign) is needed to assign the value to the variable.

So now we have created a variable age and assigned a value 28.

You can now use this variable in your code by simply referring to its name.

# Using the variable print(age) # This will print the variable value **28** to the console

Constants

Constants in programming are variables that are meant to remain unchanged throughout the program. Their value is constant.

Unlike other programming languages, Python does not have a built-in way to create constants. Meaning that constants in Python can technically be changed after you initially assign their value.

However, it is common to use uppercase letters to indicate that a variable should be treated as a constant and its value should not be changed later

# Example of a constant in Python PI = 3.14159

In this example, PI is intended to be a constant that represents the mathematical constant pi.

Next we’ll go over the different types of variables in Python.

Last updated on