
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert a Python for loop to while loop?
Unlike while loop, for loop in Python doesn't need a counting variable to keep count of number of iterations. Hence, to convert a for loop into equivalent while loop, this fact must be taken into consideration.
Following is a simple for loop that traverses over a range
for x in range(5): print (x)
To convert into a while loop, we initialize a counting variable to 0 before the loop begins and increment it by 1 in every iteration as long as it is less than 5
x=0 while x<5: x=x+1 print (x)
- Related Questions & Answers
- Python - How to convert this while loop to for loop?
- How to emulate a do-while loop in Python?
- How does Python while loop work?
- Java while loop
- For Versus While Loop in C
- Difference Between for and while loop
- How can I write in order with for loop or while loop?
- How to break a for loop in Python?
- do…while loop vs. while loop in C/C++
- How to use C# do while loop?
- How to use for loop in Python?
- How to handle exception inside a Python for loop?
- How to create a triangle using Python for loop?
- How to write a while loop in a JSP page?
- How to use nested while loop in JavaScript?
Advertisements