Variables

What are variables?

A variable in Python is a symbolic name that serves as a reference or pointer to an object. Once an object is assigned to a variable, it can be referred to by that name. However, the data is still contained within the object.

In Python, a variable is a named location in memory that stores a value. Variables are used to store data values in programs. When you create a variable, you specify a name for the variable and a value to store in the variable. You can then use the variable name in your program to refer to the value stored in the variable.

For example, you might create a variable named x and assign it the value 10. You can then use the variable x in your program to refer to the value 10.

# In this example, the variable x is created and assigned the value 10. 
# You can then use the variable x in your program like this:
variable = 1

# You can also assign a new value to a variable using 
# the assignment operator (=). For example:
variable = 2

# A variable with a type hint
variable_with_type: int = 1

print(variable_with_type) # print the result of our variable

Variable Rules in Python

  • A variable must start with a letter or the underscore character.

  • A variable cannot start with a number.

  • A variable can only contain alphanumeric characters and underscores (A-z, 0-9, and _ )

  • Variables are case-sensitive (age, Age and AGE are three different variables)

Last updated