- Python Programming with Raspberry Pi
- Sai Yamanoor Srihari Yamanoor
- 205字
- 2021-07-02 23:48:47
Conditional statements
In Python, conditional statements are used to determine if a specific condition is met by testing whether a condition is true or false. Conditional statements are used to determine how a program is executed. For example, conditional statements could be used to determine whether it is time to turn on the lights. The syntax is as follows:
if condition_is_true:
do_something()
The condition is usually tested using a logical operator, and the set of tasks under the indented block is executed. Let's consider the example, check_address_if_statement.py (available for download with this chapter) where the user input to a program needs to be verified using a yes or no question:
check_address = input("Is your address correct(yes/no)? ")
if check_address == "yes":
print("Thanks. Your address has been saved")
if check_address == "no":
del(address)
print("Your address has been deleted. Try again")
In this example, the program expects a yes or no input. If the user provides the input yes, the condition if check_address == "yes" is true, the message Your address has been saved is printed on the screen.
Likewise, if the user input is no, the program executes the indented code block under the logical test condition if check_address == "no" and deletes the variable address.
- 32位嵌入式系統(tǒng)與SoC設(shè)計導(dǎo)論
- Oracle SOA Governance 11g Implementation
- Mastering Spark for Data Science
- 網(wǎng)絡(luò)組建與互聯(lián)
- 信息物理系統(tǒng)(CPS)測試與評價技術(shù)
- 精通數(shù)據(jù)科學(xué)算法
- Linux服務(wù)與安全管理
- R Data Analysis Projects
- Linux Shell編程從初學(xué)到精通
- 工業(yè)機器人實操進(jìn)階手冊
- Red Hat Enterprise Linux 5.0服務(wù)器構(gòu)建與故障排除
- Embedded Linux Development using Yocto Projects(Second Edition)
- Spark Streaming實時流式大數(shù)據(jù)處理實戰(zhàn)
- Mastercam X5應(yīng)用技能基本功特訓(xùn)
- 探索中國物聯(lián)網(wǎng)之路