Describes the nested use of if Statements in Python.

Source: Internet
Author: User

Describes the nested use of if Statements in Python.

This article mainly introduces the nested usage of if Statements in Python, which is the basic knowledge in getting started with Python. For more information, see

This may be the case when you want to check that the next condition is parsed as true. In this case, the nested if structure can be used.

In the nested if statement structure, you can have another if... elif... else structure in the if... else structure.

Syntax:

Syntax for nesting the if... elif... else structure can be:

?

1

2

3

4

5

6

7

8

9

10

11

12

If expression1:

Statement (s)

If expression2:

Statement (s)

Elif expression3:

Statement (s)

Else

Statement (s)

Elif expression4:

Statement (s)

Else:

Statement (s)

Restrictions. Please confirm. The source must be specified for reprinting.

This may be the case when you want to check that the next condition is parsed as true. In this case, the nested if structure can be used.

In the nested if statement structure, you can have another if... elif... else structure in the if... else structure.

Syntax:

Syntax for nesting the if... elif... else structure can be:

?

1

2

3

4

5

6

7

8

9

10

11

12

If expression1:

Statement (s)

If expression2:

Statement (s)

Elif expression3:

Statement (s)

Else

Statement (s)

Elif expression4:

Statement (s)

Else:

Statement (s)

For example:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

#! /Usr/bin/python

 

Var = 100.

If var & lt; 200:

Print "Expression value is less than 200"

If var = 150:

Print "Which is 150"

Elif var == 100:

Print "Which is 100"

Elif var = 50:

Print "Which is 50"

Elif var <50:

Print "Expression value is less than 50"

Else:

Print "cocould not find true expression"

 

Print "Good bye! "

When the above code is executed, it will produce the following results:

?

1

2

3

Expression value is less than 200

Which is 100

Good bye!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.