# Lists and Tuples in Python |Episode #9 |

Hello and welcome to another Python tutorial, this is our Python Series where we teach you python from Scratch. You have learn a lot till now. You have seen Strings and Escape sequences. If you are new to the series and don’t know how to catch up. So, don’t worry. You can CLICK HERE to see previous classes. Now we will move further to List and Tuples in python.

## What are Lists in Python?

Python lists are containers to store a set of values of any data type.

Well, while teaching you variables and datatype, I told you to think a variable like a container which can hold a thing like toffees (data type) of certain type. But what if you want all types of toffees or not only toffees. You also want Chocolates and other stuffs.

For example, you want to store marks of students, till now you to like:

Marks1 = 56

Marks2 = 78

Marks = 15

But what if we want all the marks in one place, then we can use lists. For example:

Marks = [ 56, 78, 15 ]

Here, Marks is a list of marks of students. Easy isn’t it?

And you can do it for pretty much everything.

Sample = [ “Kevin”, “Apple”, 7, 79.9, FALSE ]

Here there is a list named Sample in which we hold different types of data like Kevin and Apple which is a string type and 7 which is an integer and 79.9 which is a floating-point number and FALSE which is a Boolean.

## List Indexing

A list can be indexed just like a string. Here is an example below.

List = [1, 5, 10, “Kevin”, 17]

List – 1

List – 5

List – Kevin

List – Results into an error as the following index is not in a list.

You can also change the values of elements using indexes. For example:

List = 53

Now the value at index 1 is 53. Earlier it was 5.

Let’s write a program to print the sum of marks of a student in 4 different subjects.

Here first we initialize a list of marks then we iterate it using a for loop and then we sum the marks and print the sum. Try out yourself: Write a program that takes a list of marks and gives the average of marks.

## List Methods

There are some inbuilt functions in the list to make work easier. Let’s see some of them.

L1 = [ 1, 8, 7, 2, 21, 15 ]

• L1.sort() – Sort and Update the list like here L1 = [ 1 ,2 ,7 ,8 ,15 ,21 ]

• L1.reverse – Reverse and Update the list like here L1 = [ 15, 21, 2, 7, 8, 1 ].

• L1.append(16) – It adds the element at the end of the list. Here 16 will be added.

• L1.insert(3, 5) – This will add 5 at the 3rd index.

• L1.pop(2) – It will delete the element from index 2 and return its value.

• L1.remove(15) – It will remove 15 from the above list.

## Tuples in Python

A Tuple is an immutable data type in Python.

So first of all, What’s immutable?

Immutable simply means “that cannot change”. So, Tuples are that data types that cannot be changed, once formed. Ex:

Tuple = ( 1, 67, 90, 15 )

This is a tuple that contains some value. Now if we try to change the tuple value at index 1 which is 67, then it will throw an error. Let me show you.

Did you see the error, Tuple doesn’t support Item assignment. This can be done on the list, but we are unable to do that in a tuple and that’s a specialty of Tuple.

Tuple1 = () – Empty Tuple

Tuple1 = (1, ) – Tuple with a single element. Always remember to put a comma if there is a single element in Tuple.

Tuple1 = (1, 6, 7,) – Tuple with more than one element.

So, Once defined a Tuple’s elements can’t be altered or manipulated. So, that’s all for what is a tuple and how to use it. Now let’s see some Tuple Methods ahead.

## Tuple Methods

Num = (1, 7, 2 )

• Num.count(1) – It will return the number of times 1 occurs in Num.

• Num.index(2) – It will return the index of the first occurrence of 2 in Num.

So, that’s the end of Today’s class. We get into some pretty interesting stuff today. For a revise, we looked into Lists in Python, Their Indexing, and List Methods. Then we move ahead to Tuples and see how it works.

So, that’s all for today. If you want to check more List and Tuple Methods, CLICK HERE. This is Dolores Haze, signing off, I will see you in the next class, till then “Keep Coding” and Bye.