# Python3 loop statement

This chapter will introduce you to the use of Python loop statements.

The loop statements in Python have for and while.

The control structure diagram of the Python loop statement is as follows:

## While loop

The general form of the while statement in Python:

```while Conditions:
statements```

Also need to pay attention to the colon and indentation. Also, there is no do..while loop in Python.

The following example uses while to calculate the sum of 1 to 100:

## Instance

#!/usr/bin/env python3 n= 100 sum = 0 counter= 1 while counter <= n: sum= sum + counter counter += 1 print("The sum of 1 to %d is: %d" % (n,sum))

The results are as follows:

`1 to 100 The sum is:  5050`

### Infinite loop

We can implement an infinite loop by setting the conditional expression to never be false. The example is as follows:

## Instance

#!/usr/bin/python3 var= 1 while var == 1 : # expression is always true num= int(input("Enter a number:")) print ("The number you entered is: ", num) print ("Good bye!")

Execute the above script, the output is as follows:

```Enter a number :5
The number you entered is: 5
Enter a number :```

You can use CTRL+C to exit the current infinite loop.

Infinite loops are very useful for real-time requests from clients on the server.

### while loop using the else statement

Execute the else statement block in while ... else when the conditional statement is false:

## Instance

#!/usr/bin/python3 count = 0 while count < 5: print (count, " Less than 5") count = count + 1 else: print (count, " greater than or equal to 5")

Execute the above script, the output is as follows...：

```0 Less than 5
1 Less than 5
2 Less than 5
3 Less than 5
4 Less than 5
5 greater than or equal to 5```

### Simple statement group

Like the syntax of an if statement, if you have only one statement in your while loop, you can write the statement on the same line as while, as follows:

## Instance

#!/usr/bin/python flag = 1 while (flag): print ('Welcome to the web tutorial!') print ("Good bye!")

Note: For an infinite loop above, you can use CTRL+C to break the loop.

Execute the above script, the output is as follows:

```Welcome to the web tutorial!
Welcome to the web tutorial!
Welcome to the web tutorial!
Welcome to the web tutorial!
Welcome to the web tutorial!
...```

## for statement

Python for loops can iterate over any sequence of items, such as a list or a string.

The general format of the

for loop is as follows:

for <variable> in <sequence>: <statements> else: <statements>

Python loop loop instance:

## Instance

>>>languages = [" C", " C++", "Perl" , "Python" ] >>> for x in languages: ... print (x) ... C C++ Perl Python >>>

The break statement is used in the following for instance, and the break statement is used to jump out of the current loop body.：

## Instance

#!/usr/bin/python3 sites = ["Yahoo", "Google","Welookups","Javacodegeeks"] for site in sites: if site == "Welookups": print("web tutorial!") break print("Cyclic data " + site) else: print("No loop data!") print("Complete cycle!")

After executing the script, loop to "Welookups"Will jump out of the loop body：

```Cyclic data Yahoo
Cyclic data!
Cyclic data!```

## range()function

If you need to iterate over a sequence of numbers, you can use the built-in range() function. It generates a sequence of numbers, for example:

## Instance

>>>for i in range(5): ... print(i) ... 0 1 2 3 4

You can also use range to specify the value of the interval.：

## Instance

>>>for i in range(5,9) : print(i) 5 6 7 8 >>>

You can also have the range start with the specified number and specify a different increment (or even a negative number, sometimes called 'step size'):

## Instance

>>>for i in range(0, 10, 3) : print(i) 0 3 6 9 >>>

negative number：

## Instance

>>>for i in range(-10, -100, -30) : print(i) -10 -40 -70 >>>

You can combine the range() and len() functions to traverse the index of a sequence as follows:

## Instance

>>>a = ['Google', 'Yahoo', 'Welookups', 'Javacodegeeks', 'QQ'] >>> for i in range(len(a)): ... print(i, a[i]) ...0 Google 1 Yahoo 2 Welookups 3 Javacodegeeks 4 QQ >>>

welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.