Types of variable assignments in Python

Introduction

Variables can be defined as containers for storing data values. They make it easier for us to manage or make corrections to our code. As beginners, we can write our code without variables. However, as we begin to advance in learning, we will understand their importance.

Variable assignments

There are two types of variable assignments:

  • Single value assignment: Variables that store only one data value.
  • Multiple value assignment: Variables that store more than one data value.

Single value assignment

This is a variable where only one data value is assigned to it. The data values can be of any data type.

Example 1

name = "Favour Peters"
print(name)

Explanation

In the above example, name is the variable, while "Favour Peters" is the data value whose data type is a string.

Example 2

age = 20
print(age)

Explanation

In the above example, age is the variable while 20 is the data value whose data type is an integer.

Note: While assigning these data values to variables, their representation changes according to their data type. For numerical data types like floats and integers, no special symbol is needed. For strings, we use double quotes (""). Different data types are represented differently.

Multiple data assignment

This is a variable where more than one data value is assigned to the variable. These data values can also be accessed individually.

Example 1

team = "Favour", "Victor", "Clarence"
print(team)

Let's access the individual data values in team:

team = "Favour", "Victor", "Clarence"
print(team[1])

Explanation

Using index count, we start from index 0. So "Favour" is at index 0, "Victor" at index 1, and "Clarence" at index 2. We access the data value at index 2.

Note: In multiple data variables, we use commas (,) to separate the data values. When accessing these data values individually, we use square brackets([]) for their index numbers.

Example 2

We can also use different data types in one variable:

diff = "Favour", 20, True
print(diff)

Explanation

The variable diff contains the following data types:

  • "Favour": String
  • 20: Integer
  • True: Boolean