[0-Basic Introduction to Python Web Notes] 2. Python's logical operations and process statements

logic operation

Python provides basic logical operations: not only Boolean operations ( and, or, not ), but also comparison operators ( ==, !=, <, >, <=, >= ) for comparing two values. Relationship. These operators are used to determine whether an expression is true or false, thereby making conditional judgments.

The logical operation rules are as shown in the following table (where x=1, y=2):

operator describe Example result
and AND operation True and False False
or OR operation True or False True
not NOT operation not True False
== equal x == y False
!= not equal to x != y True
< less than x < y True
> more than the x > y False
<= less than or equal to x <= y True
>= greater or equal to x >= y False

In python we can try logical operations through the following code:

# 比较运算符示例
x = 1
y = 2

# 逻辑与(and)
result_and = True and False  # 结果为 False

# 逻辑或(or)
result_or = True or False  # 结果为 True

# 逻辑非(not)
result_not = not True  # 结果为 False

# 等于:x是否等于y
result_equal = x == y  # 结果为 False

# 不等于:x是否不等于y
result_not_equal = x != y  # 结果为 True

# 小于:x是否小于y
result_less_than = x < y  # 结果为 True

# 大于:x是否大于y
result_greater_than = x > y  # 结果为 False

# 小于等于:x是否小于等于y
result_less_equal = x <= y  # 结果为 True

# 大于等于:x是否大于等于y
result_greater_equal = x >= y  # 结果为 False

control flow statement

For novices, it is enough to know that python control flow statements include conditional statements (if statements) and loop structures (for loops and while loops)

Conditional statement (if statement)

Conditional statements are used to selectively execute different blocks of code based on different conditions. The most common conditional statement is the if statement, which is used to determine whether a condition is true and then execute the corresponding code.

The following is a simple if control condition:
Insert image description here

The corresponding code example:

# 定义年龄变量
age = 18

# 判断年龄是否大于等于18
if age >= 18:
	# 输出成年了
    print("成年了")
else:
	# 不满足if进入else输出未成年
    print("未成年")

Loop structure (for loop, while loop)

  • for loop
    The for loop is used to traverse the elements in a sequence (such as a list, string, etc.) and perform corresponding operations.
# for循环 列表示例
fruits = ['apple', 'banana', 'orange']
for fruit in fruits:
    print(fruit)

The result of running the above code is:
Insert image description here

# for循环 字符串示例
fruit = 'apple'
for i in fruit:
    print(i)

The result of running the above code is:
Insert image description here

You can also combine the range() function to iterate numbers:

# 使用range()的for循环
for i in range(5):
	# 这里会输出0,1,2,3,4
    print(i)

The result of running the above code is:
Insert image description here

  • while loop A
    while loop repeatedly executes a block of code until a specified condition is no longer met.
# while循环示例
count = 0
while count < 5:
    print(count)
    count += 1

The result of running the above code is:
Insert image description here

continue, break and pass keywords

  • continue keyword usage
    The continue keyword skips the remaining statements of the current loop and proceeds to the next loop

['apple', 'banana', 'orange'] , here I don't like banana, so I want to skip banana, which can be achieved by using the continue keyword, code example:

# for循环 continue示例
fruits = ['apple', 'banana', 'orange']
for fruit in fruits:
    if fruit == 'banana':
    	#如果fruit等于banana就跳过
        continue
    print(fruit)

The result of running the above code is:
Insert image description here

  • The break keyword usage
    The break keyword is used to terminate the loop statement and forcefully stop the current loop structure.

['apple', 'banana', 'orange'] , here I hate banana very much. When I meet banana, I don't want anything else! This can be achieved through the break keyword, code example:

# for循环 break示例
fruits = ['apple', 'banana', 'orange']
for fruit in fruits:
    if fruit == 'banana':
    	#如果fruit等于banana就结束
        break
    print(fruit)

The result of running the above code is:
Insert image description here

  • The use of the pass keyword
    acts as a placeholder for the code block. If you haven't figured out how to deal with banana, we can put a placeholder first. Code example:
# for循环 pass示例
fruits = ['apple', 'banana', 'orange']
for fruit in fruits:
    if fruit == 'banana':
        # 如果fruit等于banana就啥也不干
        pass
    print(fruit)

The result of running the above code:
Insert image description here
I would like to ask, what will happen if you don’t put pass? There will be a syntax error! Because : the back represents the new next-level code block, which must have something. At this time, you can use pass to occupy the position.

fruits = ['apple', 'banana', 'orange']
for fruit in fruits:
    if fruit == 'banana':
    print(fruit)

Insert image description here

Nesting of control flow statements and elif

Here we have a requirement, those over 18 years old will be exported as adults, those under 18 years old will be exported as minors, and those under 3 years old will be exported as little kids.

  • Solution 1: Nested control
    We can understand the requirements as the following flow chart:
    Insert image description here
    Its corresponding code example:
# 定义年龄变量
age = 2

# 判断年龄是否大于等于18
if age >= 18:
    # 输出成年了
    print("成年了")
else:
    # 不满足if进入else输出未成年
    if age <= 3:
        print("小屁孩")
    else:
        print("未成年")

The result of running the code is: little kid

  • Option 2: Introduce the elif concept

The function of elif is to check whether the next condition is true when the condition of the if statement is false. If it is true, execute the corresponding code block. Otherwise, continue to check the next condition or execute the code in the else block (if any). .
Insert image description here

Based on the above requirements, sample code:

# 定义年龄变量
age = 2

# 判断年龄是否大于等于18
if age >= 18:
    # 输出成年了
    print("成年了")
elif age <= 3:
    print("小屁孩")
else:
    print("未成年")

In the code example:

  1. If the age is greater than or equal to 18, it will output: "Adult"
  2. If the age is less than or equal to 3, it will output: "little kid"
  3. If the age is neither greater than or equal to 18 nor less than or equal to 3, it will output: "Underage"

The result of running the code is: little kid

For more practical projects, please visit the official website below

Guess you like

Origin blog.csdn.net/m0_47220500/article/details/132360358