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  >  Article  >  Backend Development  >  Usage of if statement in python

Usage of if statement in python

藏色散人
藏色散人Original
2020-05-27 09:30:298308browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn