Home > Backend Development > Python Tutorial > Python入门教程之if语句的用法

Python入门教程之if语句的用法

WBOY
Release: 2016-06-10 15:12:41
Original
1048 people have browsed it

 Python中的if语句是类似的其它语言的。 if语句包含使用该数据进行比较,并根据比较的结果做出了决定的逻辑表达式。
语法:

if语句在Python编程语言的语法是:

if expression:
  statement(s)

Copy after login

如果布尔表达式的计算结果为true,那么if语句块将被执行。如果if语句布尔表达式计算为false,那么第一组代码将被执行。

Python编程语言的假定任何非零和非null为true,如果是zero或null,则假定为false值。

2015514104636191.jpg (272×365)

例子:

#!/usr/bin/python

var1 = 100
if var1:
  print "1 - Got a true expression value"
  print var1

var2 = 0
if var2:
  print "2 - Got a true expression value"
  print var2
print "Good bye!"

Copy after login

当执行上面的代码,产生以下结果:

1 - Got a true expression value
100
Good bye!

Copy after login

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