Python basis - conditional statements (if statement)

Copyright: please indicate the source https://blog.csdn.net/TaoismHuang https://blog.csdn.net/TaoismHuang/article/details/91042983

In programming, we often encounter such a problem, when certain conditions are satisfied, given the corresponding measures. In python, we can use statement is used to determine if a given condition is satisfied, the corresponding block is performed according to the result of the determination.

If statement will be mentioned herein, a single branch decision statement, two statements branch judgment, the branching operation determined trinocular statement, and if the if the nest.

Single branch judgment statement
if 判断条件:
	语句块

First execution determination condition, when the judgment condition is satisfied statement block executes, if the condition is not satisfied, is not performed.

Case Study: Internet cafes prohibit minors under 18 years of age to enter, enter the user's age, if less than 18, then print "Minors are not allowed to enter."

age = int(input("请输入您的年龄:"))
if age < 18:
    print("禁止进入!")

Analyzing the condition of the if statement can be> (greater than), <(less than), == (equal),> = (greater than or equal), <= (less than or equal) to represent the relationship.

if the judge sentences can also shorthand:

if x:
   print("True")

As long as x is a non-zero value, non-empty string, a non-null List, it is determined is True, False otherwise

a = [1,2,3]
if a:
	print("True")
Two-branch decision words
if 判断条件:
	语句块1
else:
	语句块2

First execution determination condition, when the judgment condition is satisfied executes a statement block, if the condition is not satisfied, the execution statement block 2.

Example: If you are younger than 18, print "Minors are not allowed to enter", otherwise print "Welcome!"

age = int(input("请输入您的年龄:"))
if age >= 18:
    print("欢迎光临!")
else:
    print("禁止进入!")
Multi-branch judgment statement

Example: If the age is 18 or less, print juvenile, age greater than 18 and less than or equal to 25, print the youth, greater than 25 is less than or equal to 40, print the prime of life, greater than 40, to print your menopause.

We need multiple similar to the above judgment, we can use the following ways to solve

if 判断条件1:
	语句块1
elif 判断条件2:
    语句块2
...
elif 判断条件n:
    语句块n
else:
    语句块

First execution judgment condition 1, if the condition 1 holds a block of statements executes a block of statements 1 finished executing the jump branch statement; If the condition 1 is not satisfied, the judgment condition is performed 2, when the condition 2 is satisfied, the execution statement block 2; If the condition 2 does not hold, then continue down until judgment condition is satisfied, to determine if conditions are not established, else the following statement block is executed.

Note: elif is an abbreviation of else if, there can be multiple elif

Specific cases to achieve the following:

age = int(input("请输入您的年龄:"))
if age <= 18:
	print("少年")
elif age <= 25:
	print("青年")
elif age <= 40:
	print("壮年")
else:
	print("更年期到啦")

if the statement has a feature, which is down from the judge, if the judge is on a True, the corresponding statement after the judgment execution, it ignored other elif and else.

if the nest

Case: We now enter a number to determine whether he is even number greater than 10

If you want to solve these needs, we need two statements to determine whether the first number determines whether the input is greater than 10, and the second is the judgment on the basis of first come up determination whether this number is even.

Simply put, that is, in a nested if statement then the if statement, the effect is as follows:

if 判断条件1:
    if 判断条件2:
        语句块2-1
    else:
        语句块2-2
else:
    语句块1-2

Execution process: first execution determination condition 1, condition 1 is satisfied, the judgment condition 2 is executed, if the condition 2 is satisfied, the execution statement block 2-1, or 2-2 The statements; Condition 1 is not satisfied block of statements executes 1-2

Note: else and if the supporting and related indent, if the following else are aligned.

num =  int(input("请输入一个整数:"))
if num > 10:
	if num % 2 == 0:
		print("输入的数是一个大于10的偶数")

Note: From the perspective of grammar that nested layers is not limited, however, from the readability and maintainability of the code, the best nesting depth should not exceed three.

Of course, if a plurality of conditions to simultaneously determined, may be used or (or), represents a successful determination condition set up a two conditions; and when used (and), it represents a case where only two conditions are satisfied, the condition determination It was successful.

Cases can also be achieved using the following code:

num =  int(input("请输入一个整数:"))
if num > 10 and num%2 == 0:
	print("输入的数是一个大于10的偶数")
else:
	print("输入的数不是一个大于10的偶数")
Three head operations

grammar:

result1  if 判断条件  else  result2

If the condition is satisfied is performed result1, otherwise execution result2. result can be a variable, it can be a constant can also be an expression.

Essence: The if statement is mandatory to write in a row to complete a second election results.

Case: determining whether a number is even

number = int(input("请输入一个整数"))
res = True if number%2 == 0 else False
print(res)

Guess you like

Origin blog.csdn.net/TaoismHuang/article/details/91042983