Python Tutorial 7

 Data Types: List and Tuples

In this lesson, we will learn about lists and tuples. We already learned how to assign a value to a variable. And then we did strings and numbers, which will be enough to do anything we want with computer programming. However, what happens when we have a set of facts? 
like a list of students.
So let's create a new variable called students with random names and let's put them inside a string.








These are some examples of the trivial things we want to do with our data, but it is going to be hard if they are stored as strings.

Now how many students do I have? 
What's the name of the first student?
 How can I add a new student to that list?
 How can I remove a student?

In Python, we can use list and tuples to solve that problem. In other programming languages they're commonly called arrays.
So let's change the type of variable "students". To create a list we use braces [ ]















As we can see, the above is now a list.
Tuples are very similar. The only difference is that they are immutable. We will talk about that later.
Now that we have a list, all the operations I just mentioned are really easy to perform.
How many students do I have? We can get it from the len function, which will return the number of elements in a list.









What's the name of the first student?
Let's use braces!









like strings, lists, and tuples are also sequences. And their elements can be of any data type. We can have a list of strings like we have right now, a list of numbers, a list of strings and numbers, and we can even have a list of lists and so on. 
So now a quick challenge for you. Can you get the name of the last student?
How about the names of the first two students? Try it out, and then get back here when you're done!

















As quoted in yellow, you can see an error message:'student' is not defined and was generated from running the code. Why? This is because there was no such variable as student; that is a typo; it should be students with an's' Python is very sensitive, so be careful. 










Tuples
Tuples are like a list, but they're immutable. So let's say I want to store the months of the year. I can use a list, but can they ever change? Probably not. So in this case, it's better to use a tuple. That is represented by parenthesis. 







We can see the type of the variable as a tuple, which means we can't add or remove or change any element  from it anymore, but we can get a value from it the same way we do with lists.





Now let's use a new file from the menu and start writing our code.
So, can I use a list to store the months of the year? Sure, but using a list is safer. So we make sure the list won't be changed later, even by accident, which could happen in complex programs. It is important to know that strings are also immutable. We can't add, remove, or change elements with strings and tuples. We can get the size of them and get specific elements from them. But those impossible things with tuples are possible with lists. In our next tutorial, we will have an exercise on lists and tuples.
 
We will later learn how to change an element of the list using the equal to sign (=), which will generate an error with a tuple; add an element to the list using the append method; insert an element using the insert method; remove an element from a list using the pop method while indexing; remove specific elements using the remove method; and also, interestingly, merge two lists. Good luck, and happy coding!
 

































No comments:

Post a Comment

Python Programming Tutorial 15

 Error Handling Anytime we work with user input, we should make sure not only the data is valid and within the range we want, like what we d...