[Python] Basic pattern and usage of if statement (comparison operator and Boolean operator)

[Python] Basic pattern and usage of if statement (comparison operator and Boolean operator)

A review of the if statement pattern.


** ■ Table of contents **
  1. [Basic syntax of if statement](# 1 Basic syntax of if statement)
  2. [No conditional branch](#No conditional branch)
  3. [Conditional branching (else only)](#Conditional branching else only)
  4. [Two or more conditional branches (without else)](#Two or more conditional branches without else)
  5. [Two or more conditional branches (with else)](# Two or more conditional branches with else)
  6. [Conditional expression operator](# 2 Conditional expression operator)
  7. [Operator list](# 3 Operator list)
  8. [Example of comparison operator](# 4 Example of comparison operator)
  9. ["==" equal](# equal)
  10. ["! =" Not equal](# Not equal)
  11. [Greater than ">" (not included)](greater than # not included)
  12. ["> =" or more (including)](# or more included)
  13. [Less than "<" (not included)](not including #)
  14. ["<=" Or less (including)](# including below)
  15. [Includes "in" element](includes # element)
  16. [Do not include "not in" element](Do not include # not-in element)
  17. [Equal "is" objects](#Equal objects)
  18. [Example of Boolean operator](# 5 Example of Boolean operator)
  19. ["and" and](# and)
  20. ["or" or](# or)
  21. [Not "not"](not #)

## 1. 1. Basic syntax of if statement There are 4 patterns of if statements, which are combinations of conditional branches "else" and "elif".

① No conditional branch ② One conditional branch. True or False ③ Two or more conditional branches. Processing is executed only under the specified conditions. ④ Two or more conditional branches. Also set the processing in case of false

▼ ① No conditional branch

If the conditions are not met, do nothing.

Pattern 1 (no conditional branching)


if A:
    AAA

"A": Conditional expression "AAA": Processing when A holds

Example (no conditional branch, True)


A = 100

if A == 100:
    print('A is 100.')

#output
#A is 100.

Example (no conditional branch, False)


A = 90

if A == 100:
    print('A is 100.')

#### ▼ ② Conditional branching (else only) Two branches. True or False?

Pattern 2 (else)


if A:
    AAA
else:
    BBB

"A": Conditional expression "AAA": Processing when A holds "Else:": When A does not hold "BBB": Processing when A does not hold

Example (with conditional branching, else)


A = 90

if A == 100:
    print('A is 100.')
else:
    print('A is not 100.')

#output
#A is not 100.

#### ▼ ③ Two or more conditional branches (without else) Executes processing only under the specified conditions. Describe branching conditions and processing with elif. └ Elif = else if (if anything else)

Pattern 3 (elif)


if A:
    AAA
elif B:
    BBB
elif C:
    CCC

"A": Conditional expression "AAA": Processing when A holds "Eli if B": Second conditional expression "BBB": Processing when B holds "El if C": Third conditional expression "CCC": Processing when C holds

Example (with conditional branching, elif, True)


A = 70

if A == 100:
    print('A is 100.')
    
elif A == 80:
    print('A is 80.')
    
elif A == 70:
    print('A is 70.')

#output
#A is 70.

Example (multiple matching conditions)


A = 70
B = 100

if A == 100:
    print('A is 100.')
    
elif B == 100:
    print('B is 100.')
    
elif A == 70:
    print('A is 70.')

#output
#B is 100.

It ends when the conditions are met from the top. └ Both "B == 100" and "A == 70" are True, but the process ends when the above "B == 100" becomes True.


#### ▼ ④ Two or more conditional branches (with else) Also set the processing in case of false

Pattern 3 (elif, else)


if A:
    AAA
elif B:
    BBB
elif C:
    CCC
else:
    DDD

"A": Conditional expression "AAA": Processing when A holds "Eli if B": Second conditional expression "BBB": Processing when B holds "El if C": Third conditional expression "CCC": Processing when C holds "Else:": If none of the conditions are met "DDD": Processing when none of the conditions are met

Examples (elif, else)


A = 50

if A == 100:
    print('A is 100.')
    
elif A >= 80:
    print('A is over 80.')
    
elif A >= 70:
    print('A is over 70.')
    
else:
    print('A is 70 or less.')

#output
#A is 70 or less.

## 2. Conditional expression operator The operator used to create a conditional expression.

** ▼ There are two types of operators ** -Comparison of "comparison operator" elements -A combination of "Boolean operators" conditional expressions

3. 3. Operator list

Comparison operator meaning
== equal
!= Not equal
> Greater(Not included)
>= that's all(Including)
< Less than (not included)
<= Below (including)
in Including elements
not in Does not contain elements
is Objects are equal

Boolean operator Contents
and And
or Or
not Not

## 4. Example of comparison operator

① "==" equal

There are two equals. An error will occur if there are one or three or more.

(One equal is an assignment to a variable)

「=="equal


A = 100

if A == 100:
    print('A is 100.')
else:
    print('A is other than 100.')

#output
#A is 100.

### ② "! =" Not equal

When "!" Is added at the beginning, it means "not".

「!=Not equal


A = 100

if A != 100:
   print('A is other than 100.') 
else:
   print('A is 100.')

#output
#A is 100.

Supplement: The name of "!" Exclamation mark, exclamation mark, surprise mark


### ③ Greater than ">" (not included)

③ Larger than ">"


A = 50

if A > 50:
   print('A is greater than 50.') 
else:
   print('A is 50 or less.')

#output
#A is 50 or less.

### ④ "> =" or more (including)

「>="that's all(Including)


A = 50

if A >= 50:
   print('A is greater than 50.') 
else:
   print('A is 50 or less.')

#output
#A is greater than 50.

### ⑤ Less than "<" (not included)

Less than "<" (not included)


A = 50

if A < 50:
   print('A is less than 50.') 
else:
   print('A is 50 or more.')

#output
#A is 50 or more.

### ⑥ Below "<=" (including)

「<=Below (including)


A = 50

if A <= 50:
   print('A is 50 or less.') 
else:
   print('A is greater than 50.')

#output
#A is 50 or less.

⑦ Including "in" element

if A in B └ A is included in B.

Includes "in" element


A = 'pen'
B = 'I have a pen'

if A in B:
   print('Yes') 
else:
   print('No')

#output
# Yes

### ⑧ Does not include "not in" element `if A not in B` └ A is not included in B.

Does not include "not in" element


A = 'I have a pen'
B = 'pen'

if B not in A:
   print('Yes') 
else:
   print('No')

#output
# No

### ➈ "is" objects are equal Check if the id numbers assigned to the objects are the same.

Stricter than "==".

"Is" objects are equal


A = 'I have a pen'
B = 'I have a pen'

if A is A:
   print('Yes') 
else:
   print('No')

#output
# Yes

"Is" Comparison of different objects


A = 'I have a pen'
B = 'I have a pen'

if A is B:
   print('Yes') 
else:
   print('No')

#output
# No

## 5. Example of Boolean operator Used when you want to multiply multiple conditions. Each operator can be used together.

① "and"

if A and B: └ "A" "B" conditional expression

"And"


a = 1
b = 2

if a == 1 and b == 2:
    print('a=1, b=2')
else:
    print('a=1, b=Not 2')

#output
# a=1, b=2

"And" and example 2


a = 1
b = 2
c = 3

if a == 1 and b == 2 and c != 3:
    print('a=1 and b=2 and 3 is other than 3')
else:
    print('a=1 and b=2 and 3 is not other than 3')

#output
# a=1 and b=2 and 3 is not other than 3

### ② "or" or `if A or B:`

"And"


a = 1
b = 2
c = 3

if a == 1 and b == 2 or c != 3:
    print('a=1 and b=2 or 3 is other than 3')
else:
    print('a=1 and b=2 or 3 is not other than 3')

#output
# a=1 and b=2 or 3 is other than 3

** ▼ Operators can be used together **

"And" and "or"


a = 1
b = 2
c = 3

if a == 100 and b == 200 or c != 3:
    print('a=1 and b=2 or 3 is other than 3')
else:
    print('a=1 and b=2 or 3 is not other than 3')

#output
# a=1 and b=2 or 3 is not other than 3

③ Not "not"

if not A: └ Add to the beginning of the conditional expression

Not "not"


a = 100
b = 100

if not a == b:
    print('a=not b')
else:
    print('a=b')

#output
# a=b

「!=Will be the same as


a = 100
b = 100

if a != b:
    print('a=not b')
else:
    print('a=b')

#output
# a=b

** ■ Supplement ** The if statement is often used in the for statement. If you use an if statement in a for statement, you can extract only the values that match the conditions from a huge array.

> For statement here


[Return to top](Basic pattern and usage of #pythonif statement Comparison operator and Boolean operator)

Recommended Posts

[Python] Basic pattern and usage of if statement (comparison operator and Boolean operator)
[python] Correct usage of if statement
Python basic if statement
Basic usage of Python f-string
Java and Python basic grammar comparison
Non-logical operator usage of or in python
[Super basic] Compare Python, Java and JavaScript (variable, if statement, while statement, for statement)
Python if statement
[Python] if statement
Comparison of R and Python writing (Euclidean algorithm)
[Python] Class type and usage of datetime module
Comparison of Python and Ruby (Environment / Grammar / Literal)
[Introduction to Python] Basic usage of lambda expressions
Basic operation of Python Pandas Series and Dataframe (1)
Basic usage of flask-classy
Python --Explanation and usage summary of the top 24 packages
Basic usage of Jinja2
Usage of Python locals ()
Basic usage of SQLAlchemy
A quick comparison of Python and node.js test libraries
Basic knowledge of Python
Comparison table of frequently used processes of Python and Clojure
[Python] Chapter 05-01 Control syntax (comparison operator and conditional branching)
Comparison of CoffeeScript with JavaScript, Python and Ruby grammar
Comparison of how to use higher-order functions in Python 2 and 3
[2020/06 latest version] Basic usage of python dependency management tool poetry
[Python] Correct usage of map
Super basic usage of pytest
Basic usage of PySimple GUI
Python installation and basic grammar
Addition with Python if statement
Comparison of 4 Python web frameworks
Python bitwise operator and OR
Basic usage of Pandas Summary
Sample usage of Python pickle
Python 3 sorted and comparison functions
Comparison of Apex and Lamvery
Python or and and operator trap
Python (Python 3.7.7) installation and basic grammar
Speed comparison of Wiktionary full text processing with F # and Python
Difference in how to write if statement between ruby ​​and python
Summary of differences between Python and PHP (comparison table of main items)
[Introduction to Python] How to use the Boolean operator (and ・ or ・ not)
Environment construction of python and opencv
Basic knowledge of Linux and basic commands
The story of Python and the story of NaN
Installation of SciPy and matplotlib (Python)
Python Basic Course (14 Modules and Packages)
This and that of python properties
Installation and easy usage of pytest
Speed comparison of Python XML parsing
[Python] File operation using if statement
Summary of Python indexes and slices
Basic study of OpenCV with Python
Reputation of Python books and reference books
[Python] Visualize the heat of Tokyo and XX prefectures (DataFrame usage memo)
[Introduction to Python] How to judge authenticity with if statement (True and None)
Comparing the basic grammar of Python and Go in an easy-to-understand manner
[Python of Hikari-] Chapter 05-09 Control syntax (use of for statement and while statement properly)