When to use while or for in Python

0 votes

I am finding problems in when I should use a while loop or a for loop in Python. It looks like people prefer using a for loop (less code lines?). Is there any specific situation which I should use one or the other? Is it a matter of personal preference? The codes I have read so far made me think there are big differences between them.

Feb 9, 2022 in Python by surbhi
• 3,820 points
482 views

1 answer to this question.

0 votes
Yes, there is a significant distinction between while and for.

The for statement loops over a list, an iterable object, or a generator function.

The while statement loops until one of the conditions is False.

It isn't a matter of personal preference. It all boils down to your data structures.

We frequently represent the values we want to analyze as a range (an actual list) or xrange (which generates the values). (Edit: In Python 3, range is now a generator and functions similarly to the old xrange function.) xrange is no longer available in Python 3. This creates a data structure that is specifically designed for the for statement.

In most cases, though, we already have an iterable collection: a set, tuple, list, map, or even a string, so we just use a for loop.

We may need some functional-programming processing done for us in a few circumstances, in which case we can perform that transformation as part of iteration. The sorted and enumerate functions alter an iterable in a way that makes sense with the for statement.

You must use while if you don't have a tidy data structure to cycle through or a generator function to drive your processing.
answered Feb 9, 2022 by CoolCoder
• 4,420 points

Related Questions In Python

0 votes
1 answer

When to use "while" or "for" in Python?

while and for are both flow control ...READ MORE

answered Feb 9, 2022 in Python by Nandini
• 5,480 points
394 views
0 votes
1 answer

When to use %r instead of %s in Python? [duplicate]

The %s specifier converts the object using ...READ MORE

answered Aug 2, 2018 in Python by bug_seeker
• 15,510 points
1,162 views
0 votes
1 answer

When to use file vs open in Python?

File() has been removed since Python 3.0 ...READ MORE

answered Oct 30, 2018 in Python by SDeb
• 13,300 points
1,263 views
0 votes
1 answer

How do I use urllib to see if a website is 404 or 200 in Python?

For Python 3, try doing this: import urllib.request, ...READ MORE

answered Nov 29, 2018 in Python by Nymeria
• 3,560 points

edited Dec 11, 2018 by Nymeria 14,028 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,069 views
0 votes
1 answer

What is the python keyword "with" used for?

It's useful when you have two or ...READ MORE

answered Feb 9, 2022 in Python by CoolCoder
• 4,420 points
698 views
0 votes
1 answer

How do I specify new lines on Python, when writing on files?

It is up to you to decide ...READ MORE

answered Feb 14, 2022 in Python by CoolCoder
• 4,420 points
678 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP