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 if judgment statement in Python

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces you how to use the if judgment statement in Python. The content is very detailed. Interested friends can refer to it for reference. I hope it can help you!

1. Basic syntax of if judgment statement

If conditions to judge:

What to do when the conditions are true

Note: code indented as a tab key, or four spaces (pycharm automatically added for us) in python development,Tab and space do not mix

Example 1:

Requirements:

1. Define an integer variable

2. Determine whether you are over 18 years old (>=)

3. If you are over 18, you are allowed to enter the Internet cafe.

Summary:

The above two examples are only the values of the age variable are different, but the results are different; you can see the role of the if judgment statement: that is, when certain conditions are met, the block of code will be executed, otherwise the block of code will not be executed

Note:

Indent code with one tab or four spaces

2. Comparison (that is, relational) operator

The comparison operators in Python are as follows:

3. logical operators

The logical operators in Python are as follows:

4.if - else

Think about it: when using if, it can only do what it needs to do if the condition is met. What if you need to do something when you don't meet the conditions?

We use else in the following format

Note that else must be used with if, and its else is not followed by conditions

if conditions:

Things to do when conditions are met 1

Things to do when conditions are met 2

... (omitted)...

else:

Things to do when conditions are not met 1

Things to do when conditions are not met 2

... (omitted)...

Example 2

demand

1. Enter user age

2. Determine whether you are over 18 years old (>=)

3. If you are over 18, you are allowed to enter the Internet cafe.

4. If you are under 18, prompt to go home and do homework

5. if statement advanced--elif

In development, if can be used to determine the condition, else can be used to handle the condition does not hold

However, if you want to add more conditions, the code that needs to be executed will be different depending on the addition, you can use

The elif syntax is as follows:

if Condition 1:

Condition 1 Fulfilment of Executed Code

elif Condition 2:

Code executed when condition 2 is met

else:

The code executed when none of the above conditions are met

Note:

Both elif and else must be used in conjunction with if, not alone.

You can think of if,elif, and else, with their indented code, as a complete block of code.

Example 3

Requirements:

1. Define the day string variable to record the program name

2. If it's Valentine's Day, you should buy roses/watch movies

3. If it's a birthday, you should buy a cake/gift.

4. Every other day is a holiday.

6. If nesting

The application scenario of elif is: to judge multiple conditions at the same time, and most of the conditions are flat. In development, use if to judge the conditions. If you want to add conditional judgment to the execution statement where the conditions are established, you can use nested if. The nested application scenario of if is: Under the premise that the previous conditions are met, add additional judgment. The nested syntax format of if is no different from the previous one except indentation.

The syntax format is as follows:

if Condition 1:

Condition 1 Fulfilment of Executed Code

....

If condition 2 based on condition 1:

Code executed when condition 2 is met

....

#Condition 2 is not satisfied

else:

Code executed when condition 2 is not met

....

#Handling when condition 1 is not satisfied

else:

Code executed when condition 1 is not met

....

Example 3

Requirements:

1. Enter the current balance of the bus card and receive it with the cart_money variable

2. If it's more than 2 yuan, you can get on the bus.

3. Definition seat variable =1, 1 means there is a seat, 0 means there is no seat

4. If there is an available seat in the car, you can sit down.

5. If there are no empty seats on the bus, please hold on to the handrail

6. If it is not less than 2 yuan, the balance is insufficient.

How to use the if judgment statement in Python is shared here. I hope the above content can be of some help to everyone and learn more. If you think the article is good, you can share it so that more people can see it.

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