[PYTHON] Functions that can be used in for statements

Functions that can be used in for statements

Python uses a for statement to iterate over. If you want to perform processing such as repeating by specifying the number of times, combine it with the range function.

range () function Returns a series of numbers starting at 0 and incrementing or decrementing by 1 (default), stopping before the specified number. range (start, end, step)

How to use Description
start An integer that specifies the starting position. The default is 0.
End An integer that specifies the post-end position.
Step An integer that specifies increment and decrement. The default is 1.

Start An integer that specifies the start position. The default is 0. End An integer that specifies the position after the end. Step Increment, an integer that specifies decrement. The default is 1.

range (start, end, step)

for f in renge(5)

print (f, end = ‘ ’)

01234 Since the value specified for "End" is not included, 4 is returned. If you want to get up to 5, you can specify 6.

range(1, 5, 2)

range(10, 5, -2)

print(list(range(3))) With this form, it seems that you can get it as a list type.

Loop counter I'm ashamed to say that I didn't know the meaning of the loop counter, so I looked it up. A variable that controls the loop. Used when the end condition of the iterative process is the number of processes, the loop counter is incremented by 1 each time the process is performed, and the number of times the process is performed is counted and controlled. .. The above meaning is called a loop counter.

If you want to use the loop counter in Python, use the enumerate () function.

enumerate()

l = [‘kokugo’, ‘suugaku’, ‘eigo’]

for i, subject in enumerate(l): print(i, subject) 0 kokugo 1 suugaku 2 eigo

If the second argument is specified as shown below, the loop counter is returned from the specified value.

l = [‘kokugo’, ‘suugaku’, ‘eigo’]

for i, subject in enumerate(l, 11): print(i, subject) 11 kokugo 12 suugaku 13 eigo

zip() Loop counters can also be defined using the zip () function

for i , w in zip(['kokugo', 'suugaku', 'eigo'], [78, 82, 54]): print(subject, number) kokugo 78 suugaku 82 eigo 54

It is also possible to define the loop contents with variables as shown below.

subject = ['kokugo', 'suugaku', 'eigo'] number = [78, 82, 54]

for i , w in zip(subject, number): print(subject, number) kokugo 78 suugaku 82 eigo 54

Recommended Posts

Functions that can be used in for statements
Basic algorithms that can be used in competition pros
ANTs image registration that can be used in 5 minutes
Goroutine (parallel control) that can be used in the field
Goroutine that can be used in the field (errgroup.Group edition)
Scripts that can be used when using bottle in Python
A timer (ticker) that can be used in the field (can be used anywhere)
Python standard input summary that can be used in competition pro
++ and-cannot be used for increment / decrement in python
Easy padding of data that can be used in natural language processing
I created a template for a Python project that can be used universally
File types that can be used with Go
Building Sphinx that can be written in Markdown
Mathematical optimization that can be used for free work with Python + PuLP
I wrote a tri-tree that can be used for high-speed dictionary implementation in D language and Python.
A personal memo of Pandas related operations that can be used in practice
Easy program installer and automatic program updater that can be used in any language
I made a familiar function that can be used in statistics with Python
Japanese can be used with Python in Docker environment
Python knowledge notes that can be used with AtCoder
Can be used in competition pros! Python standard library
[Django] About users that can be used on template
Python3> slice copy / slice notation> used in for statements, etc.
Overview and useful features of scikit-learn that can also be used for deep learning
Summary of statistical data analysis methods using Python that can be used in business
File sharing server made with Raspberry Pi that can be used for remote work
Text analysis that can be done in 5 minutes [Word Cloud]
Evaluation index that can be specified in GridSearchCV of sklearn
Can be used with AtCoder! A collection of techniques for drawing short code in Python!
[Django] Field names, user registration, and login methods that can be used in the User model
[Python3] Code that can be used when you want to resize images in folder units
How to set variables that can be used throughout the Django app-useful for templates, etc.-
XPath Basics (3) -Functions often used for XPath
Confirmation that rkhunter can be installed
Make a Spinbox that can be displayed in Binary with Tkinter
33 strings that should not be used as variable names in python
About character string handling that can be placed in JSON communication
New features in Python 3.9 (1)-Union operators can be used in dictionary types
Make a Spinbox that can be displayed in HEX with Tkinter
Python standard module that can be used on the command line
I made it because I want JSON data that can be used freely in demos and prototypes
Understand the probabilities and statistics that can be used for progress management with a python program
[For beginners] Baseball statistics and PyData that can be remembered in 33 minutes and 4 seconds ~ With Dai-Kang Yang
AtCoder C problem summary that can be solved in high school mathematics
Acoustic signal processing module that can be used with Python-Sounddevice ASIO [Application]
Solution to the problem that Ctrl + z cannot be used in Powershell in Docker for windows environment (provisional)
Hide the warning that zsh can be used by default on Mac
Serverless LINE Bot that can be done in 2 hours (source identifier acquisition)
Summary of various for statements in Python
Precautions when using for statements in pandas
Maximum number of function parameters that can be defined in each language
Acoustic signal processing module that can be used with Python-Sounddevice ASIO [Basic]
A story that heroku that can be done in 5 minutes actually took 3 days
[Python3] Code that can be used when you want to cut out an image in a specific size
QPS control that can be used in the field (Rate Limit) Limits execution to n times per second
A memo for making a figure that can be posted to a journal with matplotlib
A class for PYTHON that can be operated without being aware of LDAP
[2015.02.22] Youtube-dl has been updated and can no longer be used in previous versions.
I want to create a priority queue that can be updated in Python (2.7)
If "can not be used when making a PIE object" appears in make
Summary of scikit-learn data sources that can be used when writing analysis articles