This lecture is all about another essential structure in Python, called a tuple.
Tuples
Tuples are similar to lists, so we can use them to store a list of items. But unlike lists, we can not modify them, add new items or remove existing items. We can say that the tuples are immutable.
Let me show you. Tuples are identified by parenthesis instead of the square brackets in lists.
numbers = (4, 1, 7, 5, 0, 9)
Now, if we pull out the methods list for tuples, you can see that we don’t have the append or insert methods so new items cannot be added to this tuple. We only have two methods: count and index.
We use “count” to count the number of occurrences of an item. The “index” is used to find the index of the first occurrence of an item. So, we can only get information about a tuple, and we can’t change it. By the way, these other methods that you see here, start with two underscores.
We refer to these as magic methods. They’re more of an advanced topic, and we will touch on them in our future series.
Other than being immutable, tuples share most of the characteristics of a list.
Practically speaking, most of the time you’ll be using lists, but tuples are also useful. If you want to create a list of items and make sure nowhere in your program do you accidentally modify that list, then it’s better to use a tuple.
Unpacking
In this section, I’m going to show you a powerful feature, we have in python called unpacking.
Let’s define a tuple “coordinate”
coordinate = (1, 2, 3)
Now, imagine these are the coordinates for ‘x’, ‘y’ and ‘z’. Now let’s say, we want to get these values and use them in a few complex expressions in our program. Maybe, we want to include them as part of a large complex formula.
So, together we’ll have to write code like this:
coordinate[0] * coordinate[1] * coordinate[2]
This line of code will multiply all the coordinates but it is inefficient to write our code like this. First, let’s use a better approach and store these coordinates in separate variables.
x = coordinate[0]
y = coordinate[1]
z = coordinate[2]
That is better. Right? So nothing new so far. Now, let me show you a powerful feature called unpacking and with that, we can achieve the same result with far less code.
x, y, z, = coordinate
Now, this single line of code is exactly equivalent to the three lines of code written above. Let me explain to you how it works.
When the Python interpreter sees this statement, it will get the first item in this tuple and assign it to the variable. Similarly, second and third items will be assigned to the variables. In doing this, we are unpacking this tuple into 3 variables. Let’s check our output.
Unpacking can also be done with lists.
Dictionaries
We are going to learn about dictionaries in Python. We use dictionaries in situations where we want to store information that comes as key-value pairs.
Here’s an example. Think of a customer with a bunch of attributes like name, email, phone number, address, and so on. Now each of these attributes has a value. For example:
name = Jay
email = [email protected]
In this example, our keys are name, email, and phone. Each key is associated with a value so this is where we use a dictionary. With a dictionary, we can store a bunch of key-value pairs. Let me show you how to define a dictionary in python.
customer = {
"name": "Jay",
"email": "[email protected]",
"age": 30,
"is_verified": True
}
The key can be set to strings, numbers, boolean, or anything. The keys are unique and can only be defined once, i.e. I cannot define “age” again with some other number. They are case-sensitive too.
Now, if I type customer["name"],
it will call the customer’s name. Let’s print it on the terminal:
We can also use the methods with dictionaries. The same function demonstrated above can be replicated by using the “get” method.
There is one particular difference between the above two methods. Let me demonstrate that.
You can see that when we simply printed a non-existent key, it resulted in an error. On the other hand, using the method to call out non-existent key results in a “none” result which represents the absence of a value. When using the “get” method, we can also supply a default value.
For example, if this dictionary doesn’t have a “birthdate” key, we can supply it like this:
customer.get("birthdate", "Jan 01, 1994")
We can also update any key in the dictionary, similar to a list. Let’s say we want to update the name key in our dictionary. Here’s how we will do it.
customer["name"] = "Shahbaz"
A similar syntax can be used to add a new key in our dictionary too. Let’s add the key “birthdate” in a similar fashion.
customer["birthdate"] = "Jan 01, 1994"
So, this is the basics of using dictionaries in Python. They’re extremely important and they have a lot of applications in the real world.
Exercise
You are going to write a program that converts digits into words like this:
Solution
Here’s the solution for you guys.
phone = input("Phone: ")
digits_mapping = {
"1": "One",
"2": "Two",
"3": "Three",
"4": "Four",
"5": "Five",
"6": "Six",
"7": "Seven",
"8": "Eight",
"9": "Nine",
"0": "Zero"
}
output = " "
for ch in phone:
output += digits_mapping.get(ch, "!") + " "
print(output)
Similarly, you can build other fun programs like an “emoji converter” Have fun PYTHONERS!
Wrap Up!
I hope you enjoyed this lecture. Next, we are going to dive into a significant aspect of Python programming, the functions, and parameters.
Leave a Reply