How to use for loops and while loops in python
Hi guys, I am wondering how to Use the for loops and while loops in python. Please write the full explanation that how did it work and please give me the exact and correct syntax of for and while loops. I have already watched many videos about that but I did'nt understand what did they try to say and what was the syntax. The ones who will explain me properly how to use them and their syntax then I will select their answer only and I will publish it in the Python daily dose series.
You use for
with an iteraterable.
for item in [1, 2, 3]:
for i, l in enumerate([1, 2, 3])
for i in range(1, 10)
and a for loop takes a bool:
while 1 < 2
:
Thanks for help @CodeLongAndPros
hi there!
let me explain them to you:
for loops
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
for example:
for i in range(5): #will repeat for 5 times # remember to indent print("Hello")
output:
Hello Hello Hello Hello Hello
another example: a countdown program
import time countdown = 60 # setting a variable for the countdown for i in range(60): # repeats 60 times print("Countdown: "+str(countdown)) # prints what number we are at countdown -= 1 # takes 1 off the countdown (because it has been counted) time.sleep(1) #waits 1 sec before continuing
output:
Countdown: 60 Countdown: 59 Countdown: 58 Countdown: 57 Countdown: 56 Countdown: 55 Countdown: 54 # etc until Countdown = 1
learn more about for loops here:
https://www.w3schools.com/python/python_for_loops.asp
https://www.learnpython.org/en/Loops
https://wiki.python.org/moin/ForLoop
https://www.geeksforgeeks.org/loops-in-python/
https://www.tutorialspoint.com/python/python_for_loop.htm
while loops
With the while loop we can execute a set of statements as long as a condition is true.
example:
while True: print("Hello")
output:
Hello Hello Hello Hello Hello # until infinity
another example: a sort of password guesser:
password = 100 #sets the password guess = input("guess password: ") #asks for the guss while guess != password: #keeps on going until password is found print("try again!") guess = input("guess password: ") if guess == "100": # if user finds password print("youve guessed it!") break #ends the loop
output:
learn more about while loops here:
https://www.w3schools.com/python/python_while_loops.asp
https://realpython.com/python-while-loop/
https://www.programiz.com/python-programming/while-loop
https://www.geeksforgeeks.org/python-while-loops/
tell me if you have any other questions ;)
(link to repl with examples: https://repl.it/@Bookie0/helpprob#main.py)
@Bookie0. If I wanted I would mark you the second one who had the best answer. Currently I have marked @DyanmicSquid's answer as the best
@ParthChawla Hi again,
As you said, Here is my own definition:-
There are two types of Loops:-
1. While Loop
A While Loop is basically a structure in Python which allows us to loop through and execute a block of code multiple times.
Correct syntax of writing a While Loop:
i = 1 while i <= 10: print(i) i+=1 print('Done with While Loop!')
2. For Loop
A For Loop is a special type of loop in Python which allows us to loop over a different collection of items. It can be used in strings, numbers, and etc...
Correct syntax of writing a For Loop
for letter in "Hello World\n": print(letter)
If you need theory beyond these types, Comment me!
Thanks!
Hope this helps
@PattanAhmed I commented. Now tell me the theory
Thanks for help @Coder100
@ParthChawla Hi,
There are two types of Loops in Python
While Loop:-
Check these amazing websites to know more about While Loop:-
Click here
For loop:-
Check this amazing website to know more about For Loop
Click here
Thanks!
Hope this helps
@PattanAhmed, You answered my question so quick. That is amazing. You really are one of the most top contributers of repl.it. I would have appreciated you if you gave the answers on your own.
@ParthChawla I can't type that much here...
So, I redirected there
Thanks!
Hope that helps
@PattanAhmed, You are good in coding. I invite you to code with daily ds everyday from 8 to 9pm, India time
@PattanAhmed, As you are good in coding. Visit the python daily ds and answer the question asked in the python daily dose. If it was right then you can be featured in the next python daily dose
@ParthChawla Ok, I will try my best!
@ParthChawla What is Python ds?
Where is it?
@PattanAhmed, It is actuall python daily dose which I post everyday
@ParthChawla Where it is?
Thanks!
A While Loop is basically a structure in Python which allows us to loop through and execute a block of code multiple times.
Correct syntax of writing a While Loop:
i = 1
while i <= 10:
print(i)
i+=1
print('Done with While Loop!')
Maybe you can understand from here.
https://www.entechin.com/python-if-else-one-line-statement/
try these examples out yourself!
@DynamicSquid, Thank you so much for the answer. This is easy to understand and has the correct syntax. That is why I have marked your answer the best one.