Day-6 Python + MYQL FREE Sessions

Akansha Yadav builds a Python calculator using previous concepts, then teaches loops. She covers for-loops with range and step parameters, while-loops for condition-driven repetition, break and continue statements, and when to use each type.
Chapters
Hello everyone, how are you all? Yeah, am I audible to you? Yeah, all good. So let's start with today's session. I'm going to just share my screen. Let me know once it is visible to you. Is it visible? So up till now we have covered 5 sessions, and in those 5 sessions we have covered these 5 topics. We discussed about syntax, structure, we discussed about variables, data types, input/output, we discussed about operators, different type of— types of operators that is available in Python. And along with that, in the yesterday's session we discussed about conditional statement. So now let's overview. If we wanted to create a calculator, then what you need to do. So I have already written a code for that, okay, so that we can discuss, uh, the next topic as well, which is loops, okay. So just to save time, I have already written code on my screen, so I will just explain you the code and the structure, okay. So let's first talk about the simple calculator. If you wanted to create a simple calculator in the professional way, then you can follow these steps. The first step is here that you are going to display a welcome message. So it can be anything according to your calculator type. You can modify it. Suppose you wanted to give your calculator a nickname, so you can simply say that welcome to, uh, like let's say any AI or any type of name you wanted to give it. So you have to replace it with the name that you're going to give it, and then you can simply display a welcome message. Now second step is to take number. From the user. So we are going to work as like as a basic calculator. So for calculation we need two numbers. So we are taking the numbers input with the float data type. Why we are taking float data type? Reason being like if someone wants to do calculation with the decimal numbers, so that will also go with the float, and they can also enter any integer value as well. So both can be you know, done with the float data type. So you have— we are taking number 1 and here we are taking number 2. Then next is to display some operations. Basically, we want to provide some options to the user. So I am going to provide an option saying that if you want addition, so you need not to type the entire addition word or you need not to use this addition symbol, you simply have to use 1, 2, 3, 4. So using different print statement, I have written the multiple messages like select the operation to perform, for one for addition, second for subtraction, multiplication, and division. And this is how I'm just displaying certain things. After that, I want to take input again from the user, but this time this input is going to be the choice of the user, that what exactly user wants to perform. User wants to perform addition, subtraction, multiplication, or division. So accordingly, user will enter 1, 2, 3, 4. So I have written enter the message corresponding do the operation. 1, 2, 3, 4. Accordingly, user will enter the number. So we are taking that number. We need not to convert that number into integer because we are not going to perform anything on this particular number because this is just a choice of the user. So every time you need not to convert it, only when it is required you have to do that. Then step 5 is that you have to perform the operation using the conditional statement. So it means in this case you have to use the operators, you have to use the conditional statement. So first is we are going to check whether the choice of the user is 1. If choice of the user is 1, it means according to our operational, like, the message that we have displayed, 1 means addition. So we are saying that result is equal to number 1 plus number 2, and then I am displaying print the result of the addition is this. I have used a different way of writing the print statement. You can write down the same print statement with the, uh, like the simple way that we have discussed already. Or else you can use this formatting method as well. When, when we are writing this f before the quotation begins, it means in this case you need not to separate your text with the commas. You need not to use commas anywhere. So you can see I have written the result of the subtraction is, and then within the curly bracket I have mentioned the variable, but I am not separating it, and even I am not writing it after quotation. When you wanted to make your— make it in one go within one single quotation, then you can follow this method. This method is useful when you are writing a long text. Along with that, you want the numbers or the values from the variable, then you can use this formatting method. So for this, you have to write f in the beginning before quotation begins, and then within quotation, you have to make sure whenever you wanted to call a variable, you have to call it within curly brackets. That's it. You can proceed with writing anything else also. If you wanted to write, you can Anything you want, you can write it after curly brackets as well. And as many variables you wanted to call, you can call within this. You just have to make sure whenever you are calling a variable, you have to use curly brackets and you have to write f in the beginning. Is this clear to everyone? Yes, ma'am. Um, For example, if we need to give two results, like why we are giving F? Anything formatting? F stands for format. Yeah, this is for format. Earlier, I will show you the previous print statement that we discussed. I think we were taking the values. Yeah. Over here we were writing print statement and then we have written floor division, and after floor division I closed that particular quotation and then I added a separator, then I have written the variable name. But in this case, every time if I'm going to write a long message, I have to add a comma, then I have to open a bracket, then I can write whatever I want. If I want the value of a variable again, then again I have to use a separator, then again separator, and then coming up with the message. So it is bit like, it's, it's going to take time, right? So to just to save your time, you can use this method which is formatting method where you just have to mention F before you open any quotation and then you can write whatever you want within the quotation itself. You just have to make sure you use the curly brackets. Got it? Okay, now the next— like, according to the choices I have made the operations, like for 1 it was supposed to be addition, for 2 it was supposed to be a subtraction, for 3 it was supposed to be multiplication, and for 4 it was supposed to be a division. Now in case of division, let's say the number 1 entered by the user was 10 and number 2 which is entered by the user is 0. So if you are dividing anything with 0, as I told you earlier also, you are going to encounter zero division error. So what I am doing over here is within elif I have written one more if saying that if choice is 4, which is division, okay, we can do division, but now we have to check whether your denominator is not 0. If your denominator is not 0, then only we will perform division. Otherwise, to avoid the error which is zero division error, we are going with the else statement. So here we are checking if number 2 is not equal to 0, then to perform division, number 1 divided by number 2, and then display the result. Else just press print error, division by 0 is not allowed. So this particular statement is basically helping us avoid that particular zero division error. Got it? Is this making sense to everyone? Simply, when you wanted to avoid certain errors and you are aware that at this stage you will get that error, then at that point you have to use these conditional statements to avoid that. Simply, when I am just taking that in, whenever the choice goes with the 4, okay, we can perform divisions, our program can perform division for you. But in this case, we have to check whether your denominator is not 0, because if denominator is 0, we will encounter zero division error. So to avoid that, I have written simply that it shouldn't be 0. If it is not 0, then simply perform division and display the result, else directly display that error Division by 0 is not allowed. Now let's say if the user has not entered 1, 2, 3, 4, user has entered something else, maybe 5 or any alphabetical character. So in that, that case, I have written else saying that invalid operation choice, which is invalid input. Please choose a valid operation. It means you have to just enter 1, 2, 3, 4. You can choose only out of these 4 numbers. Now I'm going to execute this. So it's asking for like you have to enter the first number. So let's say my first number is 50, and now I have to enter the second number. So let's say the second number is 2.0. Now it says that enter the corresponding to the operation 1, 2, 3, 4. Now where this, uh, like the, the values, like the message is coming up. So the message is coming up just below that tell where it went. Yeah, over here. Welcome to the simple Python calculator. Select the operation to perform, and here is the options for us. Now accordingly, we have to— so let's say I'm writing 3, so which is multiplication, so it will perform multiplication and will display the result. If I'm going to execute it again, if I'm saying, uh, first number is 10 and second number is 0, then If I'm saying 4, then in the output it's going to show— yeah, so error: division by 0 is not allowed. Got it? This is how you can simply create a basic calculator with the concepts that we have covered so far in the 5 sessions. Any question in this, or shall we move on to the next topic, which is loops in Python? Okay, so now the next topic is loop input. Now what is loop? Loop is basically when you want something to repeat. If I ask you that you have to display— you have to write hello 100 times, so writing it manually will take time, obviously. So we are going to use computers to make it happen. And in programming language, when you wanted to make repetition, when you wanted to iterate over something, you can use loops. So there are two things for which you can use loops. For first is that you can iterate over any sequence. Second is you can execute one block of code multiple times based on the conditions. So there are two use case of loop. And in Python, we have two types of loops. First is for loop, second is while loop. And loops are used for iteration and for executing a block of code multiple times. So this is the basic introduction to loop. Now let's talk about the loops like one by one. Like in Python, we have first one is the for loop. So for loop is used to iterate over sequences, any sequence it can be. Suppose you wanted to iterate over list. So for example, let's say I have Where is the cursor? Yeah, so list2 is equal to— let's say I have A, B, C, comma, D. Let's say simply I have created a list2 variable and I have stored 4 letters in it. If I'm going to print this, so if I'm going to just say print list 2, so it will display all the letters at once from the list. If I want something from the list, I can simply use the brackets and I can say that, okay, I want the first one. So it's giving me the first one. If I wanted to get the second one, I can say 1 because we are using the indexing concept over here, which is applicable in list and the strings. Same concept, everything is same. Indexing begins with 0, and you can use any numbers to display the corresponding item from the list. Now when we are saying list2 and this, if I want to display everything, like I wanted to display ABCD in different, different lines, basically I wanted to iterate over the list. Iteration means going through that particular thing one by one. So whenever we are going to use loops and when the terms comes iteration, it is going to iterate over each and every element. So this is element number 1, this is element number 2, this is element number 3, and this is element number 4. So there will be in total 3 to 4 iterations to print all the outputs. Okay, so how to write it exactly? First use case of for loop is that you can iterate over sequence. So the syntax is that you have to write for variable in sequence. Okay, so for variable in sequence. Now what is variable and what is sequence? Because for and in are two keywords in Python. For and in are two keywords which is reserved word in Python. So this is must, you have to write it, for and in. Now variable is a temporary variable just for a short period of time, just for this piece of code, you have to declare one temporary variable in this case. So it is going to be a temporary variable which is going to hold each item from the sequence for a short period of time just for making that iteration. That particular temporary variable will hold that item for some time, and after that, that value will be replaced with the new value. And sequence is nothing, just a list. Like we have created a list, so it can be anything. So sequence is just a list. So I'm going to replace the sequence with the list2, and let's say instead of writing a variable, I'm going to say item. For item in list2, print item. And if I'm going to run this, you can see A, B, C, D I'm getting in the different, different line. This is how you iterate over something. Now let's say I'm saying, um, ok is my variable and I am writing Python in it. Ok Python. Ok is equal to Python, which is my variable. For item 2 in ok, let's say. So item 2 is the temporary variable, ok is the variable in which I have stored Python. And now I am going to print item 2 from this. Now in this case, when we are going to iterate over a string, then you will get all the characters printed for you in the output. So I'm going to run this. You can see P-Y-T-H-O-N. I'm getting it in the new different, different lines. And this is what we call as an iteration. So basically right now, these block— the— this code is basically executing the code block for each item. How it is working? It's taking that value, like item 2 will first hold P, then Y, then T, and every time that previous value will be removed, new value will be stored in the temporary variable, and this will continue up till the last letter. And once the letters are done, then it will stop right there. Got it? Any question in this? No, ma'am. Now let's say I wanted to print some numbers. So you remember when we were discussing about data types, at that time I told you that there is a range data type as well. So you can use range data type over here. So when I'm saying for k in range 5, okay, so when I'm saying range in 5, you remember that we would— we were— when we were checking the type, it was showing from 0 to 5. Now I am going to print k. So you can see 0, 1, 2, 3, 4. You are getting first 5 numbers like from the beginning, obviously it's going to include 0, and 5 will not be included. So whichever number you will mention, that number will not be included, a number less than that will be there. So if I'm writing 6, I will get it up till 5. If I am writing 10, I will get it up till 9 because it's going to count 0 as well. So this is how you can use range also in this for loop. This is basically generating numbers from 0 to whatever number, the last number you're going to write. So range number— range function can basically take 3 inputs, right? You remember, it is same like slicing. So it goes with start, stop, step. So range function can take 3 values: start, stop, and step. Start means you have to mention the starting value. If you wanted to start from any other number, you don't want 0, 1, 2, 3, you want it to start from 5. So starting number will be 5. Now let's say you wanted to end it up at, uh, like at 10. So you can write range bracket open 5 comma 10. 5 is starting number and 10 is the stop number. Now if you want any step, by default step is always 1. It one by one the numbers are printed. But if you want some other things like 2, so you have to write step. It means it's going to skip 2 for you and then the next number will be printed. So if I'm going to use this logic over here, now let's see what's going to be about. It's saying 5, 7, 9. So it means it is skipping 2 this time. It's not printing 5, 6, 7. It's printing 5 and then skipped that 6, and then 7 is in the output, then 8 got skipped, and then we have 9. So this is what range function is. This works with the number. Any question in this? Okay, you can also use else in this. Else clause also you can add in this for loop. Simply you can say, suppose I wanted to know when my loop got finished, like when exactly the iteration stopped. So you can simply add else— is just an additional or optional, you can say, it is not compulsively part. You can add an optional else block after, you know, just to check your loop execution. So you just have to enter, then add else print, let's say, end. So it will print all the output and then end will be printed just to make sure that everything work Well, okay, now the next type of loop is the while loop. Now within the for loop, we were iterating it over something, right? I— okay, let's talk about one more example with the loop, then we will move to the next topic, while loop. Okay. So within the for loop, let's say I wanted to make something repeat for multiple times. So I am going to write, let's say, for l in range, print hello. Now I wanted to display hello 10 times, so I've simply written it with the range function saying that I want it 10 times and hello has to be printed. If you want numbers also with that, so you just have to write l variable because that is the temporary variable which will hold that particular single value for a short time, and it will display like this. If you wanted to start from 1 and you want the 10 numbers to be there, so you have to write the logic like 1, 11, and then display it. And you can see you are getting hello 10 times. So this is how you can make anything repeat multiple times. Right now we are working just with this print statement. So according to your use case, you can change your print statement with any conditions. Or anything further, like if you wanted to include if-else in this, you can do that. Now let's talk about the next type of loop, which is while loop. When it comes to while loop, while loop will repeat— while loop is used for repetition— while loop will repeat a block of code as long as the condition is true. Make a note, it will go as long as the condition is true. In for loop, we were not writing any specific or, you know, that typical kind of conditions. We were just writing some sequences, we were iterating it over something, and we were making something, uh, repeat multiple times. That's it. But within the while loop, it goes with the condition. So the syntax of the while loop is you write while, which is a keyword, then you have to mention the condition. Condition is You can use— it can be a Boolean conditions as well, true, false, something should be there. Everything within the true and false can make it go with a smooth process. With before while loop, you have to declare a variable. Just like in for loop, we are having the variable within the for loop statement, but in while loop you have to initialize one variable before you write any condition. So if I am saying x is equal to 0, so value of x is 0. So when I am writing while, now let's say I'm writing x is greater than equal to 0, then do something. If the value of x is greater than equal to 0, then do something. It can be like, let's say I'm just printing the value of x. Simply the way, same thing that we did with the— now if I'm going to run this, obviously you are going to get 0, 0, 0, 0 every time because I have used one operator in this. Let me just take it back. Yeah, I have written greater than or equal to. Yes, it is equal to 0 and the condition is true because we are not increasing the value of x, we are not decreasing the value of x. If you are not making any changes in the variable x, then it will, it will act as an infinite loop, infinite loop which will never end. There is no ending point. You can see, as long as my kernel is active, it will continue to print 0, 0, 0, 0, 0, because I just asked to print that particular value for me. Now I'm going to interrupt it. Yeah, now it got stopped because I interrupted my kernel itself, because there is no other way to do. Now I'm going to make some change. I'm saying if x is greater than 0, so if now this condi— if this condition is true, then only it will print value of x for me. Okay, so now you can see it's just printing 0. Now let's say I wanted to Every time when the loop will basically, uh, loop will start, basically, then I want it to increase my variable x with 1 every time. So I'm writing x plus equal to 1, and I'm going to run this. I am making an increment after printing. So when the loop started, the condition itself was False. That is why we are not getting any output. To make it happen, what we can do is we can say less than 5. If x is less than 5, then do this and make an increment. Now I'm going to run this. Now this time it's going to print first 5 numbers for me: 0, 1, 2, 3, 4. Because the condition says x is less than 5, yes. X is less than 5, which is 0, so it printed 0 for me. So then the next line was to make an increment. So now my x variable got incremented, and now the value stored in the x is 1. When it is 1, then it will check, okay, 1 is less than 5, yes, that's true, so it will print it. Then again I asked to add 1, so 1 1 means 2. 2 is less than 5, yes, it will print it. Then it will add 1, then 3 is less than 5, yes, then 4 is less than 5, and then 5 is not less than 5. So that is why we are getting up till 4, and there the loop ended. So basically, if it evaluates, it works on the basis of true and false. When you want an infinite loop, that you want that, okay, it should continue, I don't want it to stop, then you have to go with— you can also simply write while true— if I'm going to copy this and I'm going to save this— if I'm going to say while true, then this is an infinite loop. This only can break according to the condition that I am going to write over here. Like if I can write if this is condition and I want it to stop, or else or something, then only it will stop. Otherwise there is no way that while true will stop. While true will continue and continue. So simple way to use while loop is to work on the conditions. When you are writing your conditions correctly, it will work. Now I wanted to break while loop, which is Maybe anywhere I wanted to break it. Let's suppose the same code which we have. Let me add it. Spell it. Okay, so I'm writing x is equal to 0 while— this is the condition. And I somehow, I wanted to break it in mid of it. Like, I don't want a complete numbers to be printed. I want certain digits to be skipped, or at certain number I want it to break. I want numbers to be skipped, or else I want it to break on some numbers. So there are some statements that can be added along with the loops. It can be used with the while loop and it can be used with the for loop. Both loops you can use these statements. So there are two statements. First statement is the break statement and second statement is the continue statement. So break statement, obviously as the name says, it's— it will help you to break it. So if, let's say, in this I am going to say if x is equal to equal to 3, then simply break. If x is equal to equal to 3, then simply break. And I'm going to run this. Now you can see 0, 1, 2. I'm not getting the complete 5 numbers, I'm just getting 1, 2, 3. No matter what the condition is, no matter what I have asked it to print, in the mid, before printing anything, I have said that, okay, if it is 3, break. Stop right there. Break means stop the loop there itself. If I wanted to skip 3, then I can simply replace break with the continue statement. Let me just copy this again, and instead of a break, I will write continue, and this will instantly. I did something before this. Uh, ma'am, uh, while loop is still usable nowadays or mostly for loop is used? No, it totally depends on the use case because for loop has a different use case and while loop has a different use case. It's not about it's in trend or not. It depends upon the use case. Generally, if you are creating games— okay, yeah, go ahead. No, no, you tell. I was just asking the same thing. Okay, so I'm saying that just like if you wanted to create games, right, and that you want— like, let's suppose right now I'm in a mood to play game, so I'm playing maybe for an hour I wanted to play, or maybe for 2 hours, for 3 hours, or for 4 hours. It totally depends upon my mood, right? So I, according to my mood, I'm going to play it. So I wanted to start it on my own, uh, like behavior, and I wanted to stop it by my end. I don't want my game to stop instantly after 15 minutes, 20 minutes, or after certain levels. No, I wanted to play again and again, right? When it comes to The point that you want it again and again, it means you will go with the while. You will make it while True and you will keep it up to the user, and user can stop it anytime. Maybe let's say you can provide a user saying that, okay, if you wanted to stop it, you have to enter Q. If user will enter Q anytime, that game will stop. If user is not entering Q, it means user wants to play it. And it should go on. Your game shouldn't stop. So that is why we use while loop. And for loop is just for the basic iteration and for repetition. Got it? Yes, ma'am. Anyone else? Any other question in this? Yeah, anyone, any question? No, ma'am. Okay, I'm going to just stop it first and I will make the increment first and then I'm going to check it. Okay. This is how it has to be because we are taking 0. Okay, 0 is less than 5, and then we are making an increment and printing it. Then we are checking whether it is equal to 3 or not, and then we are printing it. Okay, so this is how continue statement works. Same thing with the for loop also. I will show you how you can use Okay, so here you can say if l == 3, then break. Now let me go to that. You can see 1, 2 you have got, but not the 3. And after 3 also you are not getting anything. If I'm going to say 8, then it will print up till 7, and 8 onwards there won't be any output. So this is how it goes. With the break. If I'm going to say continue, then simply it will continue but just will skip that particular number. So l == 8, you can see in the output we don't have 8, we have 7, and directly we have 9. When you wanted to skip, go with continue. When you wanted to break, go with break. So anything you have in your mind related to loops, you can come up with the questions now. Ma'am, can you solve some real-time examples in while loop? For this, first I will suggest you that you go through the concept with the syntax. Okay, once you have the good understanding where to use break, how break, break works, how continue works, and how else statement works, right? When you know the statements along with the loops, right, then only you can go with the complex statement. If I will take you to the complex statement, you can understand the question but not the code at that time. Then there comes a gap. Okay, so first understand the syntax and try to understand what these statements are for. Okay, okay, ma'am. Yeah, anyone else? Any other question related to loops or conditional statements? Anything? Yes, ma'am. And if we want to practice on those more and more, so which shows we should use? Sorry, what do you want to use? And can you repeat it? Yeah, what are you asking? Can you repeat your question once? Yes, ma'am. I am saying if I want to practice on these for loop and while loop, so what, what source I should use? Source use for questions. Okay, you are asking for source. Okay, so just like I have provided you some questions in the last session as well related to conditional statement, right? So I will provide for the for loop and the while loop also. I'm going to provide you basic set, okay, like 4 to 5 questions which will be very basic one, so which will help you get familiar with the syntax, okay. So that I will provide you by after this session, okay, same on the Discord channel itself, okay. Okay, okay, ma'am. Yeah, that you can practice. And ma'am, if we got some problem, yeah, you can share it on the Discord itself. Okay, ma'am. Yeah. Anyone else? Any other question? Or should we wind up the session? Uh, ma'am, share the Discord link please. Okay, I will share it. On WhatsApp. Okay, okay, ma'am. Yeah, all right guys, so you need to practice loops and then we'll discuss the next topic in the next session. Okay, all right guys, bye-bye.










