Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The use of Multi-layer nesting of Python if statements

Shulou Source: shulou.com Published: 2022-06-02 07:39:56 10月01日 Update

This article mainly explains "the use of multi-layer nesting of Python if sentences". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the use of multi-layer nesting of Python if sentences".

Introduction to the use of multi-layer nesting of Python if statements. Nesting of if statements means that if statements contain if statements. In Python, if, if else and if elif else can be nested with each other. Therefore, when developing the program, we need to choose the appropriate nesting scheme according to the needs of the scene. It is important to note that when nesting each other, the indentation specifications for different levels of code blocks must be strictly followed.

If statement refers to one of the two operations used in the programming language to determine whether the given condition is satisfied or not and to execute the given operation according to the result of the decision. The return value of if is true or false, which can be stored as a Bool variable and takes up a byte.

The nesting of if statements is that if statements contain if statements

In the format of the above if statement nesting, first determine whether the result of conditional expression 1 in the outer if statement is True, if the result is True, then execute code block 1, then determine whether the result of conditional expression 2 of inner if is True, and if the result of conditional expression 2 is True, then execute code block 2.

For if nested statements, there are two points to note:

1.if statements can be nested at multiple levels, not just two.

two。 If statements, if-else statements, and elif statements can be used for both outer and inner if judgments.

Calculate the total number of days in the month according to the year and month

The above code first defines the variables year and month to represent the year and month, which are used to receive the year and month input by the user, respectively, and then judge the month: if the month is 1, 3, 5, 7, 8, 10, 12, the output "year and month has 31 days"; if the month is 4, 6, 9, 11, the output "year and month has 30 days" If the month is February, you need to judge the year: "the year has 29 days" when the year is a leap year, and "the year has 28 days" when the year is a normal year. Python Foundation

Thank you for your reading, the above is the content of "the use of multi-layer nesting of Python if statements". After the study of this article, I believe you have a deeper understanding of the use of multi-layer nesting of Python if statements, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Statement month multi-layer year usage method result code year month output condition expression learning content inner layer variable outer layer different appropriate between Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Tech Info OPPO Reno Redmi NVidia