Python control syntax (memories)

A memorandum around Python's control syntax. We will make additions and corrections as needed.

Comparison operator

operator Explanation
A == B A and B are equal
A != B A and B are not equal
A < B A is smaller than B
A > B A is greater than B
A <= B A is less than or equal to B
A >= B A is B or higher
A in [LIST] [LIST]There is A in
A not in [LIST] [LIST]There is no A in
A is None A is None
A is not None A is not None
Condition A and condition B Satisfy both condition A and condition B
Condition A or Condition B Satisfy either condition A or condition B

if

num = 5
if num == 0:
    print('The number is 0')
elif num < 0:
    print('Number is less than 0')
else:
    print('Number is greater than 0')

while

limit = input('Enter:') #Accept input
count = 0
while True:
    if count >= limit:
        break #If count is 10 or more, exit the loop

    if count == 5:
        count += 1
        continue #If count is 5, go to the next loop

    print(count)
    count += 1
else: #Execute when the loop ends without breaking
    print('Done')

for

for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
    if i == 5: #If i is 5, go to the next loop
        continue:

    if i == 8: #If i is 8, exit the loop
        break:

    print(i)
#Execute when the loop ends without breaking
else:
    print('Done')

#When you want to process a specified number of times but do not need to retrieve the value_To use
for _ in range(10):
    print('hello')

#Process until it exceeds 10 by skipping 2 to 3
for i in range(2, 10, 3):
    print('hello')

#If you also want to get the index
for i, animal in enumerate(['dog', 'cat', 'bird']):
    print(i, animal)

#When you want to expand and get multiple lists at the same time
animals = ['dog', 'cat', 'bird']
foods = ['meat', 'fish', 'bean']
for animal, food in zip(animals, foods):
    print(animal, food)

#Dictionary loop processing
data = {'x': 10, 'y': 20}
for k, v in d.items():
    print(k, ':', v)

Recommended Posts

Python control syntax (memories)
Python control syntax, functions (Python learning memo ②)
with syntax (Python)
Install Python Control
Python syntax-control syntax
[Python] Chapter 05-02 Control Syntax (Combination of Conditions)
[Python of Hikari-] Chapter 05-06 Control Syntax (Basics of Comprehension)
About Go control syntax
[Python of Hikari-] Chapter 05-08 Control syntax (while statement-another iterative syntax-)
[Python] Chapter 05-01 Control syntax (comparison operator and conditional branching)
[Python of Hikari-] Chapter 05-05 Control syntax (for statement-multiple loops-)
Instrument control using Python [pyvisa]
#python Python Japanese syntax error avoidance
[ev3dev × Python] Single motor control
[Python of Hikari-] Chapter 05-10 Control syntax (interruption and continuation of iteration)
[Python of Hikari-] Chapter 05-04 Control syntax (for statement-use of range function-)
[Python of Hikari-] Chapter 05-07 Control syntax (conditional branching of comprehension notation)
Virtual Environment Version Control Summary Python
[ev3dev × Python] Control of multiple motors
Python
New syntax for Python 3.8 [assignment formula]
[Python of Hikari-] Chapter 05-03 Control syntax (for statement-extracting elements from list-)
Study from Python Hour2: Control statements
Compiler in Python: PL / 0 syntax tree
[ev3dev × Python] Display, audio, LED control
Try frequency control simulation with Python
Exclusive control with lock file in Python
Differences in syntax between Python and Java
I tried to touch Python (basic syntax)
[ev3dev × Python] SSH Control (remote control with keyboard)
Differences between Ruby and Python (basic syntax)
When Python "Syntax Error: Non-ASCII character ~" appears
SublimeText2 and SublimeLinter --Syntax check for Python3--