Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Conditional Statements

Python Conditional Statements

Python conditional statements determine which code block to execute based on the result (True or False) of one or more statements.

The following diagram gives a simple view of how conditional statements execute:

The Python programming language treats any non-zero and non-null value as true, and 0 or null as false.

In Python programming, the if statement is used to control program execution. Its basic form is:


if condition:
    执行语句……
else:
    执行语句……

When the condition is true (non-zero), the following statements are executed. The executed content can span multiple lines, with indentation used to indicate that they belong to the same scope.

The else statement is optional; it allows you to execute relevant statements when the condition is not met.

GIF demonstration:

A concrete example is as follows:

Example

#!/usr/bin/python

-- coding: UTF-8 --

Example 1: Basic usage of if

flag

=

False

name

=

'

luren

'

if

name

==

'

python

'

:

Check whether the variable is python

flag

=

True

Set the flag to true when the condition is met

print

'

welcome boss

'

and output a welcome message

else

:

print

name

Output the variable name when the condition is not met

The output is:


luren            # 输出结果

The condition of an if statement can use > (greater than),<< (less than), == (equal to), >= (greater than or equal to),<<= (less than or equal to) to express their relationship.

When there are multiple values to check against a condition, you can use the following form:


if 判断条件1:
    执行语句1……
elif 判断条件2:
    执行语句2……
elif 判断条件3:
    执行语句3……
else:
    执行语句4……

The example is as follows:

Example

#!/usr/bin/python

-- coding: UTF-8 --

Example 2: Usage of elif

num

=

5

if

num

==

3

:

Check the value of num

print

'

boss

'

elif

num

==

2

:

print

'

user

'

elif

num

==

1

:

print

'

worker

'

elif

num

<

0

:

Output when the value is less than zero

print

'

error

'

else

:

print

'

roadman

'

Output when none of the conditions are met

The output is:


roadman        # 输出结果

Since Python does not support switch statements, multiple conditional checks can only be implemented with elif. When multiple conditions need to be checked at the same time, you can use or, which means the condition succeeds if either of two conditions is true; when using and, the condition succeeds only if both conditions are true.

Example

#!/usr/bin/python

-- coding: UTF-8 --

Example 3: Multiple conditions in an if statement

num

=

9

if

num

=

0

and

num

<=

10

:

Check whether the value is between 0 and 10

print

'

hello

'

Output result: hello

num

=

10

if

num

<

0

or

num

10

:

Check whether the value is less than 0 or greater than 10

print

'

hello

'

else

:

print

'

undefine

'

Output result: undefine

num

=

8

Check whether the value is between 0 and 5 or between 10 and 15

if

(

num

=

0

and

num

<=

5

)

or

(

num

=

10

and

num

<=

15

)

:

print

'

hello

'

else

:

print

'

undefine

'

Output result: undefine

When an if statement has multiple conditions, you can use parentheses to determine the order of evaluation; conditions in parentheses are evaluated first. In addition, and and or have lower precedence than > (greater than),<(less than) and other comparison operators. That is, without parentheses, greater-than and less-than are evaluated before and and or.

Simple statement groups

You can also use an if conditional statement on a single line, as in the following example:

Example

#!/usr/bin/python

-- coding: UTF-8 --

var

=

100

if

(

var

==

100

)

:

print

"

The value of the variable var is 100

"

print

"

Good bye!

"

Running the code above produces:


变量 var 的值为100
Good bye!