Program processing is a combination of "sequential / branch / iteration". Programming languages generally execute statements written from above in sequence, By changing the processing content according to the value of the variable, more complicated processing can be performed by the program. You can run it.
The structure of the if statement, which is the basis of branch processing, is as follows. If the "condition" is correct, the "process" is performed.
** if condition: processing**
Indent the line that describes "processing" in the if statement.
** if condition: processing**
When written this way, unlike other programming languages, Python It returns an error similar to the following: IndentationError: expected an indented block
if_example.py
a = 1
if a == 1:
print("a is 1")
The above program will output "a is 1". If a value other than 1 is stored in the variable a, the conditions after if are not satisfied, so Nothing is output.
If the "condition" is satisfied, "process A" is executed. If not, "process B" is executed.
** if condition: Process A else : Processing B **
else_example.py
a = 1
if a == 1:
print("a is 1")
else:
print("a is not 1")
The above program will output "a is 1". If a value other than 1 is stored in the variable a, "a is not 1" will be output.
"Process A" when "Condition A" is satisfied, "Process B" when "Condition A" is not satisfied and "Condition B" is satisfied. If none of the "conditions" are met, "Process Z" is executed. You can write multiple elif statements.
** if condition A: Process A elif condition B Process B else : Processing Z **
elif_example.py
a = 1
if a == 1:
print("a is 1")
elif a == 2:
print("a is 2")
elif a == 3:
print("a is 3")
else:
print("a is not 1,2,3")
The above program will output "a is 1". If 2 is stored in the variable a, "a is 2" is output. If 3 is stored in the variable a, "a is 3" is output. If a value other than 1,2,3 is stored, "a is not 1,2,3" is output.
Many of the "conditions" in if statements determine * a value comparison and a value * to determine whether the condition is met or not. Here are the operators used in this comparison.
Comparison operator | Description |
---|---|
== | equal |
!= | Not equal |
>= | that's all |
<= | Less than |
> | Greater |
< | Smaller |
Since the meaning of the symbols used in arithmetic is the same, detailed explanation is omitted, but only the operator of the equal sign is "==" instead of "=". Also, cross-type comparisons are not equivalent. You can compare integers with zero decimal places (1 == 1.0 returns True), 1 == "1" returns False.
You can combine multiple "conditions" to create more complex "conditions".
and_or_not_example.py
a = 1
b = ['a','b','c']
if not (a == 1 and 'z' in b) or len(b) != 3 :
print("ok!")
This program was forcibly written to explain the functions of and, or, not, Do you know what you are doing? The correct answer is ** (a is 1 and'z'exists in b) Does not meet the conditions on the left, or the number of elements in b is not 3 **.
Values that Python treats as False
The None type is a special type that indicates "there is no value". Use is None (or is not None) to determine if it is None.
none_explain.py
x = None
if x is None:
print("x is none")
y = False
if y is not None:
print("y is not none")
Note that False is not None.
Next: Python Basic Course (9 iterations)
Recommended Posts