官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 阿坝县| 晋州市| 奎屯市| 富蕴县| 磴口县| 四平市| 射阳县| 武穴市| 辽中县| 商丘市| 聂拉木县| 定南县| 博爱县| 普洱| 兴隆县| 静宁县| 通辽市| 安达市| 鹰潭市| 手机| 壤塘县| 巴彦淖尔市| 辉县市| 闻喜县| 保亭| 沙雅县| 新津县| 唐山市| 秦安县| 河北省| 阿克苏市| 荆州市| 马龙县| 共和县| 镶黄旗| 大同市| 客服| 密云县| 秭归县| 赤水市| 晋中市|