Now let's move on to lists lists are very similar to a string except that each element can be of any
type in the strings each element was a character but in lists each element can be of any type so the
final list.
We have to use the square records.
So if I write one and one is our a variable name then in the square right.
If I write 1 2 3 4
1 2 3 4 5 and run this everyone is a want list.
Check right.
Babe
L1 are destroying that type of L1 characters list to print all its value.
We.
Print L1
and we are getting all the values that anyone is containing
just like strings.
We can also use the same slicing techniques to manipulate lists.
So if I write a one square record zero this will give me an element present does the location which
is one.
So here one is at locations Zero.
Who is their location.
1 3 is their location 2 4 3 favorite for just like string.
I can use colon to select multiple elements.
If I write 0 4 then 3 it will give me the first three elements here.
The location 3 is excluded.
So I will get elements present at locations Zero location one location all three is excluded.
Just like slicing in the strings you can also use steps.
So if I write L1 now if I want to select all the elements I will just state for them.
Then I will write another.
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 a starting and stopping location since we want all the elements.
And now we are defining this step as two.
So it will give me all the elements of my list.
So again it is ignoring when for instance we decided to step off to
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 a to equate to square record one former if I want to create a string I will write and
double quotation a comma if I want to provide floating point numbers I will write 1.0 and if I run this
my 2 is created under that type of my first element is in second element to the string.
The tone at the moment is.
But we can say on this in that single list as well.
So if we want to check we can check using the type operator.
If I type now inside a do I want the data type of first element.
So I will write zero and they did the end because 1 is lower and 0 if I write type
square one.
Which is string because of what it is.
Double quotation and we have created a string as a string right.
What I do is a list.
That's why we just state type of I do it for the string us list.
We can also provide list as an element of another list.
So if we write L3 equate to
a do
Cuomo 1 Omar 2.0
if we've done this come.
So we have quite a list as you to list inside L3.
So if we print L3
here our first element is a list which is a true and never second element is in and third element this
load.
So just to check if we write type of and three former zero.
This is joining us as list because we have provided you two inside of L3.
Now we have mentioned how to select the first element of L3.
We can just write L3 and then record we can write 0.
This will give us the first element.
My first element is a list.
So how to select first element of that list.
We can write L3
and square the record.
I will first rate 0 right now.
I am accessing my first element which is the list.
Now if I want the second element of this list I will write in the squared record one if I done this
Come on it couldn't give us the character in these types of lists are called nested lists because we
have lists inside the list.
Now you can also create lists using special functions such as range.
So if I write list and then record and write Grange in Grange I have to provide the starting point stopping
point in this step.
So if I provide zero as my starting point then as my stopping point and step as to and if I run this
range will provide me an output of 0 2 4 6 8 and I'm using list keyword to convert this output and the
form off list so we can create another variable for equate to
range 0 2 10 with step 2.
Now if we bring out a variable L for
we can see 0 is over for sending my 2 4 to 6 and then 8 if we want it all sorted and then descending
order we'll write for the dots sort.
So do the function and the arguing it is reverse equator grew.
If we don't give this city what's equal to group it will automatically started in ascending order.
What we wanted in descending order.
That's why we are providing argument.
What's equally true.
Now if we check and before you can see a bit sorted in the descending order our first element is 8 which
is the largest and the last element is zero.
This mother said My
if we want to add more elements in our list we can use a pen function.
We'll write for that.
A pen
and then record you mentioned.
Really.
Well here we are writing Ben
this add the value pen at the end of the list as you can see we have Ben at the end of the list if we
want to change value of any element we can just write that element address.
So look for Square Records zero.
This is our first element and we want the value of this element to be three.
So it would equate to three it will automatically change the value of the first element to three.
So earlier it was a non-event Lewis three
if you want to change values off my table elements.
We can write for you want to change the value of second and third so we'll write one more than three.
This means elements second and third since three is not included and one is included.
And then in this square a record we can write the values.
So if we write 5 almost 7
now we are replacing the 6 2 5 4 7 3.
Then this you can see the values are not changed in the way we are creating another list to replace
the elements of this list.
Now if we want to in certain element account is specific index will do this by using a keyword insert.
So if we write for insert and we want to insert this element at the first position so we'll write 0
since 0 either plus or nation will write 15.
So 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 violation.
Now all my other elements are shifted by one in space.
Now if we want to remove element of this string we can do this by using who will command first as the
remove function.
And the second is that delete function remove function will remove the value of that element.
So for example if I write removed the code 3 and 4 remove record three it will first search the elements
which have the value three in that case this is the second element.
So delete this second element.
If we run this
whereas if I am using delete it really delete the position of that element.
So for example but for right then I added for and then I have to specify the location.
So if I and I zero that means the first location will be deleted.
So we've done this again and therefore you can see the plane is not on the network we are on the on
the 5 7 2 0 9 10 that making now if you want to know more about lists you can write.
Help help me look you where you can use anywhere you can write and record you can write this if you
run this
you to get all the information out.
We are not going to discuss all the.
And just so quick EP if you want to do more your output just select the set converted to blindly and
then again convert it to court.
Now you're also based on so if you have some large output you can.
You probably want to do this.
Now let's move on to the.
The palace is another data type voided whereby 10
so the police are just like list except that they cannot be modified once created.
That's what we call them immune tables.
So to balance out immune tables and a list is your but so to assign that up.
We use parentheses so for lists we use a square record for doubles we use parentheses if we write even
equation that sentences one comma to
so if you see when we were putting the list there were squared record at the end and they suck but for
the doubles we have parenthesis and we cannot change the values of this elements so just to write if
we want to change the value of this element we can write even
then squared records 0 equal to 5 since this is a table we will get an error while running this.
So you can see the error is double object does not support item assignment you can not change the value
of the apple once created to get the tape of the Stephen Page even you can see the typist double there
are very limited use cases of apples and we will hardly use the apples in our analysis.
Let's move on to the next day today which is dictionary
dictionaries are also like lists except that each element is a key value pair and thus index for assigning
dictionaries is using live records in the list.
We use a squared record in double we use parentheses in dictionaries release college records so in the
list there are indexes starting from 0 1 2 3 4.
Here you can see that you are assigning this indexes on your own.
So for each element you have the assignment index which we call up key and the element is called s value.
That's why we define dictionaries as a collection of key value pairs.
So to assign a variable be one which is our dictionary we'll write where we look at live records now
here.
First we have to write that key he won and then right colon and the value of this scheme which is 1
comma.
You do
so in a list there were index 0 1 2 3 4 this key 1 2 key.
3 are just like indexes
fee this
would be one run this you can see in this.
We were only getting the values in the here.
We are getting first the keys and then the value so if you're one you can see the type of this B1 type
B1 the typist dictionary.
Now if I want to grab the first value I will write B1 and then a squared record I will write even
if it on this you can see for the E given we have a value of once in the list we are using directly
using the indexes such as 0 1 2 3 4 in dictionaries we have to use this D to get the value of that bit
of
so far I tried to write like this.
B1 squared records 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 statements such as if as or else if and it also supports 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.