多语言展示
当前在线:523今日阅读:168今日分享:49

python核心编程(第二版)--学习笔记--2.11

python中的if语句
工具/原料
1

python交互解释器

2

linux shell

方法/步骤
1

python中if语句格式: Python 3.4.3 (default, Nov 28 2017, 16:41:13) [GCC 4.8.4] on linuxType 'help', 'copyright', 'credits' or 'license' for more information.>>> x = 1>>> if x > 0:...     print (' x > 0 is True.')...  x > 0 is True.>>>

2

python中if ... else ... 格式:Python 3.4.3 (default, Nov 28 2017, 16:41:13) [GCC 4.8.4] on linuxType 'help', 'copyright', 'credits' or 'license' for more information.>>> x = 1>>> if x > 0 :...     print ('x > 0 is True.')... else:...     print ('x > 0 is False.')... x > 0 is True.>>>

3

python中if ... elif ... else格式:Python 3.4.3 (default, Nov 28 2017, 16:41:13) [GCC 4.8.4] on linuxType 'help', 'copyright', 'credits' or 'license' for more information.>>> x = 1>>> if x > 0:...     print ('x > 0 is True.')... elif x == 0:...     print ('x == 0 is True.')... else:...     print ('x < 0 is True.')... x > 0 is True.>>>

注意事项

注意python中的表达式格式,与其它语言不同,不需要小括号来包裹表达式。

推荐信息