While Loops in Python | Python Tutorial – Day #18
![*](https://i0.wp.com/allprowebdesigns.com/wp-content/uploads/2024/05/1716655037_maxresdefault.jpg?resize=840%2C430&ssl=1)
Video Title: While Loops in Python | Python Tutorial – Day #18
We discussed ‘For Loops’ in preceding video and that was quite fun using them And we learned how it make our tasks so easy And along with ‘For Loops’ we learned how we can print multiplication tables And now we’ll learn how we can do the same with while loop And why ‘While Loop’ was created and added to Python Programming ‘While Loop’ was introduced to Python programming to save time of a programmer How? Let’s move to the computer screen and let me tell you [ [S [ST [STA [STAR [START [STARTI [STARTIN [STARTING [STARTING [STARTING T [STARTING TH [STARTING THE [STARTING THEM [STARTING THEME [STARTING THEME] Now we’re about to discuss ‘While Loop’ And how we can use them But first let me tell you whether you use ‘For Loop’ or ‘While Loop’ They both will do your work But sometimes the syntax of ‘While Loop’ is convenient and sometimes that of ‘For Loop’ In ‘For Loop’ syntax we learned that we write "for i in range" And let’s suppose if we write " 3 " here And if I write " print(i) " here It’s very simple that you’ll get 0,1&2 printed We all know that But if I do the same thing using ‘While Loop’ I’ll firstly create a variable And I’ll write "While (i>3)" And then I’ll write " print(i) ‘ And then I’ll increment the value of i Let me explain what have I written What I did here is… I applied a condition for ‘while’ I command it to make the value of ‘i’ 0 And then I commanded it " While the value of (i>3) Keep on printing the value for ‘ i ‘ " And I’m keep on increasing the value of ‘ i ‘ in every iteration At first "0" will be printed and then i’s value will increase to 1 Then " 1 " will be printed and then i’s value will increase to 2 Then " 2 " will be printed and then i’s value will increase to 3 But…… As the value of ‘ i ‘ keep on increasing this loop is keep on executing This condition will be checked in every iteration Let me explain how it works What’s the value of ‘ i ‘? It’s " 0 " Firstly it’ll move to line #5 And it’ll check if the condition is satisfied or not Because if not… Then we won’t want to execute anything from this condition OK! This condition will be executed because this condition is true here (0<3) is true The answer will be "Yes the condition is true" " print(i) " means print("0") So " 0 " will be printed And then " i = i + 1 " which means i’s value will increase to 1 And then it’ll again move to this line and check if (1<3), and it’ll get "true" And then it’ll print " 1 " too And now i’s value will increase to 2 Is (2<3)? Yes it is…. So it’ll print 2 and i’s value will increase to 3 Is (3>3)? No it’s not…. Not at all And because (3>3) is ‘False’ so look, i’s value won’t be printed this time This code won’t be executed this time and it’ll exit the loop And let me write here " print("Done with the loop") " And now let me remove this ‘For Loop’ Now if I run this so "0,1&2" will be printed But if I change this to (i<=3) Note this point (3<=3) is True If we discuss if (3<=3) so it’s true Because ‘Less than equals to" means either ‘less than’ or ‘equal’ Now if we talk about (3<=3) so this condition is satisfied (3<=3) While (3<=3), 3 will be printed here Here " 0, 1, 2, & 3 " will be printed instead of " 0, 1 & 2 " if I change this to "while (i<=3) " Look, " 0, 1, 2, & 3 " is being printed This is how ‘While Loop’ works Ideally we don’t use ‘While Loop’ with numbers But we use them with complex conditions For that, let me give you an example. Let’s assume user gave an input Assume user gave an input So here I’ll write " int(input… " I already have told you why we use this …("Enter the number: ")) " So this is how we can take the input as an ‘Integer’ "As an ‘Integer’ " this is important to note I’ll command it " while (i<=3) " Keep on taking the input from the user So I’ll add this in a ‘Loop’ I’ll command it to "take input from the user while…." Let me increase this value "….. input is less than 38 and keep on printing that number And I’ll remove this ( i = i + 1 ) from here What function is this loop performing? This loop is keep on taking the input until…. Until i’s value is less than equals to 38 (i<=38) Let me run this program…. It is saying " i is not defined " What’s this man! OK Let me do one thing I also have to take an initial value of ‘ i ‘ from the user Before executing the ‘While Loop’ let me take the value of ‘ i ‘ Look I’ve taken the value of ‘ i ‘ from the user so firstly this line of code will be executed Then it’ll ask me about the value of ‘ i ‘ so let’ suppose if I give " 8 " And because (8>38) is true, so it’ll ask me again about the value and will print " 8 " Or if I give value " 9 " so it’ll print " 9 " " 9 " "Enter" Look it has printed " 9 " and again asked me the same question Now let’s suppose if I write 34… It asked me this question again and suppose if I write 38 38 is actually less than equals to 38 so these line will keep on executing Until when? Until the " (i<=38) " is ‘True’ And the moment the value of ‘ i’ becomes greater than 38 Which means if this condition is not satisfied So it’ll print "Done with the loop" and exit the loop So if I make the value of ‘ i ‘ " 98 " so 98 will be printed Condition checked, it was false and then this was printed And the program is over I hope you get this I’ve written here "As the name suggests, while loops execute statements while the condition is True." While the condition is ‘True’ ‘While Loop’ will keep on executing "As soon as the condition becomes False, the interpreter comes out of the loop. " What does interpreter means? It is Python Interpreter is the control of your program which help you to execute your program This is what I mean when I say ‘Interpreter’ That interpreter will exit the loop Control will exit the loop But if the condition is ‘True’ so it’ll keep on doing its work Whether I refer it as ‘Interpreter’ or ‘Control’ Or ‘Program Controller’ they all are same You know what I mean Now you can run this program Here I’ve used quite simple example of ‘Decrementing Loop’ This is important too Let me comment this out This is quite important too. It is a ‘Decrementing While Loop’ At first the value of count is " 5 " But this time condition specifies " while (count > 0) " And now if I keep on decreasing count’s value so a time will come when the condition become ‘False’ Because a time will come when count’s value will be 0 So this is the reverse while loop ‘Decremention While Loop’ First " 5 " will be printed then 4… 3… 2… 1… And now if I explain you the execution so pay attention At first the value of count was " 5 " And this condition was true because (5>0) Then I did " print(count) " and " 5 " was printed Then " count = count – 1" so the new value was 4 And it’ll check the condition again because it is ‘While Loop’ Is (4>0)? Yes it is… so 4 will be printed and the new value will be 3 because of this line And then it’ll check " 3 " and it’ll print 3 and same goes for 2 and 1 and so on A time will come when the value of count will be " 0 " Is ( 0 > 0 ) ? No absolutely not So the condition will be false And the program will exit out of the loop I hope you all understood this " Here, the count variable is set to 5 " I’ve explained this here Which decrements after each iteration. Iteration is count for the running of a loop means how many times the loop ran Condition checked…. Entered in a loop the count goes for 1 iteration The process repeats…. It means 2nd iteration So this is what ‘Iteration’ means "Depending upon the while loop condition, we need to either increment or decrement the counter variable" Now we either have to increase or decrease it as per our program If we made a mistake in ‘While Loop’ For eg let’s suppose if I mistakenly written this as " count = count + 1 " So this will become an infinite loop Let me show you…. Look it has become an infinite loop And it’ll keep on printing the values Do you understand this? It means that it’ll print 5…6…7…8…9…10…. and it’ll keep on printing till infinity It won’t stop until I cancels it I’ve to cancel it now Now I’ll delete it so that it’ll be canceled And I…. What’s going on man! I started an infinite while loop… How can I cancel it now? I think I can do this by closing this console Oh god! Oh I can stop this program Here’s the option It didn’t stop! Why man? OK! It stopped Oh-ho I ran this program again Let me fix it I’ll fix it & stop it Sorry, community of Replit because I’m using so much computing [CHUCKLES SOFTLY] Let me tell you a story what happened once When I was college, a lot of ants made their colony in my laptop There were a lot of them and I started an infinite ‘While Loop’ I was in Java Programming language But it is the same And the fan of my laptop starts catching up speed just like a drone Which result in running the ants away So ‘While Loop’ may also helpful for you in other things too Now if I run it so it’ll print 5…4…3…2…1… You can also use ‘Else’ with while loop And this is how you can use ‘Else’ That if the condition is not satisfied with ‘While Loop’ so it’ll enter ‘else’ Now here I’ll write " print("I am inside else") " The value of count is " 5 " and let me tell you how this works It’ll keep on executing and finally when the condition will be false It’ll print "I am inside else" So this is quite simple just pay attention here how you can use ‘Else’ with ‘While Loop’ "You can even use the ‘Else Statement’ with the ‘While Loop’ " "Essentially what the else statement does is that as soon as the ‘While loop’ condition becomes false the interpreter comes out of the ‘While Loop’ and the else statement is executed. When this will exit from the ‘While Loop’, ‘Else’ will be executed I hope you understood this that whenever…. Whenever it’ll fail to enter the ‘While Loop’ because of the unsatisfied condition it’ll print ‘Else’ Now if I change it to (count = -5 ) will the ‘Else’ statement be printed? Tell me in the comments You’ve to tell me… I hope you’ll have guessed "It will" Now look… "I am inside else" will be printed So this was quite simple Now I’d like to tell you one more thing In other programming languages we have ‘Do While Loop’ It is not in Python, let me enlighten you What ‘Do While Loop’ does is, it executes the ‘While Loop’ at least once ‘Do While Loop’ is made for the type of cases which we discussed at the first Which I commented now Let me comment this one out What we did is… we executed this outside of ‘While Loop’ once Now if I write here " print(i) " so it’ll be a perfect ‘Do While Loop’ What ‘Do While Loop’ does is, It execute the content of loop at-least once It execute the content of loop once irrespective of whether the condition is true or false Irrespective of whether the condition is true or not it executes it If ‘Do While Loop’ was supported here… Let me tell you syntax for ‘Do While Loop’ It doesn’t support in Python but in C, C++ and Java But you must aware how ‘Do While Loop’ works If I talk about C language I’m not teaching you C, so don’t assume I’m teaching you C This place is for condition and this is for content In-fact I’ve to replace "content" with "loop body" What happens is… It executes at-least once And if the condition is ‘True’ so it executes again This is quite simple it is ‘Do While Loop’ Sometimes a question erupt in Python "Emulate ‘Do While Loop’ in Python" This is the frequently asked question that’s why I’m telling you what’s this And if you’re thing you’re here only to learn Python And I don’t have anything to do with ‘Do While Loop’ Don’t think that Because if you don’t know what ‘Do While Loop’ is, so how can yo emulate that? ‘Do While Loop’ is a simple ‘While Loop’ which executed at-least once irrespective of whether the condition is true or not It’ll execute once then if the condition is ‘True’ It’ll executed again if the condition is ‘True’ It’ll executed again But if ‘False’ so it’ll be exited So this is ‘Do While Loop’ Now let me tell you one more thing In the succeeding video I’ll tell you about ‘Break & Continue Statement’ In the mean time you’ve to figure this out on your own Now you’ll say "Why I figure it out on my own?" Look, when you’ll start figuring out things on your own And if you’re able to understand them then that feeling will be different Remember this In the succeeding video I’ll tell you about ‘Break & Continue Statement’ And it’s your duty to come back to the Repl of Day #18 And try to emulate ‘Do While Loop’ on your own What does ‘Emulation’ means? It means that you just have to emulate ‘Do While Loop’ Now this is the way to emulate ‘Do While Loop’ I’ve written body 2 twice And let’s suppose if the body was of 2000 lines so our code would have been of 4000 lines Because you’ll write one time inside and one time outside That’s not the ideal So there’s even more efficient way which you’ll use Just watch the next video once of Day #19 And then try to emulate ‘Do While Loop’ I hope you all understood what ‘While Loop’ is And if you haven’t accessed the play-list of Python so make sure to access it That’s it for this video guys Thank you so much guys for watching this video And I’ll see you next time
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
Day 18 done ✅
My practice solution
#Decrementing while loop
i = 5
while (i > 0):
print(i)
i = i -1
else:
print("Happy New Year")
#Increment while loop
y = 1
while (y <= 3):
print(y)
y = y + 1
else:
print("Get Set Go!")
#Enumarate do-while loop
while True:
number = int(input("Enter a positive number"))
print(number)
if not number > 0:
break
for loop in range(4):
print(2)
a=int(input("ENTER YOUR NUMBER HERE: "))
while (a<3):
print(a+1)
a=a+1
Kon kon 2025 me dekh raha hai 😂
100 videos in a single day but on day 10, present sir
Sir yeh repl Kaishe access kare visual studio vale??
how do you select all and put # in front?
hello
day 18
i am a complete newbie and have tried a nested code of while loops
your review would be valuable for me
k = 5
print(k , "is smaller than 50")
while(k<51):
print(k , "is smaller than 50")
if (k == 50):
print(k , "is equal to 50")
k = int(input("the value:"))
else:
print(k , "is bigger than 50")
jsk
that samajh rhe ho 🤣🤣🤣
bhaiya me vivaan ho maine jab apne bataya hua code dala to mujhe vo har number ke bad mujhe statement dikha raha hai whereas mayne ye code dala
i = 0
while(i<3):
i = i + 1
print (i)
print ("Done with the loop") to mujhe barabar out put aya assa kyo ?
harry yrr You are teaching very well.
I dont know why these kind of teacher we dont get in college. 👍
😘😘😘😘😘😘
use of infinite loop : to scare away ants
1:31
print("I am inside else") 💀💀
What's The Output Of The Following:-
i = 0
while(I <= 11):
print(i)
i = i + 1
Present 💝
Multiplication table banana kab sikaa koii bataoo
12:47 do while loop मतलब पहले इस्तेमाल करो फिर,,,,, ।first you do ,then the confition ,if condition not match then dont do
I tried this as a password project character have to be more than 8 and it work if password is less than it shows weak password again enter it
Itss great
#HarryBhai
Jada fhoodko Mt do while baki hai abhi😂
Do together you will see magic😅
count = count -1
count = count *2
bhai ye batao ki for loop kb padhaya aapne