Python basic course (8 branches)

Branch processing

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.

if statement

if (if)

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 ~ else (if ~, otherwise)

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.

if ~ elif ~ else (if ~, otherwise ~, otherwise)

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

Comparison operator

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.

and or not (and or not)

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

A value that always returns False

Values that Python treats as False

None type

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

Python basic course (8 branches)
Python Basic Course (7 Dictionary)
Python basic course (9 iterations)
Python Basic Course (11 exceptions)
Python basic course (6 sets)
Python Basic Course (Introduction)
Python Basic Course (3 Python Execution)
Python Basic Course (5 List Tuples)
Python Basic Course (1 What is Python)
Python Basic Course (14 Modules and Packages)
RF Python Basic_01
Basic Python writing
Python3 basic grammar
RF Python Basic_02
Python Basic Course (at the end of 15)
Python basic course (4 numeric type / character string type)
Python I'm also basic
Python basic grammar / algorithm
Basic sorting in Python
[python] class basic methods
Python3 cheat sheet (basic)
Python basic grammar (miscellaneous)
Python basic memorandum part 2
Python basic memo --Part 2
Basic Python command memo
Python basic grammar note (4)
Basic knowledge of Python
Python basic grammar memo
OpenCV basic code (python)
Python basic memo --Part 1
python memorandum super basic
Python basic if statement
Python Basic --Pandas, Numpy-
Python application: Pandas Part 1: Basic
BASIC authentication with Python bottle
Python basic dict sort order
[Python] Using OpenCV with Python (Basic)
Python installation and basic grammar
Python Basic Grammar Memo (Part 1)
Python basic grammar (miscellaneous) Memo (2)
Basic usage of Python f-string
I learned Python basic grammar
Python basic grammar (miscellaneous) Memo (4)
Python (Python 3.7.7) installation and basic grammar
Java and Python basic grammar comparison
Python
Scraping with Selenium in Python (Basic)
Python course for data science_useful techniques
I took Progete's Python Learning Course I
Basic grammar of Python3 system (dictionary)
Basic Python operation 2nd: Function (argument)
Python application: data visualization part 1: basic
Basic study of OpenCV with Python
Basic Linear Algebra Learned in Python (Part 1)
Python basic operation 1st: List comprehension notation
Getting Started with python3 # 1 Learn Basic Knowledge
Setting up Basic authentication using Python @Lambda
[Basic grammar] Differences between Ruby / Python / PHP
Python basic operation 3rd: Object-oriented and class
Learn Python! Comparison with Java (basic function)
[Python] I personally summarized the basic grammar.