Variables in Python

Greetings! Some links on this site are affiliate links. That means that, if you choose to make a purchase, The Click Reader may earn a small commission at no extra cost to you. We greatly appreciate your support!

Variables are the building blocks of a Python program and we will be using them a lot. Therefore, let us discuss them in-depth in this lesson.


What is a Python Variable?

A variable in Python is a symbolic name that points to the location where data is stored in memory. Variables are created or declared whenever a value is assigned to a character or sequence of characters in Python.

In the example below, the value 10 is being pointed by the variable called val_num.

# Declaring the variable called val_num with the value 10
val_num = 10

# Printing the value pointed by the variable called val_num
print(val_num)
OUTPUT:
10

Since the variable val_num is just a container pointing to where the data is stored in memory, the variable can be assigned to another variable as well. This passes the reference of the value to the assigned variable as well.

# Assigning the variable val_num with the value 10
val_num = 10

# Assigning the variable sec_val_num with the value 10
sec_val_num = val_num

# Printing the value pointed by the variable sec_val_num
print(sec_val_num)
OUTPUT:
10

We can also change which value a variable points to as show in the example below:

# Assigning the variable val_num with the value 10
val_num = 10

# Assigning the variable val_num with the value 20
val_num = 20

# Printing the value pointed by the variable val_num
print(val_num)
OUTPUT:
20

Best practices and rules for naming a variable in Python

In order to properly name a variable in Python, we must follow a set of rules like in any other programming language. The rules and best practices are as follows:

  1. Variables names should only consist of a combination of lowercase letters (a-z) or uppercase letters (A-Z) or digits (0-9) or an underscore (_).
  2. A meaningful word should be used as a variable name. For example, age is a meaningful variable name that explains that the variable holds a reference to an age value.
  3. Variable names should not start with a digit.
  4. Variable names consisting of two or more words should have an underscore (_) in between them. For example, date_of_birth is a variable name that holds a reference to a date of birth value.
  5. Variable names should not use special symbols other than an underscore (_).

Keywords in Python

Python keywords are case-sensitive words in Python that have a Python-specific meaning. Such keywords cannot be used as variable names and are reserved by the Python programming language.

Since keywords define the syntax and structure of Python, they are large in number and remembering all of them is very hard when starting out. The correct way to memorize such keywords is by actually using them in Python and we will be doing so throughout the course. But, for now, here are some example Python keywords along with their meaning:

Python KeywordMeaning
defUsed for creating a function in Python
globalUsed for declaring a global variable in Python
classUsed for creating a class in Python
importUsed for importing libraries in Python
TrueUsed for denoting a boolean

Again, you do not have to worry what the above keywords mean right now. We will cover them throughout this course.


What is a Constant in Python?

A constant in Python is a variable whose value is not meant to be changed. The general practice for declaring a constant is by using uppercase letters for the variable name.

In the example below, PI is a constant whose value is not meant to be changed.

# Declaring a constant called PI with the value 3.14
PI = 3.14

# Printing the value pointed by the constant called PI
print(PI)
OUTPUT:
3.14

Now you know the most basic concepts related to variables in Python. We will build on these concepts starting from the next lesson of this course.

Leave a Comment