0: print("positive number") elif num == 0: print("zero") else: print(" negative number")"."/> 0: print("positive number") elif num == 0: print("zero") else: print(" negative number")".">
Home >Backend Development >Python Tutorial >Usage of if statement in python
Usage of if statement in python
The following examples are judged by using if...elif...else statement The number is positive, negative or zero:
Recommended: "python tutorial"
Example (Python 3.0)
# Filename : test.py # author by : www.php.cn # 用户输入数字 num = float(input("输入一个数字: ")) if num > 0: print("正数") elif num == 0: print("零") else: print("负数")
The output result of executing the above code is :
输入一个数字: 3 正数
We can also use inline if statements to achieve this:
Example (Python 3.0)
# Filename :test.py # author by : www.php.cn # 内嵌 if 语句 num = float(input("输入一个数字: ")) if num >= 0: if num == 0: print("零") else: print("正数") else: print("负数")
The output result of executing the above code is:
输入一个数字: 0 零
The above is the detailed content of Usage of if statement in python. For more information, please follow other related articles on the PHP Chinese website!