Click for Python Variable Part 1
Python Variables Are Case-Sensitive
Variable names are case-sensitive. You have to write the correct variable name when you try to print or want to use the variable.
Code:
firstname = 'Python'
print(Firstname)
Output:
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.
Code:
firstname = 'Python'
Firstname = 'Java'
Now here, don’t get confused that we are updating the value of firstname
. Now these are two different variables (firstname and Firstname
).
Variable Are Dynamically Typed
In other programming languages, variables are statically typed. That 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 was a statically typed language and we assigned a variable like:
Code:
num = 1000
Now here we created a variable named num
, which stores an integer value. Now if we update this value:
Code:
num = 'Python'
Now if Python were a statically typed language, it would show an error. We first assigned the num
variable to the integer data type, and now we are trying to assign the num
variable to the string data type. So this is not possible in statically typed language because we first assign an integer value to a variable. So now we can only assign an integer value to this variable. But Python is a dynamically typed language. For example:
Code:
num = 1000
print(num)
num = "Python"
print(num)
Output:
1000
Python
So we can easily assign integer value and string value to same variable.
Assign Multiple Values
Now in the previous example, we have assigned only one value to a single variable. We can also assign multiple values to multiple variables or single values to multiple values.
Assign Many Values to Multiple Variable
Now here in this example, we are going to assign multiple values to multiple variables.
Code:
a,b,c = 'Python', 100, 'Java'
print(a,b,c)
Output:
Python 100 Java
So here instead of assigning separated values like a = ‘Python’, b = 100, c = ‘Java’. We simply assign multiple variables in one line. Now here is one very important thing to remember: The number of variables should be the same as the number of values; otherwise, we will get an error. For example:
Code:
a,b,c = 'Python', 100
print(a,b,c)
Output:
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 this is very important to assign the same number of values to the same number of variables.
One Value To Multiple Variables
We can also assign a single value to multiple variables. For example:
Code:
a = b = c = "Python"
print(a)
print(b)
print(a,b,c)
Output:
Python
Python
Python Python Python
Here is the difference: in this example, we use the equal sign (=) when assigning a single value. And in the previous example, when we write a, b, c, we use the comma (,) sign when assigning multiple values.
However, we can also assign multiple values in here, but the result will show in a tuple. For example:
Code:
a = b = c = "Python", "Java"
print(a)
print(b)
Output:
('Python', 'Java')
('Python', 'Java')
Also, we can assign a single variable with multiple values, and the result will be shown in a tuple.
Code:
a = 'Python', 'Java'
print(a)
print(type(a))
Output:
('Python', 'Java')
<class 'tuple'>
Unpack A Collection
If we have a collection of values in a list or a tuple. We can unpack those values directly into variables. This is called unpacking. For example:
Code:
animals = ['Cow', 'Dog', 'Cat']
a, b, c = animals
print(a)
print(b)
print(c)
Output:
Cow
Dog
Cat
Now here we have a list named animals, which have three values (Cow, Dog, Cat). So we assign directly these three values to three variables (a, b, c). So in the result we will get a = Cow, b = Dog, c = Cat. This is called unpacking a collection.
Now if we assign only two variables:
Code:
animals = ['Cow', 'Dog', 'Cat']
a, b = animals
print(a)
print(b)
print(c)
Output:
ValueError: too many values to unpack (expected 2)
We get ValueError (too many values to unpack, expected 2) because we assign two variables (a, b) so there should be two values, but animals
the list has three values. So the number of variables and the number of values should be the same.
However, there is an option when we have mismatched numbers of values and variables. We use the asterisk (*) sign. For example:
Code:
animals = ['Cow', 'Dog', 'Cat', 'Rat']
a, *b = animals
print(a)
print(b)
Output:
Cow
['Dog', 'Cat', 'Rat']
Here we have four values in list but we assign only two variables. 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) is assigned to Cow and then all the rest of the values will be assigned to the second variable (b). Now if we change the asterisk sign from b to a:
Code:
animals = ['Cow', 'Dog', 'Cat', 'Rat']
*a, b = animals
print(a)
print(b)
Output:
['Cow', 'Dog', 'Cat']
Rat
Now we use an asterisk 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 practical:
Code:
animals = ['Cow', 'Dog', 'Cat', 'Rat']
a, *b, c = animals
print(a)
print(b)
print(c)
Output:
Cow
['Dog', 'Cat']
Rat
Now here we assign another variable (c) and an asterisk sign with (b). So we already learned that a single value will be assigned to another variable and the group of values will be assigned to a variable that has an asterisk sign. Now what if we used multiple asterisk signs?
Code:
animals = ['Cow', 'Dog', 'Cat', 'Rat']
a, *b, *c = animals
print(a)
print(b)
print(c)
Output:
SyntaxError: multiple starred expressions in assignment
So we get SyntaxError; it indicates that we cannot use multiple asterisks. So this is how we unpack a collection.
End of Python Variables Part 2
2 thoughts on “Python Variables (Part 2): Case-Sensitive and Dynamically Typed: Assign Multiple Values And Unpack a Collection”