First Python 3 ~ First comparison ~

Introduction

Please refer to here for notes.

Conditional branch by If statement

import random
x = random.randint(1, 3) #Substitute a random number from 1 to 3

if x == 1:
    print("This is 1") #When the conditional expression is satisfied

elif x == 2:
    print(“This is 2”) #Processing when conditional expression 2 is satisfied
    
else:
    print("This is not 1 or 2") #When the conditional expression is not satisfied

・ Don't forget the: (colon) in the if statement An error will occur. (I often forget) ・ Not elsif It's easy to get confused because some languages use elsif.

Indent

Indenting the beginning of a line of code is called "indentation". In Python, enter one tab or four half-width spaces in the indent of one row.

#abridgement
if x == 2000:
    print("2000 points")

In Python, it starts with a ":" (colon) after the conditional expression and a ":" (colon) after the else. Treat until the end of the indent line as a block.

Indentation, in Python, represents the process to be executed when a conditional expression is satisfied, and at the same time It makes it easier for programmers to distinguish the processing to be executed according to the conditional expression.

Comparison operator

List

Example meaning Example to be true
a < b a is less than b 10 < 15
a > b a is greater than b 10 > 7
a <= b a is less than or equal to b 10 <= 15
a >= b a is greater than or equal to b 10 >= 7
a != b a and b are not equal 10 != 1

Example sentence

Compare whether you can drink

import random
age = random.randint(18, 22)    #How old are 18 in age~Randomly assigned in the range of 22
text = "" 
if age < 20:
    text = "No drinking"
else:
    text = "Drinkable"    
print(str(age) + "Talent" + text)

Recommended Posts

First Python 3 ~ First comparison ~
First time python
First time python
First Python ~ Coding 2 ~
First python [O'REILLY]
First Python 3rd Edition
PyQ ~ Python First Steps ~
First Python image processing
[Python] Python / Scikit-learn's first SVM
First Python miscellaneous notes
Python package manager comparison
[Python] Chapter 01-01 About Python (First Python)
First Fabric (Python deployment tool)
Python, Java, C ++ speed comparison
Python
Null object comparison in Python
First neuron simulation with NEURON + Python
Comparison of 4 Python web frameworks
About Python string comparison operators
Python 3 sorted and comparison functions
Python Note: About comparison using is
Java and Python basic grammar comparison
First simple regression analysis in Python
First Python 3 ~ The beginning of repetition ~
Web scraping with Python First step
First Python 3 ~ Extra: Numerical Random Fantasy ~
[GUI with Python] PyQt5-The first step-
Prepare your first Python development environment
Python (from first time to execution)
C / C ++ programmer challenges Python (first step)
See python for the first time
Python executable file conversion module comparison 2
Speed comparison of Python XML parsing
The first step in Python Matplotlib
kafka python
Generate a first class collection in Python
Python basics ⑤
python + lottery 6
Python Summary
Built-in python
Python technique
Studying python
Python 2.7 Countdown
Python memorandum
python tips
First Flask
python function ①
Python basics
Python memo
ufo-> python (3)
Closure 4 language comparison (Python, JavaScript, Java, C ++)
Continuously play the first Python Sukusta MV
Python comprehension
install python
Python Singleton
(Java, JavaScript, Python) Comparison of string processing
Python basics ④
Python Memorandum 2
python memo
Python Jinja2
Comparison of Japanese conversion module in Python3