Python if Usage Examples

Document 对象参考手册Python3 Examples

The following example usesif...elif...elsestatement to determine whether a number is positive, negative, or zero:

Example (Python 3.0+)

# Filename : test.py # author by : www.example.com # User input number num = float(input("Enter a number:")) if num > 0: print("Positive") elif num == 0: print("zero") else: print("Negative")

The output of the above code is:

输入一个数字: 3
正数

We can also use nested if statements to achieve this:

Example (Python 3.0+)

# Filename :test.py # author by : www.example.com # Nested if statement num = float(input("Enter a number:")) if num >= 0: if num == 0: print("zero") else: print("Positive") else: print("Negative")

The output of the above code is:

输入一个数字: 0
零

Document 对象参考手册Python3 Examples

Other Extensions