Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the if statement in Python

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to use if sentences in Python". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to use if sentences in Python".

I. condition judgment

1. Definition

Conditional judgment is the thinking of communicating with the computer. The goal is to let the computer know under what conditions and what to start. The same is true for Python, the reason why Python can complete automated tasks, such as automatically punching in, automatically grabbing Internet buzzwords, and automatically downloading blockbusters, is because it can perform conditional judgment.

2. The form of expression

(1) one-way judgment: if

If stands for "if". Code format:

If xxx:

Print ('xxx')

For example, if the money is greater than or equal to 1000, bring your girlfriend to eat Japanese food. The code is implemented as follows:

Code explanation:

The first step is to assign the current situation with the = sign: assign 1000 to the variable money.

The second step is to determine the if condition: if the value of Qian money is > = 1000, execute the next line of command after the colon.

The third step, use the print () command to print out the results, meet the conditions, and bring your girlfriend to eat Japanese food.

Note: after the colon of the if statement and in front of the print () statement, several spaces appear to be indented. Indentation is a grammatical rule that will help Python better distinguish between code structure and hierarchy, and computers can better execute Python code. Everything that is indented is called a block of code within an if statement. If the if condition is met, the computer executes the block of code within the if statement.

(2) two-way judgment: if else

If else stands for "if... not satisfied, just..." . Specifically, if the condition of if is not met, the command under else is executed. If and else themselves represent the condition of choosing one of the two. Code format:

If XXX:

Print ('XXXX')

Else:

Print ('XXXXXXX')

For example, Xiao K plans to run every day if she weighs more than 90 jin (including 90 jin); if she weighs less than 90 jin, she goes for a walk every day. As a result, the code weighing 98 jin is implemented as follows:

(3) Multi-directional judgment: if ·elif ·else

In judging three or more conditions, we need to use if in Python. Elif... Else... Statement. Code format:

If xxx:

Print (xxxx)

Elif xxx:

Print (xxxx)

Elif xxx:

Print (xxxx)

Else:

Print (xxxx)

For example, if the money is more than 1000 (inclusive), take your girlfriend to eat Japanese food; if the money is more than 1000 (inclusive), take your girlfriend to pizza; if the money is less than 1000, take your girlfriend to KFC. Code demonstration:

Money = xxx

If money > = 1000:

Print ('take your girlfriend to eat Japanese food')

Elif money > = 800:

Print ('take your girlfriend out for pizza')

Else:

Print ('take your girlfriend to KFC')

Code explanation, the first step, the first line assignment, will try one by one from top to bottom to see which condition you meet? The second step is to execute the corresponding code block in which condition is satisfied, and continue to try back until the result is obtained if the condition is not met.

2. If nesting

When we encounter if there is an if, that is, there are conditions in the conditions, how can we use Python to accomplish this problem? At this point, if nesting conditions are required. The scenario used by if nesting is to observe whether other additional conditions are met when the basic conditions are met. Here is an example to illustrate that in the X-Men team, the contribution value of Wolverine is 860. Please write a code to judge the rank of Wolverine. The code needs to meet the following conditions:

a. If the contribution value is greater than or equal to 600, "Special Operations personnel" is displayed, under this premise:

a. If the contribution value is greater than or equal to 800, "Masters" is displayed.

b. If the contribution value is less than 800, "Gold" is displayed.

b. If the contribution value is less than 600, "ordinary fighter" is displayed, under this premise:

c. If the contribution value is greater than 400, Silver is displayed.

d. If the contribution value is less than or equal to 400, Bronze is displayed.

The code is implemented as follows:

Contribution=860

If contribution > = 600:

Print ('Special Operations personnel')

If contribution > = 800:

Print ('King')

Else:

Print ('gold')

Else:

Print ('ordinary fighter')

If contribution > 400:

Print ('Silver')

Else:

Print ('bronze')

Print ('done')

Third, the execution order of if nesting

With so many if conditions, what is the order in which the computer executes? Here or take the above example, the order in which the computer executes is: assign a value to contribution first. Then, because if and else choose one of the two, only one of the code in if and else will be executed, either code block 1 or code block 2; if contribution=860 meets the condition of if (contribution > = 600), it goes into code block 1 to execute print ('special operations personnel'). Then go to code block 1, contribution=860 to try the conditions in code block 1, meet the condition of contribution > = 800. you can only execute if in code block 1 and execute print ('King'). Finally, print ('done') is executed.

The above is all the content of the article "how to use if sentences in Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report