Python conditional statements using if expressions are not too difficult, what you need to pay attention to is the nested usage and how to set the corresponding conditions.
if conditional judgment statement
Python statements are executed in a fixed order, with the previous statement executed first, followed by the next. If you want your program to follow your own customized process, you need to use process control statements, most notably conditional statements and loops.
A conditional statement is denoted by if, which means that when a condition is met, the following branching code is executed. When the condition is not met, the following branching code is skipped.
In Internet products, you can often see conditional judgment scenarios. For example, in the login page of an app, after entering the username and password, the program will judge whether the username and password are correct or not, and if they are correct, it will run the user into the login state. If they are incorrect, the user must re-enter them.
The corresponding pseudo-code is:
if Username and password are correct: Login Successful else: Re-enter your username and password
The syntax of an if statement can be expressed as:
if expression: statement1 else: statement2
When the expression is True, the condition is satisfied and statement1 is executed; when the expression is False, it jumps to statement2. else is the branch that will be executed if all of the above conditions are not met in an if statement. When you're sure that you don't need to do anything with else, you can omit it:
if expression: statement1
One thing to note here is that encountering a colon in python:
When there is a new line, the new line is indented to indicate a clause. If there is no new line, but the statement follows directly, no indentation is required:
if expression: statement1
Let's take an example to illustrate the use of if statements. When a student scores more than 80 points on a test, the score is good, otherwise it is fair.
score = 88 if score > 80: print("Good.") else: print("General")
Sometimes there are more than one conditional judgment in an if statement, and you can use elif to separate each set of conditions.
score = 70 if score > 80: print("Good.") elif 60 < score <= 80: print("Not bad.") else: print("Failing.")
In the same if statement, only the first branch that satisfies the condition first will execute, the others will not. In the example above, the student scored 70 points, and the second conditional expression is satisfied first, then the second branch is taken. And in the following example, what is printed if there is an overlap in the range of multiple conditions?
score = 70 if score > 60: print("Not bad.") elif 60 < score <= 80: print("Okay.") else: print("Failing.")
Because the first condition is satisfied first, the first branch is executed directly and all other branches are not executed.
Multiple if expressions need to be judged independently.
score = 70 if score > 60: print("Not bad.") if 60 < score <= 80: print("Okay.") else: print("Failing.")
In this example, there are 2 if expressions, and the first one does not affect the execution of the second one.
nested conditional statements
An if statement can be nested within another if statement, like a Russian nesting doll. It means that the inner if statement can be executed after the outer if condition is satisfied. However, it is common to terminate the if statement early, because when conditional statements are nested many times, it affects the readability of the code.
score = 88 age = 1 if score > 80: print("Good.") if age < 6: print("Prodigy.") else: print("General")
Conditional operations you may not know about
In the program, it is often necessary to determine whether a string is empty, the most commonly used way is:
a = '' if not a: print("String is empty") if a: print("The string is not empty.")
Whether it's a string or not, or a list, dictionary, or collection, you can use the same way to determine whether they are empty or not. Of course you can also use the following ways, but these are not good practices.
if a == '': print("String is empty") if len(a) == 0: print("String is empty")
To determine if None, use is:
if a is None: print("a is None")
Determines if it is 0:
if a == 0: print("a is 0")
Examples of python conditional judgment statements
name = '' password = '' if name == 'admin' and password == '123456': print("Login successful.") else: print("Login failed.")
To this article on the use of Python conditional statement is introduced to this article, more related Python conditional statement content, please search my previous articles or continue to browse the following related articles I hope you will support me more in the future!