Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop In Python
#1
Hello All, I am learning python and I want to know the difference between while loop, do while loop, and for loop? Can anyone tell me the difference with some basic examples?
Reply
#2
How are you learning? Your learning materials should tell you the difference between them. Python doesn't have a do while loop.
Reply
#3
Really, just do a quick google search. You'll find dozens of different sites with Python loops and examples.
Reply
#4
In simple terms -
  • For loop usually specifies an item inside a list, tuple, dictionary etc.
  • While loop usually is used to carry out a process repeatedly until a given condition is satisfied
As for do while loop - well, python doesn't really have that
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#5
There are different types of loops available in python ie. For loop and While loop.

There is a another function range() to create a sequence that we can use with For Loop.
Reply
#6
@theknowshare - promoting tutorial with python 2 examples and more over not PEP8 compatible is not advisable. Please, restrain from posting with sole purpose to promote your site.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
I have found that good practice is to start with Python built-in help (help('for'), help('while'))

>>> help('while')
The "while" statement
*********************

The "while" statement is used for repeated execution as long as an
expression is true:

   while_stmt ::= "while" expression ":" suite
                  ["else" ":" suite]

This repeatedly tests the expression and, if it is true, executes the
first suite; if the expression is false (which may be the first time
it is tested) the suite of the "else" clause, if present, is executed
and the loop terminates.

A "break" statement executed in the first suite terminates the loop
without executing the "else" clause’s suite.  A "continue" statement
executed in the first suite skips the rest of the suite and goes back
to testing the expression.

Related help topics: break, continue, if, TRUTHVALUE
(END)
press q to exit help
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020