Mayungo's Python Learning Episode 7: I tried printing with if, elif, else

This time we will work on if, elif, else.

The same content is also published in the video, so please have a look if you like.

When writing a program, there are situations where you want to make different operations depending on the conditions.

Now put 1 in the variable num.

If num is 1, 1 is output. When they are the same, they are written as ==.

num = 1

if num == 1:
    print(1)

1

I haven't touched on the case where num is other than 1, so if you set it to 2, nothing will come out.

Create another condition to output the condition of 2 as it is at the time of 1.

Here we use elif.

num = 2

if num == 1:
    print(1)
elif num == 2:
    print(2)

2

There is no problem in writing any number of elif.

num = 3

if num == 1:
    print(1)
elif num == 2:
    print(2)
elif num == 3:
    print(3)

3

However, if you write one by one, it may not be sharp.

At that time, use else.

num = 4

if num == 1:
    print(1)
elif num == 2:
    print(2)
elif num == 3:
    print(3)
else:
    print("others")

others

By doing this, the same operation will be performed uniformly under the conditions not mentioned by if or elif.

Up to this point, we used numbers, but of course we can also use letters.

At this time, the rules such as the principle of if, elif, else and == remain the same.

ans = "yes"

print("Thank you for subscribing to the channel")
if ans == "yes":
    print("thanks")
else:
    print("please")

Thank you for subscribing to the channel
thanks

If, elif, else will come out frequently in the future, so let's get used to it now.

Click here for a list of each story.

Recommended Posts

Mayungo's Python Learning Episode 7: I tried printing with if, elif, else
Mayungo's Python Learning Episode 1: I tried printing with print
Mayungo's Python Learning Episode 3: I tried to print numbers with print
Mayungo's Python Learning Episode 8: I tried input
Mayungo's Python Learning Episode 2: I tried to put out characters with variables
Mayungo's Python Learning Episode 5: I tried to do four arithmetic operations with numbers
Mayungo's Python Learning Episode 6: I tried to convert a character string to a number
I tried fp-growth with python
I tried scraping with Python
I tried scraping with python
I tried web scraping with python.
I tried running prolog with python 3.8.2.
I tried SMTP communication with Python
I tried learning LightGBM with Yellowbrick
[Episode 2] Beginners tried Numeron AI with python
[Episode 3] Beginners tried Numeron AI with python
I tried scraping Yahoo News with Python
I tried learning with Kaggle's Titanic (kaggle②)
I tried sending an email with python.
I tried non-photorealistic rendering with Python + opencv
I tried a functional language with Python
[Episode 0] Beginners tried Numeron AI with python
I tried recursion with Python ② (Fibonacci sequence)
[Episode 1] Beginners tried Numeron AI with python
Mayungo's Python Learning Episode 4: I tried to see what happens when numbers are treated as letters
#I tried something like Vlookup with Python # 2
I tried "smoothing" the image with Python + OpenCV
I tried "differentiating" the image with Python + OpenCV
I tried L-Chika with Raspberry Pi 4 (Python edition)
I tried using mecab with python2.7, ruby2.3, php7
I tried function synthesis and curry with python
I tried to output LLVM IR with Python
I tried "binarizing" the image with Python + OpenCV
I tried running faiss with python, Go, Rust
I tried to automate sushi making with python
I tried playing mahjong with Python (single mahjong edition)
I started machine learning with Python Data preprocessing
I tried running Deep Floor Plan with Python 3.6.10.
I tried sending an email with SendGrid + Python
Mayungo's Python Learning Episode 9: Gold Ax and Silver Ax
[Mac] I tried reinforcement learning with OpenAI Baselines
I tried speeding up Python code including if statements with Numba and Cython
Learning Python with ChemTHEATER 03
I tried to make a real-time sound source separation mock with Python machine learning
Learning Python with ChemTHEATER 05-1
I tried to implement Minesweeper on terminal with python
I tried to get started with blender python script_Part 01
I tried to touch the CSV file with Python
I tried Python> autopep8
[OpenCV / Python] I tried image analysis of cells with OpenCV
I tried to solve the soma cube with python
I tried to get started with blender python script_Part 02
I tried to implement an artificial perceptron with python
Learning Python with ChemTHEATER 02
I tried deep learning
Learning Python with ChemTHEATER 01
I tried to automatically generate a password with Python3
I tried to solve the problem with Python Vol.1
I tried to analyze J League data with Python
Code review with machine learning Amazon Code Guru now supports Python so I tried it
I tried Python> decorator