[Introduction to Python] How to use while statements (repetitive processing)

Reference site: [[Introduction to Python] How to use while statement (repetitive processing)] (http://programming-study.com/technology/python-while/)

[Introduction to Python] How to use while statements (repetitive processing)

Not limited to Python, the basics of programming are "conditional branching" and "loop processing". There are two types of conditional branching in Python: if statements, and loop processing: "for statements" and "while statements". This time, I will introduce the while statement.

The while statement is mainly used for processing such as "infinite loop" and "repeat for a certain condition".

table of contents 1 [How to write a while statement](## How to write a while statement) 2 [Try using a while statement](## Try using a while statement) 3 [Try using break](Try using ## break)

How to write a while statement

while conditional expression:
Processing you want to repeat

I will write it as.

As the name suggests, write the condition in the "conditional expression" part. While the conditional expression is evaluated as True, it will endlessly "process what you want to repeat".

For example, in the following cases

while conditional expression:
Process A
Process B

In this case, process A is repeated, and when the conditional expression becomes False, the repetition ends and the process moves to process B. It is only the while block that is repeated.

Try using a while statement

Let's create a while loop that displays 1 to 5.

index = 1
while index <= 5:
    print(index)
    index += 1
print("End")

Execution result


1
2
3
4
5
End

The processing flow is as follows.

In the first line index = 1, substitute 1 for the variable index

The second line while index <= 5: means that the while block is executed while the index is 5 or less (including when it is 5). The current index is 1, so it is 5 or less. Move on to the third line.

Line 3 print (index) Displays the index. Currently, the index contains 1, so 1 is displayed.

Line 4 Add 1 to the index variable. At this point, the value of the index variable is 2.

Go to line 2 The index variable is still 2. Since it is 5 or less, go to the third line again

After this, index + = 1 will continue until it reaches 6.

The second line index is 6, not less than 5. So, instead of going to the 3rd line, jump to the 5th line. Try using break

In a while loop, you can break out of the loop by using a break statement. Let's modify the previous example and do the following.

index = 1
while index <= 5:
    print(index)
    index += 1
    break
print("End")

It is the same up to index + = 1 on the 4th line, but when you reach the break on the 5th line, it immediately breaks out of the loop and moves to the 6th line. So the result is:

Execution result


1
End

up until this point

while conditional expression:
Process A

It is written as, but by using this break

while True:
if conditional expression:
        break
Process A

You will be able to write. While doing an infinite loop with while True, break with the if statement inside and exit the loop. This method is also often used, so be sure to remember it.

Recommended Posts

[Introduction to Python] How to use while statements (repetitive processing)
[Introduction to Python] How to write repetitive statements using for statements
[Introduction to Python] How to use class in Python?
[Python] Organizing how to use for statements
[Introduction to Udemy Python3 + Application] 23. How to use tuples
python3: How to use bottle (2)
[Python] How to use list 1
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
[Python] How to use input ()
[Introduction] How to use open3d
How to use Python lambda
[Python] How to use virtualenv
Introduction to Python For, While
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
[Introduction to Udemy Python3 + Application] 30. How to use the set
Python: How to use async with
[Python] How to use Pandas Series
How to use Requests (Python Library)
How to use SQLite in Python
[Introduction to Python] How to parse JSON
[Introduction to Python] Let's use pandas
[Python] How to use list 3 Added
How to use Mysql in python
How to use OpenPose's Python API
How to use ChemSpider in Python
How to use FTP with Python
Python: How to use pydub (playback)
How to use PubChem in Python
[Introduction to Python] Let's use pandas
How to use python zip function
[Introduction to Python] Let's use pandas
[Python] How to use Typetalk API
[Introduction to Python] How to write conditional branches using if statements
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
[Python] Summary of how to use pandas
How to install and use pandas_datareader [Python]
[python] How to use __command__, function explanation
[Introduction to Python] Let's use foreach with Python
[Python] How to use import sys sys.argv
[Introduction to Python] How to use the in operator in a for statement?
Memorandum on how to use gremlin python
[Python2.7] Summary of how to use unittest
python: How to use locals () and globals ()
How to use __slots__ in Python class
How to use "deque" for Python data
How to use Python zip and enumerate
[Python] Understand how to use recursive functions
Summary of how to use Python list
How to use regular expressions in Python
[Python2.7] Summary of how to use subprocess
How to use is and == in Python
[Blender x Python] How to use modifiers
[Introduction to Python] How to use the Boolean operator (and ・ or ・ not)
[Question] How to use plot_surface of python
How to use the C library in Python
[Python] How to use two types of type ()