Previous Tutorial: Python Variables (Part 1)
Python Variables
Continue Python variables tutorial. In the previous tutorial, we learned the basic introduction of variables. In this tutorial we are going to learn about how to assign multiple values to a variable and how to unpack a collection. But before that, let’s see the case sensitivity in variable names.
Variable Names are Case-Sensitive
Python is a case-sensitive language. In Python, uppercase and lowercase are treated differently. That’s why variable names are case-sensitive. Whenever we want to use the variable, we have to be aware of the case sensitivity in variable names. For example:
firstname = 'Python'
print(Firstname)
NameError: name 'Firstname' is not defined.
We get NameError
in the output with the message, ‘Firstname is not defined,’ because we create a variable named firstname
with a small f and try to print the value of Firstname
with a capital F. So f and F are different in Python. Another example:
firstname = 'Python'
Firstname = 'Java'
Now don’t get confused that we are updating the value of firstname
. Now these are two different variables (firstname and Firstname
). Another example:
text1 = 'Python'
texT1 = 'Java'
print(text1)
print(texT1)
Python
Java
We can see the difference, text1
and texT1
are two different variables here. So variable names are case-sensitive. We have to use the correct name to access that variable.
Variables are Dynamically Typed
In other programming languages, variables are statically typed. It means a variable is initially to have a specific data type, and any value assigned to it during its lifetime must always have that same type. For example, if Python were a statically typed language and we assigned a variable like this:
num = 1000
Now here we created a variable named
num
, which stores an integer value. Now if we update this value from an integer to a string:
num = 1000
num = 'Python'
Now if Python were a statically typed language, we would get an error in the output. Because we first assigned the num
variable to an integer data type, and now we are trying to assign the num
variable to the string data type. This is not possible in a statically typed language because we first assign an integer value to a variable, and now we cannot assign any other data type value to this variable. But Python is not a statically typed language; we can assign any type of value to a variable. For example:
num = 1000
print(num)
num = "Python"
print(num)
1000
Python
Because Python is a dynamically typed language, we easily assign an integer data value and a string data value to the same variable.
Assign Multiple Values to a Variable
Until now we only assigned a single value to a variable. However, we can assign multiple values to multiple variables or a single value to multiple variables.
Assign Multiple Values to Multiple Variables
We can easily assign multiple values to multiple variables in Python. For example:
text1, text2, text3 = 'Python', 'Java', 'PHP'
print(text1, text2, text3)
Python Java PHP
In this example, we created 3 variables and assigned 3 values to them. Instead of assigning a single variable, like text1 = 'Python', text2 = 'Java', text3 = 'PHP'
, we assign multiple variables in a single line. Here is a very important rule to remember: the number of variables should be the same as the number of values; otherwise, we will get an error. For example:
text1, text2, text3 = 'Python', 'Java'
print(text1, text2, text3)
ValueError: not enough values to unpack (expected 3, got 2)
So we get ValueError. It shows expected 3, got 2. Because we only assign two values for three variables. So it is very important to assign the same number of values to the same number of variables.
Assign One Value to Multiple Variables
In the upper example, we assign multiple values to multiple variables. However, if we want, we can assign a single value to multiple variables. For example:
text1 = text2 = text3 = 'Python'
print(text1)
print(text2)
print(text3)
Python
Python
Python
Here we assign a single value, 'Python'
to 3 variables (text1, text2, text3)
. Here is a difference, in this example, we use equal sign (=
) when creating the variables, and in the previous example, when we create variables text1, text2, text3
we use the comma (,) sign
. This is the difference to remember.
Now what if we assign multiple values in the same situation?
text1 = text2 = text3 = 'Python', 'Java'
print(text1)
print(text2)
print(text3)
('Python', 'Java')
('Python', 'Java')
('Python', 'Java')
Now each variable consists of multiple values. So we get the output in a tuple. A tuple is a data type in Python used to store multiple collections of values.
Assign Multiple Values to One Variable
In the upper example, we assign a single value to multiple variables. It is possible to do the exact opposite. We can assign multiple values to a single variable. For example:
text1 = 'Python', 'Java', 'PHP'
print(text1)
('Python', 'Java', 'PHP')
Here we create a single variable and assign multiple values to this variable. And this variable consists of all values as a tuple. We can check the data type with the help of the type()
function.
text1 = 'Python', 'Java', 'PHP'
print(text1)
print(type(text1))
('Python', 'Java', 'PHP')
<class 'tuple'>
So we get the output in a tuple.
How to Unpack a List Collection into Variables
If we have a collection of values in a list or in a tuple. We can unpack those values directly into variables. This is called unpacking. For example:
animals = ['Cow', 'Dog', 'Cat']
a, b, c = animals
print(a)
print(b)
print(c)
Cow
Dog
Cat
H we have a list named animals
, which has three values (Cow, Dog, Cat)
. We directly assigned these three values to three variables (a, b, c)
. We get a = Cow, b = Dog, c = Cat
in the output. This is called unpacking a collection. Now what if we only assign two variables instead of three?
animals = ['Cow', 'Dog', 'Cat']
a, b = animals
print(a)
print(b)
print(c)
ValueError: too many values to unpack (expected 2)
We get ValueError
because we assign two variables (a, b)
so there should be two values, but the list has three values. So the number of variables should be the same as the number of values. However, there is an option when we have mismatched numbers of values and variables. We can use the asterisk (*) sign. For example:
animals = ['Cow', 'Dog', 'Cat', 'Rat']
a, *b = animals
print(a)
print(b)
Cow
['Dog', 'Cat', 'Rat']
Here we have four values in the list, and we assign only two variables. But we use the asterisk sign with the second variable (b)
. It means we want all the rest values to be assigned to the b variable
. So the first variable (a)
get the first value Cow
, and then the rest of the values are assigned to the second variable (b)
. Now if we change the asterisk sign from b to a
:
animals = ['Cow', 'Dog', 'Cat', 'Rat']
*a, b = animals
print(a)
print(b)
['Cow', 'Dog', 'Cat']
Rat
Now we use the asterisk sign with the first variable (a)
. So the last value will be assigned to the second variable (b)
, and the rest of the values will be assigned to the first variable (a)
. So it does not matter where we used the asterisk sign. A single value will be assigned to another variable, and the rest of the values will be assigned to the asterisk sign variable. One more example:
animals = ['Cow', 'Dog', 'Cat', 'Rat']
a, *b, c = animals
print(a)
print(b)
print(c)
Cow
['Dog', 'Cat']
Rat
Now here we assign another variable (c)
and put the asterisk sign on variable (b)
. So we already learned that a single value will be assigned to another variable, and the group of values will be assigned to the variable that has an asterisk sign. Now what if we used multiple asterisk signs?
animals = ['Cow', 'Dog', 'Cat', 'Rat']
a, *b, *c = animals
print(a)
print(b)
print(c)
SyntaxError: multiple starred expressions in assignment
We get SyntaxError; it indicates that we cannot use multiple asterisk signs. So this is how we unpack a collection.
Conclusion
This is the end of this tutorial. In this tutorial we learned about variable case sensitivity and dynamically typed programming. We also learn how to assign multiple values to a variable. In the end, we learned how to unpack a collection.
Next Tutorial: Python Variable (Part3)

