[Introduction to Python] How to write conditional branches using if statements

[Introduction to Python] How to write conditional branches using if statements

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)

What is a Python if statement?

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".

Let's actually use the if statement

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.

How to write multiple conditional branches

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')

About operators

Comparison operator

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.)

Logical operator

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

[Introduction to Python] How to write conditional branches using if statements
[Introduction to Python] How to write repetitive statements using for statements
[Introduction to Python] How to use while statements (repetitive processing)
[Introduction to Python] How to stop the loop using break?
How to install python using anaconda
How to write a Python class
[Introduction to Python] How to parse JSON
[Python] How to write an if statement in one sentence.
Difference in how to write if statement between ruby ​​and python
[Introduction to Python] How to use class in Python?
[Introduction to Udemy Python3 + Application] 33. if statement
Introduction to Discrete Event Simulation Using Python # 1
How to write Python document comments (Docstrings)
[Python] Organizing how to use for statements
Introduction to Discrete Event Simulation Using Python # 2
How to write Ruby to_s in Python
[Introduction to Python] How to judge authenticity with if statement (True and None)
[Introduction to Python] How to write a character string with the format function
Summary of how to write if statements (Scala, Java, Rust, C language, C ++, Go language, PHP, Perl, Python, Ruby)
[Introduction to Udemy Python3 + Application] 23. How to use tuples
[Introduction to Python] How to handle JSON format data
How to make Substance Painter Python plugin (Introduction)
How to install Python
How to install python
Introduction to Python language
Introduction to OpenCV (python)-(2)
20th Offline Real-time How to Write Problems in Python
How to write a GUI using the maya command
How to set up a Python environment using pyenv
How to write string concatenation in multiple lines in Python
[Introduction to Python] How to iterate with the range function?
How to auto-submit Microsoft Forms using python (Mac version)
How to write a list / dictionary type of Python3
How to make a Python package using VS Code
[Python] How to write a docstring that conforms to PEP8
Write data to KINTONE using the Python requests module
How to exit when using Python in Terminal (Mac)
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
[Introduction to Udemy Python3 + Application] 30. How to use the set
How to retrieve multiple arrays using slice in python.
How to execute a command using subprocess in Python
How to write faster when using numpy like deque
[Technical book] Introduction to data analysis using Python -1 Chapter Introduction-
[2020.8 latest] How to install Python
How to install Python [Windows]
[python] How to check if the Key exists in the dictionary
Introduction to Python Django (2) Win
python3: How to use bottle (2)
Offline real-time how to write Python implementation example of E14
XPath Basics (2) -How to write XPath
[Python] How to use list 1
How to update Python Tkinter to 8.6
[Circuit x Python] How to solve circuit equations symbolically using sympy
How to transpose a 2D array using only python [Note]
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
Post to Twitter using Python
How to use Python argparse
Start to Selenium using python
Introduction to serial communication [Python]
Python: How to use pydub
[Python] How to use checkio