The interesting thing about Boolean values (especially expressions that return Boolean values) is that we can make decisions and take different paths based on their differencesTrue
orFalse
value.
inPythonWe useif
statement:
condition = True
if condition == True:
# do something
When the condition test is resolvedTrue
, As in the above case, its code block will be executed.
What is a block? A block is a part that is indented one level (usually 4 spaces) on the right:
condition = True
if condition == True:
print(“The condition”)
print(“was true”)
The block can consist of a single line or multiple lines, and end when you move back to the previous indentation level:
condition = True
if condition == True:
print(“The condition”)
print(“was true”)
print(“Outside of the if”)
combine withif
You can have oneelse
Block if the condition is testedif
Result toFalse
:
condition = True
if condition == True:
print(“The condition”)
print(“was True”)
else:
print(“The condition”)
print(“was False”)
And you can have different connectionsif
And checkelif
, If the previous check wasFalse
:
condition = True
name = "Roger"
if condition == True:
print(“The condition”)
print(“was True”)
elif name == “Roger”:
print(“Hello Roger”)
else:
print(“The condition”)
print(“was False”)
In this case, ifcondition
YesFalse
withname
The variable value is "Roger".
in aif
Declare that you can only have oneif
withelse
Check, but there are multiple serieselif
an examination:
condition = True
name = "Roger"
if condition == True:
print(“The condition”)
print(“was True”)
elif name == “Roger”:
print(“Hello Roger”)
elif name == “Syd”:
print(“Hello Syd”)
elif name == “Flavio”:
print(“Hello Flavio”)
else:
print(“The condition”)
print(“was False”)
if
withelse
It can also be used in inline format, which allows us to return one value or another based on conditions.
example:
a = 2
result = 2 if a == 0 else 3
print(result) # 3
More python tutorials:
- Introduction to Python
- 在macOS上安装Python 3
- Run Python program
- Python 2 and Python 3
- Basics of using Python
- Python data types
- Python operators
- Python string
- Python boolean
- Python numbers
- Python, accepts input
- Python control statements
- Python list
- Python tuple
- Python set
- Python dictionary
- Python functions
- Python objects
- Python loop
- Python module
- Python class
- Python standard library
- Debug Python
- Python variable scope
- Python, accept parameters from the command line
- Python recursion
- Python nested functions
- Python Lambda function
- Python closure
- Python virtual environment
- Use Python to use GoPro as a remote webcam
- Python, how to create a list from a string
- Python decorator
- Python Docstrings
- Python introspection
- Python notes
- Python, how to list files and folders in a directory
- Python, how to check if a number is odd or even
- Python, how to get detailed information of a file
- Python, how to check if a file or directory exists
- Python exception
- Python, how to create a directory
- Python, how to create an empty file
- Python, `with` statement
- Python, create a network request
- Python, use `pip` to install third-party software packages
- Python, read file content