Home > Backend Development > Python Tutorial > Usage of if statement in python

Usage of if statement in python

藏色散人
Release: 2020-05-27 09:30:29
Original
8427 people have browsed it

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("负数")
Copy after login

The output result of executing the above code is :

输入一个数字: 3
正数
Copy after login

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("负数")
Copy after login

The output result of executing the above code is:

输入一个数字: 0
零
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template