Day-3 Python + MYQL FREE SESsions

Akansha Yadav teaches Python fundamentals for data analysis: converting user input to numbers, string methods like lower() and replace(), and indexing and slicing with positive and negative indices. The class works through extracting keywords from reviews and parsing timestamps.
Chapters
Hello everyone, how are you?
Good, ma'am. How are you?
All good.
How's everything going on, ma'am?
What's the topic for today's class?
Yeah, first we will learn about string manipulation, that how exactly we need to do the string manipulation, and then we will move on to the conditional statements.
Uh, ma'am, actually I have a doubt in yesterday's class. Like you said, uh, that tuple, uh, means conversion, you told last class. So how to convert the tuple to list?
I think we discussed that. Let me just share my screen and then we'll—
okay, yeah, yeah.
Is my screen visible to you now?
Yes, ma'am.
Okay, so here's how we have converted a list to— like, we have created a list and tuple, right?
Yes, ma'am.
Here we have converted. So this was list earlier, but then we converted it into tuple. Okay, okay, yeah, got it, ma'am. Yeah, any other question?
No, ma'am.
All right, also the session link, right? So I have sent you a mail yesterday, same session link you have to join regularly, okay? So what I would suggest you is go to that mail and then add it to the calendar. When you will add it to the calendar, then before 30 minutes of the session you will keep getting the notification that you have a meeting in 30 minutes.
Okay, yeah, I did it.
Yeah, all right, so let's begin with the topic. Before we begin, can anyone of you let me know like what is the exact like use case of Tuple. Why do we use tuple?
Uh, tuple is immutable. Like, uh, if we enter any value, we can't change it.
Okay. And when do we use type? Do you remember type function?
Uh, yes, ma'am. Uh, to see the— means what type of list or tuple it is, to see the—
to print the— like what type of, uh, like data type there, we will use that.
Correct. So type is used to check the data type of a variable.
Right?
Yes, ma'am.
What is sequence data type?
Range. Oh, like list, tuples are the sequence data type, like one after another, like values will be there, or like, uh, like What would— like same kind of values will be there in that is a—
okay, someone has texted also. Let me just check it to know the class. Okay. So let's go through each one of them. Like, sequence data type is basically when you wanted to store multiple values at once in a single variable. Okay, like you can use list, tuple, you can go with the range, right? Oh, okay, okay. So in the last session, we have discussed about these variables. These data types in Python basically, and we have implemented it with the different variables as well, right? Like a, fruit, num— all these were the variables in which we have created all these data types, and then we are— then we stored it, right? So now we will learn the next topic is the input and the output operation. Now let's talk about input and output operation. Now, up till now I have shown you that if you wanted to display something, then you have to use print statement. And the way you have to write down the print statement is you have to write it in the lowercase, and then in the brackets you have to mention what you wanted to display. Now, what if I want the user to enter some values? Let's say I want the user to enter the value of number 1. So let's say my variable name is number_1 is equal to— now I have to take input from the user. It means I want user to type something and that value should be used for any multiplication or any calculation basically. So that value we can use. So to do that, this is called input operation. So you have to use input function for this. So we have input function, you have to open the bracket, and then if you are going to do print number_1. And if you will run this, so now at the top, are you able to see this section? Yeah, the one which I have marked in red color. Are you able to see this section?
Yes, ma'am.
So this is a space where I can type the number and that particular value which I am going to store, like with whatever I wanted to store, I can directly write it over here and that particular value will be stored in the num1 variable. You can see I haven't typed anything because I'm just showcasing you with these red color lines. So the cell is still running, like it says 45 seconds and Okay, so now I have to write down the value over here. So I'm going to take my cursor there and I'm writing 52 and I entered. So then I have printed and it's displaying the same value which I tried to write. Suppose in the same— in the value which I am going to enter, I wanted to add something. So I'm going to add 2 in it. Okay, this will give us an error. Okay, so focus on that error as well. So I have just executed it and I'm writing 52. Now it shows TypeError because in input function, whichever value you are trying to write will be stored in the string data type. Which data type?
String data type.
String data type. Okay, so you cannot perform a direct calculation on that on that particular value. If you wanted to do some calculation, you have to convert that value into integer, then only you can do it. Because numeric— on numeric value only we can do some multiplication, addition, or anything similar to this. So if you wanted to convert it into certain things like any different variable, any different data type, then you have to mention it. Just like I'm writing int input. So int input means I am trying to convert string data type into integer, and then I will be able to do addition. So I'm again writing 52 and I'm going to press Enter. Now this time it's showing 54 because I have successfully converted string to integer. Got it? Did you get it? Am I audible to you all?
Yes.
So is it clear to everyone?
Yes, ma'am.
So simply, when we are writing the input function, whichever value you are going to enter, that value will be by default stored in string data type. Okay, when it is stored in string data type, so basically when I say that you have to add 1 1, what will be the output of this?
Double 1.
1, double 1, right? This will be the addition, will be this, which 2 will be in the string data type only. Okay, so basically what I'm trying to say is that you cannot do multiplications on sequence data type, or you cannot do, uh, the multiplications or any calculation on string data type. For making some calculation in the numeric You have to go with the numeric data type only. Any question in this? No, ma'am. Now I'm just giving you 2 minutes. You have to execute this on your system and you have to tell me which type of error you are going to get— you are getting, basically. So can you guys quickly test it on your screen? The same thing, taking an input, converting string to integer.
Ma'am, we have to do that 1 1 1.
No, no, not this one. I'm talking about the one—
this one.
Okay.
Once done, just let me know.
Uh-oh.
Oh, just done. Are you getting any error?
No, ma'am.
Okay, let's just see. Others' result as well, and then we'll continue.
You're done.
Done others?
Yes, yes.
No errors?
No errors. All right.
Okay, so when I am going to run this cell, basically, so at the top, are you able to see it says that press Enter to confirm your input or Escape to cancel? So if you wanted to change this text, right? So press Enter to confirm your input or Escape to cancel. You have to utilize this bracket. This bracket will help you change this text. Suppose I am looking forward for a number from the user, so I can use the quotation. I can say that enter any number and I will run this. Wait, let me just— and then I will just rerun it. Now you can see it says that enter any number. If I'm going to run the same code in this .py file, then let me just complete this, and then I will run this and will show you the output in the terminal. So this is how it will come up. Enter any number. Now I have to enter any number. So if I'm entering 10, it will give me 12. Any question in this?
No, ma'am.
So simply, when you wanted to take input, you have to utilize this input function. And then you can take the input. And make sure before taking input itself, you are going to convert it from string data type to any other data type. Suppose you wanted to get the number which is float value, then you have to convert it into float. So simply you just take the same code, instead of int you have to write float, and if you will run it, it will accept the number in float, and it will give you the output in float as well.
Okay.
Now let's talk about string manipulation. The next topic which is important to display the data is string manipulation. String manipulation means the way you combine the strings, the way you try to print multi-line strings, and so on. Okay, so the string manipulation is important in data analysis and data science as well. So string manipulation is, let's say I'm writing s is equal to hello world. When I'm writing s is equal to hello world, it means I have just written a simple statement. If I am writing s is equal to Python programming, then it means I have just written a simple two words and I have stored it in the variable s. Now let's change this variable. B is equal to Python program. Now this is the entire string. Now if I am creating like I already have created two strings, s and b, and you wanted to print both the strings, then you can simply say s plus b, and in the output you will get the combination of both. Wait, I have to rerun this program. Okay, so now it says hello world Python programming. When I'm saying hello world Python programming, there should be a space because I'm trying to print two different strings together. So one way to add a space is you just go to the second string and then add a one tab space over here, but this won't look nice. So what you can do is you can utilize this, that particular print statement, and you can use the quotation, and then you can add one more plus symbol and adding a space there. Now in the output you will get the space. You have to make sure, like, if I'm writing this comma, right, as I told you that you have to use commas as a separator, so that too will work. But as I am working with this, you know, when trying to merge multiple things, when I'm trying to concatenate basically, then at that time you can also use this plus symbol. Okay, so s plus space plus b. Okay, so this is how you can combine the two different variable data. Okay. Any question in this? No questions.
I'm rather giving the double quotation. We can add a comma for the space thing.
It will work if you're saying that you should remove everything and just like this. Yeah, yeah, that will work, but it depends from environment to environment. Okay, okay, yeah, yeah, just try this out and just give me a minute. Yes, Shekhar, if you have more text, then you can keep on adding plus plus and you, you can combine it basically. Okay, now when you wanted to store multiple lines like a paragraph or something, then you can simply use the triple quotes. Like if I'm using this and then I can print brackets a, then you will be able to store multiple line text as well. You can see the way that I have stored, you are getting it in the output as well. So to store multi-lines, you can use single quotes, single, uh, 3 times single quotes, or 3 times double quotes. Okay, this is the same way like you do in the commenting, like if you wanted to store multiple values, when you wanted to store your notes in multiple lines, then you use these quotations, right? So same way, same structure is followed over here as well in strings. When you wanted to store paragraph or something in a variable. Okay, now next thing is to use string as an array. When I say to use string as an array, it means if I wanted to print an individual letter, then how to do that? Individual letter means, let's say Let's get back to the previous variable. Okay, let's take an example of this variable b, which is with b is equal to Python programming. Now I wanted to print first p out of it. So how to do that? You have to use slicing method. Okay, so b is the variable. From b variable, you want to print the first letter. So there comes a concept of indexing. Indexing means Uh, basically whenever any value or anything is stored in computer memory, there is an indexing, there is a memory allocation for each and everything. So in Python, or like basically in general, indexing begins with 0. So if I say you have to print first one, it means this is index, this is at index 0. So 0, 1, 2, 3, 4, 5, like this and so on. From left-hand side to right-hand side, if you are going to count it, then it is 0, 1, 2, 3, 4, 5. Like this it will go. But if you are going from right-hand side to left-hand side, then it will be -1, -2, -3, -4, because there is nothing like -0. So from left to right it is 0, 1, 2, 3, 4, 5. But from right to left, if you wanted to get something, you have to use the Negative indexing, which is -1, -2, -3, and so on. Any question in this? Yes, spaces are also counted. Like after Python, I have one tab space, so this too will be counted. Okay. Yeah, anyone, any question in this? Now when you are writing print statement with the variable b, then in— within the square bracket you have to mention the indexing. So as I said, you have to start from 0. So if you wanted to print the first p, then you have to write 0 like this: b square bracket 0. Okay, if you wanted to print some other letters from the string, then you have to do the same thing. So you will say p— b bracket begins. Let's say I wanted to print h of Python, so 0, 1, 2, 3. So I am looking forward for 3. So now I'm getting h. This is how you can print single letter from the string itself. But now let's say I wanted to print multiple letters, multiple letters, like out of entire string I wanted to print 3 or 4 letters. Then at that time you have to again follow the same indexing method, this slicing method itself. So slicing method says that within square bracket you have to mention 2 values. First will be your starting value, second will be your ending value. When it comes to starting value, so let's say I wanted to print this Python only up till n. So p is at 0, 1, 2, 3, 4, 5. n is at 5th position, right? So you will write it like print b starting from 0 to 5th. But if you will write fifth, right, if you will write 5, then you won't get n. So every time when you are writing your starting and ending both values, then you have to add 1 in it, 5 1. So then only the n will come up for you. Otherwise n won't be calculated because the last value is— every time when you are writing the last value, you have to use 1 greater than the value that you are expecting it to print. When you are expecting it up till n, then you add 1 in it, like 6. If you want space also to be printed, then you have to write 7 instead of 6. So you just remove this, you write 7, and you run it. Now this time, if I'm going to select it, you will observe that I have the space also. Now if I'm trying to select more, then it won't be because the output is only this much. Any question? Yeah, you can directly write 6. I'm just showcasing you that you every time have to add 1 in it. If you will add 1 in it, then only you will get the complete word, okay? Because— but that particular value, like 6 I am writing, then 6 won't be included, okay? It's like start index, ending index, and in between you have to separate them with the colon. Any question in this? I can write the same thing without writing the first index. Whenever you are sure that you wanted to go from the beginning, when it's— it is from beginning, like from 0 itself, then you need not to mention 0 because it is already understood by the slicing method that you— that basically slicing method has to start from the beginning. So if you are going to skip the 0 also, then also it won't affect it. It will still print the same output for you. Now let's say you wanted to print from somehow— let's say where it went. Yeah, you wanted to print programming, but it's a bit difficult to, you know, count each and every letter and go up to last and then print the— then enter the last value. So instead, you— if you want complete string up till last, then you simply have to write down the starting value. So you can say print b square bracket, and here you can mention, let's say I wanted to start from 8 and go up till the last one. So now we are getting instead of P, because P is the 7th one, so you have to write 7, and then you will get the complete programming printed for you. And you can notice that I haven't mentioned the last value. Any question in this?
No, ma'am.
Is this clear to you all? Now let's take an example. Let's say you are working on a data analysis project, and on that data analysis project, that data which you are using for making an analysis is of products. So obviously products have product description, or maybe products have the feedback, or, uh, a section where you have a text. So when you have to deal with that text, you have to use the slicing method. Same, same slicing method you have to use with the libraries as well. So you have to make sure that you have a good understanding of the slicing. Okay, now let's talk about negative indexing. So whenever you wanted to print something from the last, so you have to follow the negative indexing. B square bracket, let's say I'm writing -1. When I'm writing B square bracket -1, then you will get G as in the output. Print B square bracket -1 to, let's say, -10, then this is not the accurate way of writing the negative indexing. If I ask you a question, which one is greater, like -1 or -10? Which one is greater? You remember that exercise? -1 is greater, right? As someone said. So every time you have to go from the lower value to higher value. You cannot go from higher value to lower value, that is not possible. So you have to invert it. Now you are getting something in the output, right? Because you every time have to go from lower value to the higher value, because this is what slicing can understand. When it comes to negative indexing, this concept is important that you go from lower value to higher value. So whichever value is lower, like -10 is the lower value and -1 has a greater value, then -10 will come up first and then -1. If I am writing -11, then you will get this complete one. Now let's say I wanted to— I want you to think for a logic that I wanted to invert this entire string. Invert means basically you have to flip it. This is the string, so in output I'm expecting you to get G-N-I double M-A-R-G-O-R-P. And same with the Python as well, like this should be the output. Now you have to think for a logic, like what exactly we need to do. Or which concept, or what exactly we need to do this?
We can use reverse spring.
Can you write it in the chat if you know any way to do it? Okay, I think someone has texted something. I think those who were there in the previous batch texted the correct answer. So there comes a concept of skip— how many values you want to skip, basically. So when I am writing print bracket begins b square bracket, and you are writing your starting value and the last value, let's say, is any value, let's say 0 to 10. 0 to 10. So when you are writing 0 to 10, now you can also skip some letters. Skip means it should be in the pattern, like you wanted to skip one word after, like you wanted to go with the even numbers, so you wanted to go with the even indexing, then you can skip the odd one, okay? So that too is possible. If I'm going to just print this 0 to 10, it means you are getting P-Y-T-H-O-N and then space and then P-R-O. If you wanted to skip, let's say you wanted to skip Y-T— sorry, Y-H-N, and then you wanted to skip P and O. So it means I'm trying to skip the even one, so I have to mention that value and I can go this If I'm— by one is by default, basically one is by default. Every time when you are writing just this much, it means one. It means you are just going from one by one, one by one. But now I wanted to go two by one. So now you can see Y is not there, H is not there, N is not there, which is the even indexing. And I have skipped it with this correct syntax, which is starting, ending, and the number of letters that you— the numbers you wanted to skip. Any question in this? Yeah, anyone, any questions? So this is the concept of skipping any, any number of letters. When you wanted to reverse it, you simply have to write it down like you want the entire string to be reversed, then write down the print statement. As I said, you can skip the beginning and the ending as well. So you just have to add a colon because that is the syntax. So this is starting number, this is ending number, and there comes a skip. So starting and ending you can skip. And the last one, which is the skip one, that how many, uh, like, like you, how do you want it to, you know, be in the output, like flipped, or you wanted to skip what exactly. So at that time At that place you have to write down -1, and then everything will be in the reverse order. So this is the correct way to reverse any string. Now there are some inbuilt function which, which you can apply on your string to modify it. Let's say the product description is in the lowercase, but your model which you have created, or the functions which you have created, doesn't accept that. So what you can do is simply you can convert it in the uppercase or lowercase according to the need. So you write print b.lower, which is already in lower. So this is the output. Now you can see P is in the lower and another programming P is also in the lowercase. So if you are writing the same thing, then it will be basically in the uppercase. So just like I have written b.lower, you have to write b.upper, and this will convert it— convert your entire screen, uh, string in the uppercase. If suppose you wanted to remove any whitespace that you have. Let's say I'm just going to make modification in the string. I'm going to add a whitespace in the beginning and the ending. That is, yeah. So let's say I've added these whitespaces and I just executed it. Now I wanted to remove these Whitespaces. If I'm going to convert it in the uppercase also, you can see there are— there is a whitespace in the output. So sometimes to deal with this, whitespaces are not good for the analysis because that is going to basically fluctuate your analysis. So better is you just strip it. Strip function can remove it from the beginning and the ending, not from the middle of the text. Okay, you have to note it that when you are writing the strip function b.strip, then this function can only clear the starting and the ending space. You can see now if I've selected it, there is no space in the beginning and the ending. So strip function can only remove any whitespace from the beginning or the ending, not from the between one. Okay, now let's say you wanted to replace something within text. You wanted to replace, let's say, instead of P, you wanted to make it J. So you have to use a replace function. You just simply write print a.replace brackets. What do you wanted to replace? I wanted to replace from P and then I wanted to change it to J. Now you can see wherever it is like this— sorry, I have written the wrong variable. It should be P. Now instead of P in your string, you have J. So whatever you wanted to replace, you can replace it with this. Any question in this?
Nope.
So there are many built-in function which will help you to modify your string however you wanted to. Okay, these are just basic examples, like if you wanted to replace certain things, if you wanted to get a subset of your string, or if you wanted to, uh, get it in the reverse order or anything similar to this, you can use this slicing methods and you can use this built-in methods. Okay, so for today what you have to do is you have to practice taking an input value— input from the user, and then you have to convert it from one data type to another data type. After converting it from one data type to another data type, you have to try to do the string manipulation as well, or we call it string concatenation. When we are joining the strings, we call it string concatenation. When you are trying to make some modification, it means we are trying to do, manipulation. So string manipulation and string concatenation, which we have covered today, you have to practice these.
Okay, ma'am? Yes, ma'am, I want to ask you, indexing or slicing there, real-time scenarios.
So every time you don't need the entire disk, let's say on a product, this is any XYZ product on Amazon, okay? Someone has posted a review, okay? To just check whether that review is positive or negative, what you can do is obviously if someone has posted a review, then maybe there are certain like— you can create a list like good, good product or excellent product, I just found it amazing and all. There are certain keywords, right? So you can go and find it. Okay, after finding it, obviously to present an analysis you have to make it meaningful by giving some proof, right? So at that time you can split your entire string and then you can present it. Got it?
Uh, no, I, I want to know about this slicing and indexing.
Indexing means like it is useful everywhere, like when you are making the projects as well, right? To understand where that particular letter or that particular— like, how would you know that particular function is behaving correct, is giving you the correct output? Without indexing, that is not possible. Okay, okay.
Yes, ma'am.
And another was, I think, slicing, right?
Slicing, slicing.
Yes.
Yeah, so slicing, as I told you, that when you wanted to split it and you wanted to get the exact word and you wanted to know indexing, then at that time it is important. Yes, others, any question?
No.
You can also say that if you wanted to, you know, check for, let's say you wanted to go for the file extension, there are multiple types of files, right? For extracting the file extension also, you can use this concept of slicing and indexing. Okay, at that time also it will help you. Also, if we talk about in terms of data, right, in the data analysis, yeah, you have ever worked with the data where you have the timestamps, right? When it comes to timestamp, when you have to— like, let's say, in generally in data, the date and time will be in the single column. So you have to split it basically, okay? Once you have split it, that particular column, let's say date and time, you have split it right now. In from that date time, you have to maybe extract the year. Maybe your manager has asked you to go for the year-by-year analysis or month-by-month analysis. Then at that time, you have to focus on month, not on date. So you have to extract the month from that, and for that purpose you can use slicing. Did you get it?
Yes, ma'am.
Yeah, all right then.
You provide any assignments or anything so that it will help us to practice?
So basically, once we will cover the concept of conditional statements, operators, right, then only I will provide the questions, right, to practice. Because up till now it is all basics. Like, I can say you that, okay, you create a variable or you create— you store some values, right? So that is all basics. So for now, you just try to get a hands-on practice of syntax.
Okay, okay.
Once you have the good, good understanding of syntax, that how to declare a variable, how to store values, how to convert it from one data type to another data type, just Don't think about the practical, uh, use case of like, okay, when it will be asked to convert it, right? No, in real time, whenever you are dealing with the data, let's say you have the file in the JSON and you have to convert it into HTML, then you have to do it, right? So there are many ways to convert one file to another file, like JSON to HTML also you can do with the Python. But maybe for making it— making your task easier, you can use the online websites as well. So there are multiple alternatives as well, but for now you just stay stick to the syntax. Once you have clear understanding, like by tomorrow, I think the next session basically, after that I will provide you some questions. So then those questions will make sense to you all, okay? Because at that time you will have the clear understanding that how certain things have to be defined in Python, okay?
Okay, ma'am.
All right guys, so we'll connect in the next session. Up till then, just practice all these concepts.
So tomorrow is the session or off?
Yeah, I think we will have the session tomorrow. It's for the regular.
Okay, okay. Thank you.
All right, bye-bye, take care.










