When do you use a for loop and when to use a while loop. Are there other loop types in Python? Some of the slicing examples almost look like loops.
sneekula 969 Nearly a Posting Maven
Recommended Answers
Jump to PostThe "for loop" loops (iterates) through the items of a sequence (string, list, tuple, dictionary, set ...). The "while loop" loops until a condition is met. Though the two loops are different beasts, they can overlap in functionallity. You can break out of either loop on a conditional statement.
…
Jump to PostHere is an example of continue and else ...
for k in range(10): # skip values > 3 or < 6 if 3 < k < 6: continue print k, # 0 1 2 3 6 7 8 9 else: # works only if there is no …
Jump to PostI think I am getting a feeling in which case to use 'for' or 'while'. Never looked at a recursive fuction as a loop, but I guess it is and can be used for that purpose. Almost looks like a limited goto!
With the loops there is also continue, break …
All 12 Replies

Mouche
Zonr_0 0 Newbie Poster
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
sneekula 969 Nearly a Posting Maven
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
jrcagle 77 Practically a Master Poster
sneekula 969 Nearly a Posting Maven
jrcagle 77 Practically a Master Poster
jrcagle 77 Practically a Master Poster

Mouche
jrcagle 77 Practically a Master Poster
fireworks-style 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.