Check out my post on 21 Python for Loop Examples. Finding the factorial of a given number using while loop n = int(input("Enter the number: ")) Print("Sum of even numbers till n:",sum) 18. Finding the sum of even numbers using while loop i = 0 Reversing a number using while loop in Python n = int(input("Enter a number: ")) Finding the multiples of a number using while loop n = int(input("Enter an integer: ")) Printing the square of numbers using while loop n = 1 Print("Average of given Numbers:",average) 14. Finding the average of 5 numbers using while loop p = 5 Example of using while loops in Python n = 1 Check out these examples to get a clear idea of how while loops work in Python. In this post, I have added some simple examples of using while loops in Python for various needs. The syntax of a while loop is as follows: while condition: The loop will stop its execution once the condition becomes not satisfied. The while loop checks a condition and executes the task as long as that condition is satisfied. Use the range(start, stop, step) to customize the loop.In Python programming, we use while loops to do a task a certain number of times repeatedly.Use the for loop statement to run a code block a fixed number of times. Print(sum) Code language: Python ( python ) Summary Second, the sum is added with the number from 1 to 100 in each iteration.īy the way, if you’re a mathematician, you can use the simple formula: n = 100.Output: 5050 Code language: Python ( python ) Print(sum) Code language: Python ( python ) The following example uses the for loop statement to calculate the sum of numbers from 1 to 100: sum = 0 for num in range( 101): Output: 0 2 4 6 8 10 Code language: Python ( python ) Using Python for loop to calculate the sum of a sequence The following example shows all the odd numbers from 0 to 10: for index in range( 0, 11, 2): In this form, you can specify the value that the range() function should increase. To increase the start value by a different number, you use the following form of the range() function: range(start, stop, step) Code language: Python ( python ) Output: 1 2 3 4 5 Code language: Python ( python ) Specifying the increment for the sequenceīy default, the range(start, stop) increases the start value by one in each loop iteration. The following example uses a for loop to show five numbers, from 1 to 5 to the screen: for index in range( 1, 6): In this syntax, the range() function increases the start value by one until it reaches the stop value. In addition, the range() function allows you to specify the starting number like this: range(start, stop) Code language: Python ( python ) Specifying the starting value for the sequenceīy default, the range() function uses zero as the starting number for the sequence. In this example, we increase the index by one in each iteration and print it out. Output: 1 2 3 4 5 Code language: Python ( python ) Print(index + 1) Code language: Python ( python ) If you want to show 5 numbers from 1 to 5 on the screen, you can do something like this: for index in range( 5): In this example, the for loop executes the statement print(index) exactly five times. Output: 0 1 2 3 4 Code language: Python ( python ) Print(index) Code language: Python ( python ) The following example shows how to use the for loop with the range() function to display 5 numbers from 0 to 4 to the screen: for index in range( 5): The following diagram illustrates the for loop statement: Note that it’s always short of the final number ( n). So the range(n) generates a sequence of numbers: 0, 1, 2, … n-1. It increases the value by one until it reaches n. The range(n) generates a sequence of n integers starting at zero. It’s like the print() function in the sense that it’s always available in the program. The general syntax of a Python for loop looks like this: for newvariable in parentvariable. For instance, while there are syntax differences, the characteristic of Java's for loop is similar to how Python's for loop works. A for loop has similar characteristics in all programming languages. The range() is a built-in function in Python. An in keyword usually follows a for loop in Python. The name of the loop counter doesn’t have to be index, you can use whatever name you want. And n is the number of times that the loop will execute the statement. In this syntax, the index is called a loop counter. Statement Code language: Python ( python ) The following illustrates the syntax of a for loop: for index in range(n): In programming, you often want to execute a block of code multiple times. Introduction to Python for loop statement with the range() function Summary: in this tutorial, you’ll learn about the Python for loop and how to use it to execute a code block a fixed number of times.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |