Skip to Content
Python语言及其应用(第2版)
book

Python语言及其应用(第2版)

by Bill Lubanovic
March 2022
Intermediate to advanced
522 pages
13h 52m
Chinese
Posts & Telecom Press
Content preview from Python语言及其应用(第2版)
if
语句
47
空元组
()
空字典
{}
空集
set()
其他均被视为
True
Python
程序使用这种“真”和“假”的定义来检查空数据结构以及条
件是否不成立:
>>> some_list = []
>>> if some_list:
... print("There's something in here")
... else:
... print("Hey, it's empty!")
...
Hey, it's empty!
如果你测试的是表达式,而非简单的变量,那么
Python
会评估该表达式并返回布尔值结
果。所以,如果你输入:
if color == "red":
那么
Python
会对
color == "red"
进行评估。在先前的例子中,我们将字符串
"mauve"
赋给
color
,故
color == "red"
False
Python
转而执行下一个测试。
elif color == "green":
4.5
使用
in
进行多重比较
假设你想知道一个字母是否为元音字母。一种方法是写一个冗长的
if
语句:
>>> letter = 'o'
>>> if letter == 'a' or letter == 'e' or letter == 'i' \
... or letter == 'o' or letter == 'u':
... print(letter, 'is a vowel')
... else:
... print(letter, 'is not a vowel')
... ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano
Python实用技能学习指南

Python实用技能学习指南

Posts & Telecom Press, Robert Smallshire, Austin Bingham
Python技术基础视频教程

Python技术基础视频教程

保罗·J·戴特尔
Python面向对象编程指南

Python面向对象编程指南

Posts & Telecom Press, Steven F. Lott

Publisher Resources

ISBN: 9787115586223