About 446,000 results
Open links in new tab
  1. python - How to use range () with while loop? - Stack Overflow

    Feb 1, 2021 · In Python, range is an immutable iterable object similar to a generator. When you say range(5), it produces numbers from 0 to 4. You use a for_loop to iterate over a range, not a while_loop. For your purposes, you don't need range at all. Your code works perfectly without it.

  2. python while loop range function - Stack Overflow

    Apr 5, 2018 · Why can't while loop be used on a range function in python ? The code: def main(): x=1; while x in range(1,11): print (str(x)+" cm"); if __name__=="__main__": main();

  3. range () in while loop in Python - Spark By {Examples}

    May 30, 2024 · You can use the range function in a while loop in Python to repeat a block of code a specific number of times. The range function generates a sequence of numbers, starting from 0 by default, and increments by 1 (also by default), and stops before a specified number.

  4. Python Looping Through a Range - W3Schools

    The range () function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. Using the range () function: Note that range (6) is not the values of 0 to 6, but the values 0 to 5.

  5. Iterate Over a Range Using While Loop in Python - Python

    Learn how to iterate over a range using a while loop in Python. This tutorial includes syntax and examples to help you understand the concept.

  6. PythonLoop Through a Range - GeeksforGeeks

    Dec 23, 2024 · In this article, we will explore the different ways to loop through a range in Python, demonstrating how we customize start, end, and step values, as well as alternative methods for more advanced use cases like looping through floating-point ranges or infinite sequences.

  7. Python While Loops - W3Schools

    With the while loop we can execute a set of statements as long as a condition is true. Note: remember to increment i, or else the loop will continue forever. The while loop requires relevant variables to be ready, in this example we need to define an indexing variable, i, which we set to 1.

  8. Loops in Python – For, While and Nested Loops - GeeksforGeeks

    Mar 8, 2025 · Let us learn how to use for loops in Python for sequential traversals with examples. Explanation: This code prints the numbers from 0 to 3 (inclusive) using a for loop that iterates over a range from 0 to n-1 (where n = 4). We can use for loop to iterate lists, tuples, strings and dictionaries in Python.

  9. Python range() Function: How-To Tutorial With Examples

    Jun 27, 2023 · Python’s range acts as a built-in function, and is commonly used for looping a specific number of times in for-loops. Like many things in Python, it’s actually a Python type (or class), but when using it in a loop, we can treat it like a …

  10. Python range () Function - Analytics Vidhya

    Apr 3, 2024 · We can use the Python range () function to control the loop condition in a while loop. For example, the following code prints the numbers from 0 to 4 using a while loop: while i < 5: print (i) i += 1. The Python range () function has various use cases in Python programming.

Refresh