python 笔记 if语句的简单使用 《笨办法学Python》习题29 ——1.2
2018-01-02 10:20
821 查看
习题 29: 如果(if)目标与感悟:
•if语句的简单应用
•增值符 x += 1
ex29.py
#-*-coding:utf-8-*-
#本章涉及内容十分简单,即if sentence1: sentence2,
#if语句用法和函数有点像,第二行都需要缩进4个字符
#if语句执行过程如下:
#先执行语句一即判断语句,如果判断语句为真,则执行语句二。
#此处不一定是语句,只是一个条件,其值也可以是布尔值
peolple = 20
cats = 30
dogs = 15
if peolple < cats:
print"Too many cats! The world is doomed!"
if peolple > cats:
print"Not many cats! The world is saved!"
if peolple < dogs:
print"The world is drooled on!"
if peolple > dogs:
print"The world is dry!"
#此处出现新的符号+=,作者命名为加值符,其等同与:
#x += 1 和 x = x + 1
#其更简洁,此处dogs += 5,dogs = 20
dogs += 5
#下面3个条件都成立,所以全部输出
if peolple >= dogs:
print"People are greater than or equal to dogs."
if peolple <= dogs:
print"People are less than or equal to dogs."
if peolple == dogs:
print"People are dogs."
print "\t \t boolean test!"
print "\t -------------------------------- "
a = "test"
b = "testing"
if a != b:
print "\t \t Is true!"
print "\t\tboolean can be used"
print "\t -------------------------------- "
运行结果:
•if语句的简单应用
•增值符 x += 1
ex29.py
#-*-coding:utf-8-*-
#本章涉及内容十分简单,即if sentence1: sentence2,
#if语句用法和函数有点像,第二行都需要缩进4个字符
#if语句执行过程如下:
#先执行语句一即判断语句,如果判断语句为真,则执行语句二。
#此处不一定是语句,只是一个条件,其值也可以是布尔值
peolple = 20
cats = 30
dogs = 15
if peolple < cats:
print"Too many cats! The world is doomed!"
if peolple > cats:
print"Not many cats! The world is saved!"
if peolple < dogs:
print"The world is drooled on!"
if peolple > dogs:
print"The world is dry!"
#此处出现新的符号+=,作者命名为加值符,其等同与:
#x += 1 和 x = x + 1
#其更简洁,此处dogs += 5,dogs = 20
dogs += 5
#下面3个条件都成立,所以全部输出
if peolple >= dogs:
print"People are greater than or equal to dogs."
if peolple <= dogs:
print"People are less than or equal to dogs."
if peolple == dogs:
print"People are dogs."
print "\t \t boolean test!"
print "\t -------------------------------- "
a = "test"
b = "testing"
if a != b:
print "\t \t Is true!"
print "\t\tboolean can be used"
print "\t -------------------------------- "
运行结果:
相关文章推荐
- python 笔记 if语句中的elif和else 《笨办法学Python》习题30 ——1.2
- python 笔记 if语句的简单嵌套 《笨办法学Python》习题31 ——1.3
- python 笔记 return的使用 《笨办法学Python》习题21 ——12.28
- Python学习笔记之if语句的使用示例
- Python中if语句的说明和简单使用实例
- python 笔记 布尔表达式练习 《笨办法学Python》习题28 ——1.2
- python 笔记 python编译器初识以及split() 、pop()的使用 《笨办法学Python》习题25 ——12.29
- Python学习笔记(二)if语句的简单用法
- Python中if语句的说明和简单使用实例
- Python学习笔记(二):if语句的使用
- (hdu 简单题 128道)hdu 2004(if语句的基本使用)
- Python中条件判断语句的简单使用方法
- python中简单的if语句
- python学习笔记29(python中堆的使用)
- python中的while和for语句的简单使用
- Python中在for循环中嵌套使用if和else语句的技巧
- 【Python】if语句使用规则
- Python下opencv使用笔记(一)(图像简单读取、显示与储存)
- python pickle模块的简单使用笔记
- Python下opencv使用笔记(二)(简单几何图像绘制)