‫Now let's move on to list
‫Lists are very similar to string
‫Except that each element can be of any type.
‫In a string each element
‫Was the character
‫But in list
‫Each element can be of any type.
‫So to define a list we have to use square bracket
‫So if I write L1 L1 is a variable name
‫Then in a square bracket if I write 1234
‫12345
‫And run this
‫L1 is our list.
‫To check right type
‫L1
‫It is showing that the type of L1 character is list
‫To print all its value we write print L1
‫And we are getting all the values that L1 is containing
‫Just like strings we can also use the same ####### techniques to manipulate list.
‫So if I write
‫L1
‫Square bracket
‫Zero
‫This will give me
‫Element present at the 0th location Which is one
‫So here one is that location 0,2 is at the location
‫1
‫3 is at location 2
‫4 at 3 and 5 at 4.
‫Just like a string I can use colon to select multiple
‫Element
‫If I write 0 colon 3
‫Will give me the first 3 elements
‫Here the location 3 is excluded so I will get
‫Elements
‫Present at location 0, location 1 and location 2
‫3 is excluded
‫Just like slicing in a string
‫You can also use step
‫So if I write L1
‫Now if I want to select all the elements I will just right colon
‫Then I will write another colon
‫So first argument was for the starting location second argument was for the
‫Stopping location and the third argument is for the steps.
‫We are not giving starting and stopping location since we want all the elements.
‫Now we are defining step as two
‫So it will give me all the alternate elements of my list
‫So again it is ignoring 2 and 4 since we provided Step of 2.
‫It is not necessary for list to have all the elements of same type
‫You can assign different types of element in a single list so if I write L2
‫Equal to
‫Square bracket
‫1
‫Comma
‫I want to provide a string I will write in double quotation
‫A
‫Comma if I want to provide floating point numbers I will write 1.0
‫And if I run this
‫My ######## is created and the data type of my first element is ####### second element is a string and the
‫third ####### is #######
‫But we can assign
‫All this in a single list as well.
‫So if you want to check you can check using the type operators write type
‫Now inside L2 I want the data type of first elements I will write zero
‫And it is end because one is our end
‫If I write type
‫Do
‫Square bracket 1
‫Which is string because our a is an double quotation and we have provided it as a string
‫write L2
‫Our L2 is a list that's why we just write type of L2
‫It will showing us list.
‫We can also provide list as a element of another list
‫So we write L3 equal to
‫L2
‫Comma
‫1
‫Comma
‫2.0
‫If you run this command
‫So we have provided list
‫L2 list inside L3 so if we print L3
‫Here our first element is a list which is L2
‫and our second element is int and third element is
‫float.
‫So just to check if we write
‫Type of
‫L3
‫Comma zero
‫This is showing us a list because we have provided L2 inside of L3
‫Now
‫We are mentioned how to select the first element of L3
‫We can just write L3
‫And in bracket we can write 0.
‫This will give us the first element of my first element is a list
‫How to select first element of that list?
‫We can write
‫L3
‫Square bracket I will first write zero
‫Right now I am exercising
‫My first element which is the list
‫Now if I want the second element of this list
‫I will write in a square bracket 1
‫If I run this command it will give us
‫Character A this type of list are called
‫Nested lists because we have list inside a list.
‫Now you can also create list using special functions such as #######
‫If I write list
‫And in bracket write range
‫In range I have to provide starting point stopping point #######
‫So if I provide 0 as my starting point 10 as my stopping point
‫And step as
‫2
‫And if I run this
‫Range will provide me an output
‫Of 02468
‫And I am using list keyword to convert this output in the form of
‫List.
‫So we can create another variable l4 equal to
‫List
‫Range 0 to 10 with step 2.
‫If we print a variable l4
‫We can see zero is our first element
‫246 and then 8 if you want to sort it
‫The descending order we'll write L4 as sort, sort is a function
‫And the argument here is Reverse equal to true if you don't give this reverse equal to true
‫It will automatically sorted in the ascending order but we want it in the descending order
‫That's why we are providing argument rivers equal to true
‫Now if we check L4
‫You can see
‫It is sorted in the descending order or first element is 8 which is the
‫Largest
‫And the last element is zero the smallest element.
‫If you want to add more elements into
‫Our list
‫We can use append function will write L4
‫Dot append
‫And in bracket
‫Mentioned
‫Variable
‫Here we are writing pen
‫This
‫Add
‫The value 10 at the end of the list
‫You can see
‫We have 10 at the end of the list
‫If you want to change value of any element we can just write
‫That element address so L4 square bracket zero
‫This is our first element and we want the value of this element
‫To be three equal so if we write equal to 3
‫It will automatically change the value of
‫Of the first element to 3
‫So there it was it now the value is 3
‫If you want to change values of multiple elements we can write
‫L4
‫I want to change the value of second and third will write one
‫Colon three this means element second and third
‫since 3 is not included
‫And one is included.
‫And then in the square bracket
‫You can write the values for ignore if we write 5, 7
‫Now we are replacing this 6 to 5 in this 4 to 7 so we'll run this and
‫You can see the values are now changed.
‫In a way we are providing another list to replace the elements of this list.
‫Now if you want to insert an element at an specific index
‫Will do this by using a Keyword insert if we right L4
‫Insert
‫And we want to insert
‫This element at the first position will write zero
‫since 0 is our 1st
‫Position will write 15.
‫If we write 15 in double quotation this means we are passing this element as a string
‫So let's do this
‫You can see
‫We have inserted this 15 string
‫At the first position now all my other elements
‫are shifted by one space
‫Now if you want to remove element of this string we can do this
‫By using two commands first visit remove function and the second is the delete function
‫Remove function will remove the value of that element so for example if I write remove
‫Bracket 3 and 4 dot remove bracket 3 it will first search the element
‫Which have the value 3
‫That is this is the second element so it will delete this second element from this.
‫Whereas if I am using delete
‫It will delete the position of that element
‫So for example
‫if we write
‫Dell
‫,L4
‫And then I have to specify the location so if I write zero that means the first location
‫Will be deleted
‫we'll run L4
‫Can see the ###### is now deleted
‫We are only
‫Only 5 7 2 10 are remaining.
‫Now if you want to know more about less you can write help
‫Help is a Keyword you can use anywhere you can write help and in bracket
‫You can write list
‫if you run this
‫You will get all the information about list we are not going to discuss all here
‫and just quick tip if you want to remove your output
‫Just select the cell
‫Convert it to low MB and then again convert it to 4 then now your output is ######
‫If you have some large output you can you probably want to do this
‫Now let's move on to tuples, tuples is another data type
‫Provided by Python
‫So tuples are just like list
‫Except that they cannot be modified once created
‫That's why we call them
‫Immutable
‫So tuples are immutable
‫and a list
‫Is mutable
‫So to assign a tuple we use
‫Parenthesis
‫So for list we use square bracket for tuples we use
‫Parenthesis so if you write
‫P
‫Equal to
‫Parenthesis
‫1, 2.
‫If you see
‫When we were out putting the list
‫There were square bracket at the end and at the start
‫But for the tuples we have
‫Parenthesis
‫And we cannot change the values of this elements
‫So
‫Just you try
‫If you want to change the value of first element we can write P1
‫Then square bracket 0
‫Equal to 5
‫Since this is a tuple
‫Will get an error while running this you can see the error is
‫Tuple object does not support item assignment you cannot change the value of tuple once created.
‫To get the type of the ####### type T1
‫You can see the type is tuple.
‫There are very limited use cases of tuples and we will hardly use tuples
‫In our analysis let's move on to
‫The next data type which is dictionary.
‫Dictionaries are also like list
‫Except that each element is a key value pair
‫And the syntax for assigning dictionaries is using curly brackets in a list
‫We use the square bracket in tuple we use parentheses in dictionaries will
‫Curly bracket.
‫So
‫in our list
‫There are indexes starting from 0 1 2 3 4 here you can say that you are
‫You are assigning this indexes on your own so for each element you have to assign an index
‫Which we call a key.
‫And the element is called as value that's why we define dictionaries as a collection of key-value
‫Pairs.
‫So to assign a variable B1
‫Which is the dictionary will write equal to curly bracket
‫Now here first we have to write the key
‫######
‫And then get colon and the value of this key
‫Which is one
‫Comma
‫key 2
‫So in a list
‫There were indexes 01234 this T1 T2 T3 and just like indexes.
‫if we run this
‫we'll write ###### run this you can see
‫In this we were only getting the values
‫And here we are getting first the keys
‫And then the value.
‫So if you want
‫You can see that type of this D1 write type P1
‫The type is
‫Dictionary.
‫Now
‫If I want to Grab the first value I will write D1
‫And in square bracket
‫I will write keyone.
‫if we run this
‫You can see
‫for the key
‫key1 we have a value of 1
‫In a list
‫We were using directly using the indexes such as 0 1 2 3 4
‫In dictionaries we have to use this keys
‫To get the value of that pair.
‫So
‫if I try to write like this D1 square bracket 0 that is the first element I will get an error
‫I always have to refer this value using the key of this value.
‫So Python also supports
‫Conditional statement such as
‫If, else or else if
‫It also support
‫Loops such as for loop and while loop
‫We are not going to discuss this here
‫If you want you can explore
‫This
‫On your own
‫If needed we will discuss it
‫In our upcoming lectures.