Click for All Topics

What are Python Variables

In Python, a variable is a named location in the computer’s memory used to store a value. This value can be anything from a number to a string of text or even a more complex data type like a list or dictionary.

In other words:

  • A variable is a reserved memory location used to store values.
  • A variable is a container for storing data values.
				
					# assigning value water to a variable container bottle
bottle = "water"
# printing the value of the variable
print(bottle)                  

# Output of the above Python code:
# ---> water
				
			

In this example, we have assigned the value “water” to the variable bottle. We then print the value of the bottle variable using the print function.

Variables are important because

  • They allow us to store data for repeated use.
  • manipulate data in our programs.

We can assign a value to a variable using the equal sign (=), and then use that variable throughout our code.

Here are some examples:

				
					# assigning a value edSlash to a variable
name = "edSlash"
# printing the value of the variable
print(name)               

# changing the value of a variable to a new data Bob
name = "Education"
# printing the new value of the variable
print(name)                    

# Output of the above Python code:
# ---> edSlash
# ---> Education
				
			

In this example, we first assign the value “edSlash” to the variable name. We then print the value of the name variable using the print function. Next, we change the value of the name variable to Education”, and then print the new value.

Variables can also be used in mathematical operations:

				
					# assigning values to two variables
x = 5
y = 10

# performing addition using variables
z = x + y
# printing the result
print(z)

# Output of the above Python code:
# ---> 15
				
			

In this example, we first assign the value 5 to the variable x, and the value 10 to the variable y. We then perform an addition operation using the + symbol and assign the result to the variable z. Finally, we print the value of the z variable.

Overall, variables are a fundamental concept in programming and are used extensively in Python and other programming languages. They allow us to store and manipulate data in our programs, making it easier to create complex algorithms and applications.

Rules for naming variables in Python

In Python, there are some rules that you need to follow when creating variables.

1. Variable names can only contain letters, digits, and underscores. While variable names can contain digits, they canNOT start with a digit.

				
					# Valid variable names
score1 = 10
score2 = 20
score3 = 30

# Invalid variable names
# starts with a number
3score = 10
# contains a hyphen, Hyphen is not allowed in Variable naming
math-score = 20         
				
			

2. Variable names are case-sensitive. score is different from Score and SCORE.

				
					# Case-sensitive variable names
score = 10
Score = 20
SCORE = 30

# Printing the values
print(score)
print(Score)
print(SCORE)

# Output of the above Python code:
# ---> 10
# ---> 20
# ---> 30
				
			

3. Variable names should be descriptive and meaningful. Use names that indicate the purpose of the variable, such as age, name, or total_price.

Non-descriptive variable names should not be used as it becomes difficult to remember and keep track of them in longer codes.

				
					# Descriptive variable names
age = 25
name = "edSlash"
total_price = 50.00

# Non-descriptive variable names
x = 25
y = "John"
z = 50.00
				
			

4. Variable names should not be the same as Python keywords. Keywords are reserved words that have a special meaning in Python, such as if, else, while, for, etc.

				
					# Invalid variable names that are the same as Python keywords
if = 10
while = "hello"

# Output of the above code will give an error as it is not allowed/against the rules to use Python keywords as variable names.
				
			

Python keywords have predefined meanings in Python. These keywords are used to define the syntax and structure of code. For example, the keyword “if” is used to define a conditional statement in Python. It is important to note that using these keywords as variables can result in redefining them to a different meaning, which can create further errors in the code. Therefore, it is recommended to avoid using Python keywords as variables in order to ensure code clarity and avoid complications.

Tips & Tricks: You can determine whether a variable name is a keyword by observing a colour change in the IDE. If the IDE changes the colour of the variable name, then it is a predefined keyword and should not be used.

By following these rules, you can create variable names that are easy to understand and use in your Python programs.