- By Han Xinzi @Showmeai
- Tutorial address: www.showmeai.tech/tutorials/5…
- This paper addresses: www.showmeai.tech/article-det…
- Statement: All rights reserved, please contact the platform and the author and indicate the source
1.Python conditional statements
In Python, you can use an if statement to evaluate a condition and then execute different code depending on the result (True or False). If statements in Python can be broken down into three forms: if statements, if else statements, and if elif else statements.
The following diagram provides an overview of conditional statement execution:
The Python programming language specifies that any non-0 and non-null (null) values are true, and 0 or NULL are false.
The if statement in Python is used to control the execution of a program. The basic form is:
ifCriteria: Execute a statement......else: Execute statement......Copy the code
Where the “judgment condition” is established (non-zero), then the following statement is executed, and the execution content can be multiple lines, to indent to distinguish the same range.
Else is an optional statement that can be executed if the content needs to be executed when the condition is not standing.
Gif demo:
Here is an example (the code can run in an online PYTHon3 environment) :
flag = False
name = 'ShowMeAI'
if name == 'python': Check whether the variable is Python
flag = True Set flag to true if condition is true
print('welcome boss') # and output a welcome message
else:
print('welcome '+name) Print the variable name if the condition is not standing
Copy the code
The output is:
Welcome ShowMeAI # output resultCopy the code
The criteria of an if statement can be expressed as > (greater than), <(less than), == (equal to), >= (greater than or equal to), <= (less than or equal to).
When the criteria are multiple values, you can use the following form:
ifJudge conditions1: Execute statement1...elifJudge conditions2: Execute statement2...elifJudge conditions3: Execute statement3...else: Execute statement4...Copy the code
Here is an example (the code can run in an online PYTHon3 environment) :
num = 6
if num == 3: Num = num
print('boss')
elif num == 2:
print('user')
elif num == 1:
print('worker')
elif num < 0: # output when the value is less than zero
print('error')
else:
print('random') # Conditions are not output immediately
Copy the code
The output is:
Random # Prints the resultCopy the code
As Python does not support switch statements, multiple conditions can only be determined using elif. If multiple conditions need to be determined simultaneously, or (or) can be used to indicate that the condition is successful if one of the two conditions is true. When and is used, it indicates that the judgment condition is successful only when both conditions are true.
# if statement multiple conditions
num = 9
if num >= 0 and num <= 10: Check whether the value is between 0 and 10
print('hello')
# Output: hello
num = 10
if num < 0 or num > 10: Check whether the value is less than 0 or greater than 10
print('hello')
else:
print('undefine')
# undefine
num = 8
# check whether the value is between 0 and 5 or between 10 and 15
if (num >= 0 and num <= 5) or (num >= 10 and num <= 15) :print('hello')
else:
print('undefine')
# undefine
Copy the code
2. Simple statement groups
You can also use an if conditional statement on the same line, as shown in the following example (the code can run in an online PYTHon3 environment) :
show = 100
if ( show == 100 ) : print("The variable show has a value of 100")
print("Good bye!")
Copy the code
The output of the above code execution is as follows:
The variable show has a value of 100 Good bye!Copy the code
Video tutorials
Please click on station B to view the [bilingual subtitle] version
www.bilibili.com/video/BV1yg…
Information and code download
This series of tutorials can be downloaded from Github on ShowMeAI, which can be run locally in Python. If you can use Google Colab, you can also use Google Colab.
The Python sketchbooks covered in this tutorial series can be downloaded at:
- Python quick table
Expanded Reference materials
- Python tutorial – Python3 documentation
- Python Tutorial – Official website of Liao Xuefeng
ShowMeAI related articles recommended
- Python is introduced
- Python installation and environment configuration
- Basic Python syntax
- Basic Python data types
- Python operator.
- Python conditional control with if statements
- Python loop statements
- Python while loop
- The python for loop
- Python break statement
- Python continue statement
- Python pass statement
- Python strings and operations
- Python list
- The python tuple
- Python dictionary
- Python set
- Python functions
- Python iterators and generators
- Python data structures
- Python module
- Python file reading and writing
- Python file and directory operations
- Python error and exception handling
- Object-oriented programming in Python
- Python namespaces and scopes
- Python time and date
ShowMeAI series tutorials recommended
- Illustrated Python programming: From beginner to Master series of tutorials
- Illustrated Data Analysis: From beginner to master series of tutorials
- The mathematical Basics of AI: From beginner to Master series of tutorials
- Illustrated Big Data Technology: From beginner to master