In the previous section, we touched on branching by if statement using one condition. For example:
>>> 5 == 5
True
>>> 5 != 5
False
>>> 5 != 4
True
>>> 5 > 4
True
>>> 5 >= 5
True
>>> 5 > 5
False
However, there are some more complicated conditions. For example, "when a and b are equal and c and d are also equal". I will touch on the combination of these conditions.
If condition A and condition B are met and ** both are met ** (that is, both are ** True **), then ** True **.
Enter the following code in the ** Python Console **.
>>> a = 1
>>> b = -1
>>> a > 0
True
>>> b > 0
False
>>> a > 0 and b > 0
False
In the above, ** a> 0 ** meets the condition, but ** b> 0 ** does not. Therefore, ** a> 0 and b> 0 ** becomes ** False ** because ** both are not met **.
Currently, only two conditions are listed, but even if the AND operation is performed on the three conditions, it will be ** True ** if the three conditions are met.
The table below summarizes the AND operation.
Condition A | Condition B | Condition AandCondition B |
---|---|---|
True | True | True |
True | False | False |
False | True | False |
False | False | False |
When there are condition A and condition B, if either ** is satisfied ** (that is, either is ** True **), it will be ** True **.
Enter the following code in the ** Python Console **.
>>> a = 1
>>> b = -1
>>> a > 0
True
>>> b > 0
False
>>> a > 0 or b > 0
True
In the above, ** a> 0 ** meets the condition, but ** b> 0 ** does not. However, in the case of OR operation, ** a> 0 or b> 0 ** becomes ** True ** because it is only necessary to satisfy either **.
The table below summarizes the OR operation.
Condition A | Condition B | Condition AandCondition B |
---|---|---|
True | True | True |
True | False | True |
False | True | True |
False | False | False |
Used when ** denies it ** when condition A is present.
Enter the following code in the ** Python Console **.
>>> a = 1
>>> a > 0
True
>>> not a > 0
False
First, ** a> 0 ** is ** 1> 0 **, so ** True **. However, in the case of NOT operation, it is ** negated **, so the opposite is true, and the result is ** False **.
Condition A | notCondition A |
---|---|
True | False |
False | True |
Now, let's actually create an if statement program that combines conditions. The program of this time is as follows.
Enter the number of months from 1 to 12, "Spring" for March-May, "Summer" for June-August, "Autumn" for September-November, 12 From Monday to February, create a program that displays "It's winter". Also, if a number other than 1 to 12 is entered, please display "The month does not exist".Let's actually create a program. Create a file called 05-02-01.py </ font> in the chap05 </ font> folder and enter the following code.
05-02-01.py
month = int(input('Please enter the number of months (1-12):'))
if month >= 3 and month <= 5:
print('It's spring')
elif month >= 6 and month <= 8:
print('It's summer')
elif month >= 9 and month <= 11:
print('It's Autumn')
elif month == 12 or month <= 2:
print('It's winter')
else:
print('That month doesn't exist.')
05-02-01.py Execution result
[Execution result] </ font> Please enter the number of months (1-12): 7 </ font> It's summer
Let's explain.
First, enter the number of months, and then the processing in the if statement that meets any of the conditions is executed. Since I entered ** 7 ** this time, it applies to ** month> = 6 and month <= 8 **, so the processing in the if statement is executed and "Summer" is displayed.
Is it possible to change the condition of ** month> = 6 and month <= 8 ** to ** 6 <= month <= 8 **? Such a notation is actually possible. Let's improve the program of 05-02-01.py </ font> as follows and execute it.
.05-02-01.py
month = int(input('Please enter the number of months (1-12):'))
if 3 <= month <= 5:
print('It's spring')
elif 6 <= month <= 8:
print('It's summer')
elif 9 <= month <= 11:
print('It's Autumn')
elif month == 12 or 1 <= month <= 2:
print('It's winter')
else:
print('That month doesn't exist.')
05-02-01.py Execution result
[Execution result] </ font> Please enter the number of months (1-12): 1 </ font> It's winter
If anything, I think that it is easier to see the conditions of the format written in this range. However, the condition to be described in the current range is ** Python only **. Please note that C language and Java language of other languages cannot be written in a range.
We have prepared exercises. Please try to solve it. In addition, please use the file name specified in [] and create it in chap05 </ font>. You can specify any variable name you like. [05-02-p1.py] [1] Enter your height (cm) and weight (kg) and create a program to find your BMI. Also, from the BMI result, please display it on the screen based on the following conditions.
BMI value | Output contents |
---|---|
18.Less than 5 | Outputs "too thin" |
18.5 or more 25.Less than 0 | Output "It's normal weight" |
25.0 or more | Output "I'm obese" |
05-02-p1.py execution result
[Execution result] </ font> Please enter your height (cm): 184 </ font> Please enter your weight (kg): 77.9 </ font> Standard weight
This time, we looked at control syntax that combines conditions. However, if you write too long conditions, it will be difficult to read when you maintain the program later, so keep it to the minimum necessary.
This AND, OR, NOT operation is similar to the set operation that appeared in the set of "Chapter 4-09". I will. Please check it out.