Python中的if语句是类似的其它语言的。 if语句包含使用该数据进行比较,并根据比较的结果做出了决定的逻辑表达式。
语法:
if语句在Python编程语言的语法是:
if expression:
statement(s)
Copy after login
如果布尔表达式的计算结果为true,那么if语句块将被执行。如果if语句布尔表达式计算为false,那么第一组代码将被执行。
Python编程语言的假定任何非零和非null为true,如果是zero或null,则假定为false值。
例子:
#!/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
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
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31