Python if statement
The following examples usingif ... elif ... else statement to determine the number is positive, negative or zero:
# -*- coding: UTF-8 -*- # Filename : test.py # author by : www.w3big.com # 用户输入数字 num = float(input("输入一个数字: ")) if num > 0: print("正数") elif num == 0: print("零") else: print("负数")
Execute the above code output results:
$ python test.py 输入一个数字: 3 正数
We can also use the embedded if statement to achieve:
# -*- coding: UTF-8 -*- # Filename :test.py # author by : www.w3cschool.cc # 内嵌 if 语句 num = float(input("输入一个数字: ")) if num >= 0: if num == 0: print("零") else: print("正数") else: print("负数")
Execute the above code output results:
$ python test.py 输入一个数字: 0 零