Day-8 Python + MYQL FREE Sessions | Codingdidi

Akansha Yadav teaches string operations and Python lists, covering len, f-strings, and list methods like append, extend, and pop. She explains that lists are mutable, can hold multiple data types, and allow duplicates, then walks through nested lists and list comprehensions before moving to tuples and sets next.
Chapters
Hello everyone, how are you all? Am I audible to you? Okay, see, I gave you one question to solve in the last session. So have you guys worked on that? Like in the tables, like one-to-one, one, uh, Yeah, that multiplication one, correct. If yes, then drop your solutions in the chat. Okay, so this is what you have written. I tried this yesterday, but, uh, it is coming like, uh, till 11, 11 cross 11. Okay, let's— we'll check it. What about others? Okay, let me know once my screen is visible to everyone. Yes, it is visible. Okay, this solution you have given, right? Yes, yes, you're getting up till 10. Yeah, yeah, that's correct. Oh yeah, your code is correct. To make it more readable, what you can do is you have to try with one more thing, that you have to display it in this manner, like going horizontally. Okay, then it will be more readable. It means like how we can do like easy new column. Okay, okay, like with the column, within the column itself I want these details. Okay, then it will be more readable, right? Up if it is 1 to 10 and then it will start from here 2 and then 3. So it is basically the work of rows and columns. Okay, okay, yeah, but the logic is all good. So I think now you are able to get it, right? Okay, so our next topic is list and list manipulation and string and string manipulation. So we have already covered the string and string manipulation where I told you that how you can extract a single string, how you can work on strings and all. So we'll discuss few more things within strings, and then we will move to the next topic, which is list and list manipulation. Now, in simple, if I wanted to repeat— if I wanted to repeat something, let's say if I'm going— I wanted to repeat OK. Multiple times. So I can write it like, within this I want repeat to be 10 times. Simple. Without using loops, I am able to do it just with the operator. So I am basically printing this particular string 10 times. So this is applicable with the multiplication symbol. You can repeat any string using asterisk operator, which will basically repeat whatever text you have written. The number of times you are going to mention. So just like I have written 10, so it will repeat it 10 times. If you want space, you can add it there itself in the string itself, and then you will get the same results— same in the result. Okay, so this is how you can simply repeat any string. Now let's say I wanted to check how many letters are there in this particular string. When you wanted to check the length of the string, then you have to use the method called len. Len is the built-in method. So if you are going to say len repeat, then it is going to display the number of letters or number of characters it has, and it also includes the spaces. So if you have any white space in your string, that too will be counted in the result. So len function is used to check it. Now there are some more types of inbuilt function. Just like if I wanted to check whether this particular string is a digit, if it is a digit, then I have to use the function like repeat.is. Now I have written is. Now you can see there comes a list of all the inbuilt functions available. So isalpha, isdecimal, isdigit, This is ASCII code. Okay, so if you wanted to convert, if you wanted to check whether your string is in the lowercase, whether your string is a digit, or whether your string is an identifier, whether your string is printable. So everything related to string, when you wanted to check it, then you can use these functions. You have to start with is, and when you will write is, you will automatically get multiple values. So suppose I'm checking is_numeric. So as I said that this is built-in function, so with built-in function you have to use these brackets and then you have to run this. It will give the results in the true and false Boolean values only. Now we'll talk about string formatting. So we have discussed about one of the string formatting in the program that we were writing about the calculator, right? So that was— you have to write f and within quotation whatever you are going to write. Let's say, I am printing a variable value, a value of variable. Okay, so anywhere in this sentence, if you wanted to write down the value from the variable, then you have to use the curly brackets and you have to mention the variable name anywhere you want. You have to use this method. This is one way of printing it without separating. Another way to print is you just have to use this brackets and you have to keep on adding it up. So it goes like, let's say I'm going to create name is equal to Python, age let's say 20. Okay, simply have created two variables. Now using the different formatting method within string, my name is— sorry— and my age is .format name, comma, age. So what am I using the wrong brackets? So we are using curly bracket. Okay, so here we are going to follow the method of argument. Okay, so if I'm going to say— let me just try this. Yeah, so we are going to follow basically the argument method. So over here I am assigning the variable in the same line and I am able to print the values from the same line. Okay, so this is called format function. Using format method, you can print something like this as well. Okay, now our next topic is related to list. So list is the main part of Python. Basically, using list, you can do multiple things because list is mutable. When it comes to mutable, it means you can change the item from the list. So let's say I have created num1 as number as a list is equal to— I'm just creating simple list. Now if I wanted to change anything from the list, I can change it. Even if I wanted to store multiple different types of data, so let's say data is equal to 1, 1 comma, okay, com, 98.5, comma, let's say true. So basically now I have created a variable data where I am able to store multiple data types within a list. So list can take multiple data type and list can be created like this. Now we will talk that if you— I wanted to— we have already discussed this, I guess, that how we can access any element from the list. When you wanted to access any element, you have to just write down the name of the list and then open a bracket and then write down the indexing. So if I want the first item, I have to just say 0 and it will print the first item for me. Now let's say our first item is 1. I wanted to replace 1 with, let's say, 99. So how I can do that? You have to use the indexing for the same. So you have to say data bracket 0 because 1 is at the 0th position and I want 99 at that place. Now I have replaced it. If I'm going to just print data, I will get 99 instead of 1. So over here, if I take you back, I have created this data variable where I have 1, but here where I have printed it, I am getting 99 because I have assigned a new value to the data list first index. So this is how you can modify any element. If I wanted to change any element, you just have to use indexing and you can do that. Any question up till this? Is this clear to everyone? Okay, so this is how we are trying to modify it. Now let's say I wanted to add more values in my list. I have created list, but the items are very limited. Now I wanted to add more items in it. So when you wanted to add more item, first way, which is easy way, is go back to that cell. Take your cursor there, keep adding commas, and then rerun that particular cell. But this is not the correct way of doing it. You have to do what? You have to basically use some functions. So there are 3 different functions for adding the elements. First function is called append. Second function is called extend. Third function is called insert. So there are 3 functions: append, extend, and the third one is insert. Okay, so append means adding something to the end of the list. When you want to add something to the end of the list, then you have to use append function. Append will help you add a single element at the end of the list or to the end of the list. Extend, which is the second function, will help you add multiple items— 2, 3, 4, 5, 6, 10, 20, 30— n number of items you can add with the extend. Third is insert. When you wanted to add any element at particular index, then you have to use the insert. Is this clear to everyone?
This one, you know, get any example?
Yes. Now let's say within the same list we will work on, okay? So data is our list. Now I wanted to add one value in it, so I have to write data.append bracket open and you will mention the value. Let's say I wanted to add 65. For now I'm just going to remove both the values and I'm going to run this. Now if I will print data, I am getting 65, which is the end value. Now if I'm going to again say data.append, and I'm going to add, let's say, 35, and I'm going to run this. Now I will just rerun this data1, and you can see now I have the last value as 35. So append function we are using to add something to the end of the list. Second is extend. When it comes to extend, you have to create a new list. Okay, so let's say my new list is data1 is equal to, and I'm going to say like, I will write multiple values in this. Okay, so this is one way of extending any one list with the another one. Okay, so for example, if I'm saying data.extend— over here I can directly mention the variable name. So data.extend with the data1. I think I forgot to run that. So yeah, I need to execute that cell. Where it went. Yeah, so now if I'm saying data.extend, it means I am adding more items in my data variable, data list, and I am extending it with the new list. Now if I'm going to say print data, it will print all the values including the value which were there in the list data1. You can directly write down the value. You need not to every time create new list separately in the different variable. You open a bracket— we know that list has to be created within square brackets, so open a square bracket and then mention the values that you wanted to insert. So just like I am doing, you have to do the same. So now I have created one more list which I am going to extend within the main list, which is data. And now if I will run this data print one, you can see we have ABCD also added in the same list. Any question in this?
Extend, it will also add the end of the list.
Yes, everything will be added to the end of the list because we are not specifying anything, because we are trying to add multiple items, right? A, B, C, D. If I want A to be added at particular index, then I have to specify that index as well, right? So extend will allow you to add multiple items but to the end of the list. Append also will go with the same, like to the end of the list, but with the append you can add single value. With extend you can add multiple values. When it comes to the point that you wanted to add something at the particular index then you have to use insert method. Okay, with the insert you can— you have to specify that index value, that where you exactly want that. Okay, so, okay, data.insert. Now when I open the bracket, it shows index, which is going to be your support index, and then you have to mention any object, anything it could be. So first you have to mention the index value. So let's say I wanted to replace something instead of OK, so which is index number 1. Now what do you want at that, that index 1? What, what is the value that you're looking for? So let's say I want coding. And now if I will run, it will change the value. Now you can see we have OK, the place. Wait, I have to print it so that we can— yeah, but wait, we have okay also there. Wait. Yeah, so now you can see everything has been shifted. Anyone, any question in this? No questions. Shall we move forward? To the next topic. So this is the way you can add elements. So obviously when we are adding something, we wanted to delete certain things from the list as well. So again, when you wanted to remove elements, you have again 3 options. First is your pop, second is remove, and third is clear. Pop, remove, clear— these 3 functions will allow you remove items. And then clear. First, let's talk about pop. So pop means when you wanted to delete single item, when you wanted to remove and return the item from, uh, the specified index. Like, as let's say I wanted to remove something which is at the second index, so I have to write pop 2. Okay, so it goes with the index. It doesn't go with the value, it goes with the index. So you have to mention the index value. But when you don't know the index, you wanted to remove value itself, you know the value that you wanted to remove, but you don't know that what is the index of that value. So you can use remove function. Remove will remove that particular value which you are going to mention, whether it is a numeric value or the string value, you can mention it and you can remove it. Another thing to note within list is that list can hold duplicate values as well. So within this, there can be duplicate values. Now when you are applying the remove function, remove will only remove the first occurrence of that value. For example, let's say within this list, do we have anything which is repeated? Okay, let's first insert something repeatedly, like we want some values. Okay, so I'm going to add 99, 98, let's say 29 and 39. Okay, I'm going to extend it and I will just run this particular cell. Now you can see we have 4 more values over here. So duplicates are allowed in list. Now when I am going to use these functions. I will show you that how your remove function will behave with the multiple duplicate values. First we'll see this pop one. So when you're writing data.pop, so over here I have to mention the index value. Let's say I wanted to remove OK, so which is at second index. So I will say 2, and then it will remove OK. And what has been removed, it will show you that in the results. Now if I'm going to run this, you can say that— you can see that we don't have OK in our list now. Now the second one is data.remove. Within remove, we have to mention the value. So let's say I wanted to remove 99. So now if I will run, it will remove 99, but only the first occurrence. You can see the first time wherever 99 is found, it has been removed and the function stopped working there itself. But we still have 99 over here. If you wanted to remove this also, then you have to run the cell again. Got it? Any question in this? Now, when you wanted to clear everything, you want the list but you don't want the existing data of that particular list to be there. You want data variable to be there and you want the data variable to act as a list, but you don't want the values to be there. So at that time you have to use the clear function. So you just have to say, like, I'm not going to remove something from the data variable, I will remove from the data1. So you have to say data1.clear. Now if I'm going to print data1, I will get nothing because no values are there. I'm not getting error, I'm not getting the data1 doesn't exist, I'm not getting name error, I am also not getting that The data type is changed, nothing. List exists but data doesn't. So with the clear function, you can make your list empty. Now there are some cases where you wanted to make your list in the ascending order or in descending order. So when it comes to ascending order, you have to use some inbuilt function. So one of the function to sort the list in the ascending order is the sort function. So simply say data.sort. This data dot between float and string. Yeah, so basically I have created one list where we have multiple data types, right? So with the alphabetical one, I can't rearrange it in the ascending order. And if you have a list with the numbers only, then you can apply, uh, you can use this sort function. So let's say I am creating xy is equal to— so I have numbers now. Now I am going to apply this sort function, xy.sort, and it will be sorted. And now if I'm going to print the—
sorting will work from a similar type of data type.
Yes, so basically like in data structures, now when you will learn about data structure and algorithm one part, so there itself sometimes we need the data which is sorted only. If the data is sorted, the algorithm can be applied easily. So there you will use it a lot, okay, to sort it. It would be— it is basically a mandatory step. So you have to use the sort function which will help you sort it. So now you can see the list has been sorted in the ascending order. You can also reverse the list with the reverse function, obviously. So we have the direct reverse function to reverse it. So, uh, xy.reverse. Uh, where it went. So when we are writing list.reverse, shouldn't give None type. Let me just see. Let me— yeah, now it got reversed. Okay, so directly writing this reverse function and then printing it. Now let's say you wanted to check the index value of any— where any value you wanted to check for the index value. Let's say for example you wanted to check for 8, that what is the index value of 8 in this list. So you have to use the inbuilt function again for this. So we have a inbuilt function, .index. But this will also go with the first occurrence of that value. So you have to say, let's say, xy.index of 8. So it shows 4— 0, 1, 2, 3, 4. So index value of 8 is 4. So when you are unaware about the indexes, you go with this index method. Any question in this? When you wanted to count the occurrence of, let's say, any value— so we will go back to the previous data that we have, which is data1. Sorry, data1. We will take just data, data. And over here we have some duplicate values, right? So I will just try to check the repetition of any value. So let's say that how many times this value exists. So you have to use COUNT function. Now within this COUNT function you have to mention the value that you are looking for. So let's say we wanted to check for 29. So you can see 29 exists 2 times in your list. So this is how you can count the number of times it exists in the list. Any question in this? Also to note in the list is that you can create multiple lists within a list as well. Then that we call as a nested list. Just like we have nested if, same way we can also create nested list. So nested list acts as a matrix. So let's say I'm writing d— dt is equal to square bracket. Here you can create one list, let's say 1, 2, 3, comma. I'm creating another list which is 4, 5, 6. Again creating one more list, 7, 8, 9. So now my dt is the variable which is a nested list. If I'm going to say print dt, so this is how your dt will be printed in the results. Out of this method, like if you will create a nested list which is going to also act as a rows and columns one, okay, because this is the first one, this is the second list, and this is the third list. Now if you wanted to apply the slicing method on this, or you wanted to get any certain specific value from the list, then you have to follow the method with the two square brackets. When it comes to two square bracket means— so if I'm writing dt square bracket, you want something from the first list, go with this, open a bracket, and you want something from the second index, then you have to again open a bracket, and then you have to mention the value. So you can see this is the first list because every time indexing starts with 0, and I am looking forward for the second index. So 0, 1, and this is 2. So we are getting 6 as the value in the output. This is the list number and this is the value from that— the index value from that particular list. If you want 9 to be printed, so you will write 2 because this is your second list. 0, 1, 2— this is your second list. So you will write 2 and then you want 9. So it starts from 0, 0, 1, 2, and 9 is the value which will be printed. So this is the concept of nested list. Any question, anyone in this?
Mother said that how I can separate the So you can come up again. How I can separate 3 list?
Can you type in the chat? You wanted to separate what exactly? These list you wanted to separate? Yeah, when you want to separate a list, simply you go with this first square bracket. Okay, so dt and you run it. This is the first one, the list, the middle one. If you want the first one, you go with the 0. You can also store like, let's say, dt1 is equal to dt square 0, dt2 is equal to dt square 1, dt3 is equal to dt square 2. Now you have 3 different variables with the 3 lists because in the nested list you— we have created 3 different lists within single list. So I have created 3 different variables. Now if you will say print bracket dt3, then it will print the last one. If I will say dt2, then it will print the middle one. If I'm going to say dt1, then it will print the first one. Got it? Now let's say, for example, it, it is going to act as a rows and columns, right? As I said. So let's get back over here. So within this, it goes like this is your first row, this is going to be your second row, and this is going to be your third row. So 1, 2, 3 is your first row, 4, 5, 6, and then we have 7, 8, and 9. These are the 3 different lists and they are going to act as rows and columns. Now over here, these are the rows: 1, 2, 3. We have 3 rows. Same, we have 3 columns as well. This is your first column, this is your second column, and this is your third column. Now let's say I want to print these three values together. How I can— when you wanted to go with the multiple list, then you have to use the concept of loops. Loops can help you get these three values. Now how to do that? Is the, is the question clear to everyone? Yeah, yeah. So now from the list you want— from each and every list you want the second index value, right? This is 0, 1, 2. This is second index. So you want to go with the second index. So you have to write down for— let's say for row in variable is dt. Print row square bracket 2. Correct. Row is the local variable over here and we are printing it. And now you have to do what? You have to create a bunch of it. So you have— you can even write it in the single line as well. So the single line statement is like, yeah, let's say I'm writing column bracket row, square bracket, 2, for row in dt is equal to. So this is simple one line, one line code, okay? For row in dt, and this is the value that I'm looking forward for, and the entire data will be stored in the column variable. And later on I can print the value of column, and this is how it will be printed. So if you are going to write it in this way, like this, uh, simp— the basic way of defining a loop, then your output will be 3, 6, 9. Output is correct, but we want the output to be in the square bracket. To act as a list. When you are extracting a column directly and you want it in this list format, then you have to write it. Then you can use this method also, like we are directly assigning the value to the variable. Got it? Yeah. Hello, is this clear to everyone? Yeah, so when you're targeting an individual value, then you can use this method. If you wanted to switch, like you want first from first one, you want second value from third one, you want the third— the next list, you want the first value— when you are going with the different value, then the logic will be different. But if you are targeting the same index number from 3 different lists, then you can use this method. Okay, now can anyone of you tell me that what are the 3 key features about list in Python? Just what you have understood so far from the list concept.
Lists are mutable. Okay, we can easily add, update, and delete in that. Okay, we can do indexing and slicing also in list.
Correct.
And is it used to store multiple data types?
Correct. So list can store multiple values in the single variable. This is what exact definition of list is. Second thing is list can hold any data type, any 4 built-in data type you can mention within list. Third thing is list is mutable, so which means that you can update the list anytime you wanted to. You can also use slicing method Oh, one more thing is that it allows duplicate, so you can add duplicate values as well. If I ask you, uh, you want— you have to check basically the length of the list, that how many items are there in the list, then which function do we have to use?
Len function, correct.
So you have to use len function and you have to apply to the variable of the list and you will get it. Can I create a list with a constructor?
Yes, ma'am.
What exactly the constructor is?
Uh, uh, like list, it's a name of a particular built-in function, we can say.
Yes. Correct, correct. Can I add multiple items in the list at once, like with the one line of code? Is it possible to do that?
Yes, ma'am.
Yes, we can. Correct. So this was all about list. So we have 3 more data types. One is tuple, then we have set, and then we have dictionary.
Okay.
Tuples, set, and dictionary. These data types we will discuss in the next session. Okay, so before that, just try to practice everything which we have discussed in today's session. If you have any doubt, you can drop it in the Discord server that we have. Or else on the WhatsApp.










