Explains how to use for statements, which are often used in python, by pattern.
It's often used in complex combinations and looks difficult, but the basic syntax is very simple, just specify two elements.
▼ Supplementary data (“A” above) -Often executed for character strings and numbers. -Often specified by variables. -It also works in the form of directly describing a set of multiple elements such as list and tuple, or a single numerical value or character string.
Take one element of A and store it in a variable ("a" in the above). Repeat this ** for the number of elements **.
Every time one element is taken out, ** the process described in the second and subsequent lines is executed **.
-Retrieve the elements of list (number, character string) ・ Used for tuple type and set type ・ Extract the numerical value with the range function -Specify extraction conditions with an if statement
Examples of numbers and strings, variable specifications and direct description.
Extracting the numeric elements of a variable
list = [1,2,3]
for a in list:
print(a)
#output
1
2
3
Extracting a string element of a variable
list = ["AAA","BBB","CCC"]
for a in list:
print(a)
#output
AAA
BBB
CCC
Specify numerical value directly
for a in [1,2,3]:
print(a)
#output
1
2
3
Specify character string directly
for a in ["AAA","BBB","CCC"]:
print(a)
#output
AAA
BBB
CCC
Tuple type
tuple = "AAA","BBB", "CCC"
tuple #output:('AAA', 'BBB', 'CCC')
type(tuple) #Output: tuple
for a in tuple:
print(a)
#output
AAA
BBB
CCC
Set type
set = {1,1,2,2,3,3,3,4}
set #output:{1, 2, 3, 4}
type(set) #Output: set
for a in set:
print(a)
#output
1
2
3
4
Note: Click here for a description of tuples (https://qiita.com/yuta-38/items/e2306af6a378430ec873)
python
for a in range(5):
print(a)
#output
0
1
2
3
4
python
for a in list(range(0,8,2)):
print(a)
#output
0
2
4
6
** ▼ Supplement: About the range function ** A function that specifies "initial value", "end value of range", and "change amount" and stores the corresponding numerical value in range type.
Click here for details on the range function [https://qiita.com/yuta-38/items/408dba0df6264878a83b)
It is possible to specify the target range by using slices.
** ▼ What is a slice? ** ** A type of range specification method such as list. Notation to specify the range with [] and :.
** ▼ Slice basic syntax **
[a:b:c]
└ "a": Starting sequence number
└ "b": Ending sequence number (less than)
└ "c": Change syntax
b and c can be omitted
Click here for a detailed explanation of the slices (# https://qiita.com/yuta-38/items/0d5c55b748d10f83af53)
** Example When extracting the third and subsequent elements **
[2:]
└ Element counts from 0
Specify the number to start
list = [1,2,3,4,5,6,7,8,9]
for a in list[2:]:
print(a)
#output
3
4
5
6
7
8
9
** Example Extract the 5th to 8th elements **
[4:8]
└ Element counts from 0
└ Array number 4 is the 5th element
└ Array number 8 is the 9th element
└ Sequence number 8 is not included (end of processing)
Specify the number to start
list = [1,2,3,4,5,6,7,8,9]
for a in list[4:8]:
print(a)
#output
5
6
7
8
** Example Extract the value obtained by adding 3 by 3 from 50 to 70 **
[50:71:3]
└ 50th sequence number
└ Ends at the 71st sequence number (does not include the 71st value)
└ Change amount: Increase by 3
Use range (1,100)
└ Integer from 1 to 99
Specify the range to be extracted and the amount of change
for a in range(1,100)[50:71:3]:
print(a)
#output
51
54
57
60
63
66
69
For the if statement, click here (https://qiita.com/yuta-38/items/f974272a676d8171f4b3)
** ▼ Example of combination of for statement and if statement ① **
Extract only even numbers from the numbers 1-9. (10 not included)
Take out only even numbers
for a in range(1,10):
if a%2 == 0:
print(a)
#output
2
4
6
8
└ "%" remainder └ "a% 2 == 0" The remainder of dividing a by 2 is 0 (= even)
** ▼ Example of combination of for statement and if statement ② ** From the numerical values from 1 to 100, the numerical values that are even numbers, 90 or more, and do not include 100 are extracted by list type. (101 not included)
Combination of comparison operators
list = []
for a in range(1,101):
if a%2 == 0 and a >= 90 and not a == 100:
list.append(a)
list
#output
# [90, 92, 94, 96, 98]
Basic syntax of break statement (in if statement)
if A:
break
If condition A, processing is interrupted.
Practical example of break statement
for a in range(1,20):
if a%2 == 0:
if a > 10:
break
print(a)
#output
2
4
6
8
10
Basic syntax of continue statement (in if statement)
if A:
continue
Condition A skips processing. Others are executed.
Practical example of continue statement
for a in range(0,5):
if a == 3:
continue
print(a)
#output
0
1
2
4
** ▼ Practical example of continue statement ② ** Skip even numbers from 0 to 10 and extract. (Extract only odd numbers)
Practical example of continue statement ②
for a in range(0,11):
if a%2 ==0 :
continue
print(a)
#output
1
3
5
7
9
The process does not change whether it is described or not. ** Described to clearly indicate that it will not be processed.
Basic syntax of pass
if A:
AAA
else:
pass
If the condition is "A", the process "AAA" is executed. Otherwise, do nothing.
▼ The process is the same as below.
python
if A:
AAA
** ▼ (Original processing) Processing to extract even numbers from 1 to 10 **
Original processing
list = []
for a in range(1,11):
if a%2 == 0:
list.append(a)
list
#output
[2, 4, 6, 8, 10]
There is a pass ①
list = []
for a in range(1,11):
if a%2 == 0:
pass
list.append(a)
list
#output
[2, 4, 6, 8, 10]
There is a pass ②
list = []
for a in range(1,11):
if a%2 == 0:
list.append(a)
pass
list
#output
[2, 4, 6, 8, 10]
There is a pass ③
list = []
for a in range(1,11):
pass
pass
if a%2 == 0:
pass
pass
pass
list.append(a)
pass
list
#output
[2, 4, 6, 8, 10]
pass error ①
list = []
for a in range(1,11):
if a%2 == 0: pass
list.append(a)
list
#output
# IndentationError: unexpected indent
pass error ②
list = []
for a in range(1,11):
if a%2 == 0:
pass pass
list.append(a)
list
#output
# SyntaxError: invalid syntax
Recommended Posts