[Python] Iterative processing (for, while)

for statement (for lists)

** for variable name in list: **

fruits = ["apple" , "banana" , "strawberry"]

for fruit in fruits:
    print("What is your favorite food" + fruit + "is")

Output result My favorite food is apple My favorite food is banana My favorite food is strawberry


for statement (dictionary)

** for variable name in dictionary: **

fruits = {"apple":"Apple" , "banana":"banana" , "strawberry":"Strawberry"}

for fruit_key in fruits:
    print(fruit_key + "Is in Japanese" + fruits[fruit_key] + "is")

Output result apple is an apple in Japanese banana is Japanese banana strawberry is Japanese strawberry


while statement

** while conditional expression: **

x = 1

while x <= 5:
  print(x)
  x += 1

Output result 1 2 3 4 5


break End the iterative process Use with conditional expressions

numbers = [1 , 2 , 3 , 4 , 5]
for number in numbers:
    print(number)
    if number == 3:
       break

In the above case, the process ends when the number reaches 3. Output result 1 2 3


continue Skip conditional expression processing

numbers = [1 , 2 , 3 , 4 , 5]
for number in numbers:
    print(number)
    if number % 2 == 0:
       continue

Skip processing when divisible by 2 Output result 1 3 5

Recommended Posts

[Python] Iterative processing (for, while)
Introduction to Python For, While
Python iterative
Python Exercise for Beginners # 2 [for Statement / While Statement]
Personal notes for python image processing
2016-10-30 else for Python3> for:
python [for myself]
python image processing
Python file processing
[Python] Script useful for Excel / csv processing
2015-10-26 python> Processing every second> while True: /time.sleep (1)
VBA user tried using Python / R: Iterative processing
Image processing? The story of starting Python for
Image Processing with Python Environment Setup for Windows
About Python for loops
Python distributed processing Spartan
Python basics ② for statement
File processing in Python
Python: Natural language processing
Communication processing by Python
Multithreaded processing in python
First Python image processing
About Python, for ~ (range)
python textbook for beginners
Queue processing in Python
Refactoring tools for Python
python for android Toolchain
Image processing with Python
[Python] Iterative processing_Personal memorandum
OpenCV for Python beginners
Python string processing illustration
Various processing of Python
Install Python (for Windows)
[Python] for statement error
Python environment for projects
[Introduction to Python] How to use while statements (repetitive processing)
[Python of Hikari-] Chapter 05-08 Control syntax (while statement-another iterative syntax-)
[Python] Measures and displays the time required for processing
3. Natural language processing with Python 4-1. Analysis for words with KWIC
Building an environment for natural language processing with Python
A Java programmer studied Python. (for, if, while statement)
Image processing with Python (Part 2)
100 Language Processing with Python Knock 2015
UTF8 text processing in python
Python memo (for myself): Array
python3 Measure the processing speed.
About Fabric's support for Python 3
Python list, for statement, dictionary
"Apple processing" with OpenCV3 + Python3
Python for Data Analysis Chapter 4
Modern Python for intermediate users
Python 3.6 installation procedure [for Windows]
BigQuery integration for Python users
Set Up for Mac (Python)
Acoustic signal processing with Python (2)
Acoustic signal processing with Python
Python Tkinter notes (for myself)
OpenCV3 installation for Python3 @macOS
Python code memo for yourself
[Python] xmp tag for photos
Asynchronous processing (threading) in python