I didn't know how to use the [python] for statement

for statement

In the for statement, often include an if statement (exception handling, etc.) + break. At that time, I would like to know if it was break or ended normally (it goes around all the elements). When it ends with break, you can write the process in the if statement. On the contrary, how to write the process when all the elements are visited?

Reference URL: http://docs.python.jp/3.4/reference/compound_stmts.html

When a break statement is executed in the first suite, it exits the loop without executing the suite in the else clause. When the continue statement is executed in the first suite, it skips the execution of the remaining statements in the suite and moves on to the next element, or if there are no more elements, it goes to the else clause. I will move.

The suite here is the processing in the for statement (probably) If you don't look at everything when you learn the basics, you will overlook this. If you use else when the for statement ends, it will be executed when it ends normally. How to use is the same as if-else.

sample

The code below is a code that outputs whether the num_list contains a string (useless) The first list I passed for testing included a string.

check_num.py


import random

def num_check(num_list):
    print(repr(num_list))
    for i in num_list:
        if type(i) == str:
            print('Contains characters')
            break
    else: print('Does not contain characters.')

if __name__ == '__main__':
    num_check([random.randint(-1, 100) if i != 5 else str(i)  for i in range(10)])
    num_check([random.randint(-1, 100) for _ in range(10)])

output

output.


$ python check_num.py 
[78, 3, 2, 82, 52, '5', 20, 41, 66, 98]
Contains characters.
[96, 10, 92, 72, 95, 9, 64, 60, 92, 77]
Does not contain characters.

Summary

If you use else, you can process when it ends normally. By the way, even in the while statement, the processing when the loop is exited in the first conditional statement can be expressed using else in the same way.

Recommended Posts

I didn't know how to use the [python] for statement
[Introduction to Python] How to use the in operator in a for statement?
python I don't know how to get the printer name that I usually use.
[python] How to use the library Matplotlib for drawing graphs
I didn't know the basics of Python
[Python] Organizing how to use for statements
How to use "deque" for Python data
How to use the C library in Python
How to use MkDocs for the first time
[Algorithm x Python] How to use the list
[Python] How to use the for statement. A method of extracting by specifying a range or conditions.
How to use the generator
[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 ()
How to use the decorator
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
[BigQuery] How to use BigQuery API for Python -Table creation-
[For beginners] How to use say command in python!
I tried to summarize how to use matplotlib of python
How to know the current directory in Python in Blender
How to use the Raspberry Pi relay module Python
[Python] How to use the graph creation library Altair
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
I tried to summarize how to use pandas in python
[Introduction to Udemy Python3 + Application] 30. How to use the set
How to use the model learned in Lobe in Python
I want to use the R dataset in python
[Introduction to Python] How to get the index of data with a for statement
I tried to simulate how the infection spreads with Python
Python: How to use async with
How to use the zip function
[Python] I want to use only index when looping a list with a for statement
How to use the optparse module
How to use the __call__ method in a Python class
[Hyperledger Iroha] Notes on how to use the Python SDK
How to use Requests (Python Library)
How to use SQLite in Python
How to get the Python version
How to define multiple variables in a python for statement
How to use machine learning for work? 03_Python coding procedure
[Python] I want to use the -h option with argparse
[Python] How to import the library
[Python] How to use list 3 Added
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
Tips for Python beginners to use the Scikit-image example for themselves
I want to know the features of Python and pip
How to use python zip function
How to use the ConfigParser module
[Python] How to use Typetalk API
I tried to summarize how to use the EPEL repository again