Python Tutorial for Beginners 17 - Python while Loop



 hey guys welcome to the next blog on Python tutorial for beginners in this blog I'm going to show you how to use loops in Python and we will start with the while loop so first of all what is a loop so a loop allows us to repeat over some block of code again and again until and unless some condition is met now let's see how to use while loop in Python so let's say I'm going to declare a variable called I and I'm going to initialize it with the value 0 and to use a while loop you use this keyword and then you provide some condition so let's say I want to check whether the value inside the I variable is less than 5 or not okay and as in the case of if also we have seen we provide after the condition this colon symbol and then in the next line we write the code which we want to perform again and again until this while loop condition is true so let's say we want to just print something and we will print that the value of I is and then we will print the value of I like this also I want to change the value of I with every step so I will change the value of I using this expression which is I plus equals 1 this essentially mean that we want to just add 1 every time whenever this code is executed also we can say that we want to perform this operation I is equal to I plus 1 so this statement is similar to this statement let me provide some spaces here now in here also you will observe that I have provided some indentation here and indentation means that these two lines of code is the part of while loop ok so when I want to write something outside the while I will start from here with no indentation and let's say I want to just print that we are finished and now let me run the code and let's see what's the result and you will see here first of all this string is executed which is the value of I is and then the value of I is printed which is 0 1 2 3 & 4 so as you can see here that we have initialized I is equal to 2 so the initial value of I is 0 now when we come to this code and when this condition is checked here the value of I is 0 which is less than 5 that means this condition is true and that means that these two lines of code which are under while loop will be executed so this line is executed where value of I is printed which is I is equal to 0 and this line of code increments the value of I by 1 so when this line of code is executed now the value of I becomes 1 because previously it was 0 and once again this program flow goes to the starting to evaluate this condition and this time the value of I is 1 which is less than 5 and the condition is true once again and again this line is of code is executed and this line of code is executed and the value of I is incremented by 1 once again and the value of I becomes 2 here and this will happen again and again until the value of I becomes 5 and when the value of I becomes 5 this condition will be false and that means this code will not be executed this code will only be executed when the while loop condition is true and then at last this line of code is printed which is finished while loop now let me give you one more example of while loop and I will start from the top once again and this time I will declare a variable called num and I will initialize this value by 0 and also I will declare a variable calls an initial value of sum is also zero now here first of all I will ask the user to enter a number so I'm going to just write and also I will ask the user if he or she wants to exit from the loop he can enter zeros and then in the next line I will start with my while loop and I will give the condition if the number so if the number I'm going to write number is not equals to zero we want to perform some operations and what operation we want to perform we want to ask the user to provide any number and we will transfer this number into the variable num so here I will first of all write input and then I will ask the user to provide the number and when the user provides the number I want to convert it to a float value so I will just type cast this input to a float value and then this number will be assigned to the variable num now here don't forget to give the Kulin and in the next line what I will do is I will just use my some variable and I will just add the number value to the sum so I can just write sum is equal to sum plus num and this means that initially the value of sum will be zero and then this number will be added to the sum and until this loop runs we will get the sum of all the numbers which is entered by the user and at last I'm going to just print the value of sum so now what do you think will happen so in this condition we are evaluating that num should not be equal to zero and then only this code will be executed and we have provided the initial value of number is equal to zero so this condition will never be met and this statement will never be executed so we need to provide the initial value of num is equal to 1 and then whenever the user provides the value this one will be overwritten by the value which is provided by the user so this time when we run the code you can see first of all the program asked us to provide a number so I will provide hundred here and then press ENTER and you can see the sum is printed which is equal to hundred once again we will provide some number and then press ENTER and now you can see sum is equal to 150 once again we will provide one number and then press ENTER and now the sum is one zero five zero now let's say we provide a number is you can do zero and then press ENTER you can see that our while loop is finished because zero is not equal to zero that means a false condition and then the flow of program comes out of the loop and then the other while loop will be executed which is this while loop and that's why this output is printed we can also write that sum is equal to so we know that this is the sum so sum equals and then after the comma we will print the value of sum and once again when we execute the code and once I want to exit from the code I can use zero to exit so zero and then press ENTER and I come out of this loop now Python also supports the else statement to be associated with the loop statement so what I mean by the else statement to be associated with the loop statement is that I can use after the end of this while loop condition I can use here else and after the colon I can provide some code which I want to execute once this loop is finished okay so I can write here that I want to print for example finished sum so let me run the code once again and I will quickly provide some numbers here and which will provide the sum and when I provide a zero here then we come out of the loop and you can see this finished some string is printed using this else so once your program comes out of the loop this else statement will be executed similarly we can go down and here also instead of writing this finished while loop we can also give else statement here so I'm going to just write else and after the colon here I need to provide the indentation so for space indentation one two three four and everything will be okay and once I run the code and first of all I will provide zero here to see if this is executed and you can see this else condition is called once your while loop is finished now also you can provide some condition here which is always true so for example I can write through here and this means that while loop evaluates to true every time and this means that this statement will be executed forever so this is also possible but you need to think carefully what do you want to do do you want to execute this code infinitely or do you want at some time that this condition will be false so this is how you can use while loops in Python I hope you have enjoyed this blog I will see you in the next blog 

Post a Comment

Previous Post Next Post

Recent in Technology News