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.
There are two types of variable assignments:
This is a variable where only one data value is assigned to it. The data values can be of any data type.
name = "Favour Peters"print(name)
In the above example, name
is the variable, while "Favour Peters"
is the data value whose data type is a string.
age = 20print(age)
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.
This is a variable where more than one data value is assigned to the variable. These data values can also be accessed individually.
team = "Favour", "Victor", "Clarence"print(team)
Let's access the individual data values in team
:
team = "Favour", "Victor", "Clarence"print(team[1])
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.
We can also use different data types in one variable:
diff = "Favour", 20, Trueprint(diff)
The variable diff
contains the following data types:
"Favour"
: String20
: IntegerTrue
: Boolean