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 Postfor loops are specifically helpful for going through a string or list or tuple... the for loop executes using whatever variable you say as the current item (does that make sense?)
nums = [1,2,3,4] for number in nums: print item,
Here, for each iteration the "item" variable …
Jump to PostFor and While loops serve different functions, you can actually make a For loop using a while loop (though I don't reccomend it.)
Here's a useful function for text-based games:
def ask_yes_no_question(question): """Asks a yes or no question for the user""" response = "None" while response …
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.
…
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.