The basics of programming are "conditional branching" and "repetition". Use a for statement or while statement to repeat, and use an if statement for conditional branching. In other languages, there is also a function called a switch statement, but Python simply provides only an if statement.
table of contents 1 [What is a Python if statement? ](## What is a Python if statement?) 2 [Let's actually use the if statement](## Let's actually use the if statement) 3 [How to write multiple conditional branches](## How to write multiple conditional branches) 4 [About operators](## About operators) 4.1 [Comparison operator](### Comparison operator) 4.2 [Logical Operators](### Logical Operators)
if condition:
Process A
Process B
Process C
It is the format. Process A and Process B are executed only when the condition is True. The processing performed in the case of a condition must be indented. Process C, which is not indented, is executed regardless of whether the condition is True or False. If it is a "condition", it is okay to remember it like English, "do the indented processing immediately below".
your_age = 23
print('Age confirmation')
if your_age >= 20:
print('Over 20 years old')
print('Age confirmation finished')
This code splits into two types depending on the value of your_age. (1) When your_age is 20 or more
python
Age confirmation
Over 20 years old
Age confirmation
End
Is displayed.
(2) When your_age is less than 20
Age confirmation
Age confirmation
End
It will be displayed.
This time, conditional branching is an example of multiple.
your_age = 41
if your_age >= 40:
print('Forties')
elif your_age >= 30:
print('30s')
elif your_age >= 20:
print('20's')
In this example, "40s" is displayed. If you want to write some conditions, continue with "elif" like this. (Elif means else if. Think of it like "or ...")
Furthermore, it is possible to perform processing only when none of the conditions are met.
your_age = 15
if your_age >= 40:
print('Forties')
elif your_age >= 30:
print('30s')
elif your_age >= 20:
print('20's')
else:
print('Minors')
One thing to note is that if statements check the conditions in order from the top, and if you enter one of them, the other elif and else will not be executed. So, in the code below, "your_age> = 20" will be True, and it will end up being displayed as "20's".
your_age = 50 #50 years old!
if your_age >= 20:
print('20's') #I will come here!
elif your_age >= 30:
print('30s')
elif your_age >= 40:
print('Forties')
else:
print('Minors')
I used the symbols "> =" and "==", which are called comparison operators. The following can all be described as conditional expressions in if statements.
operator | display |
---|---|
A==B | True if A and B are equal |
A != B | True if A and B are not equal |
A >= B | True if A is greater than or equal to B(True if A and B are the same) |
A > B | True if A is greater than B(False if A and B are the same) |
A <= B | True if A is less than or equal to B(True if A and B are the same) |
A < B | True if A is less than B(False if A and B are the same) |
A in B | True if B contains A (this is a bit special, often used when B is a list, A is a string, etc.) |
There are also things called logical operators. This can also be described as a conditional expression in the if statement.
operator | display |
---|---|
A and B | True if A and B are True |
A or B | True if either A or B is True |
not A | True if A is False |
It is no exaggeration to say that the if statement is the most important in programming. I hope this article will help you understand.
Reference site: [Introduction to Python] How to write conditional branches using if statements
Recommended Posts