第二章 条件控制语句和循环语句
- 比较运算符
运算符 | 名称 |
---|---|
== | 等于 |
!= | 不等于 |
> | 大于 |
< | 小于 |
>= | 大于或等于 |
<= | 小于或等于 |
- 逻辑运算符
#运算符
and #两语句都为真返回True
or #两语句不都为假返回True
not #反转结果,结果为假返回True,结果为真返回False
- 算术运算符
运算符 | 名称 |
---|---|
+ | 加号 |
- | 减号 |
* | 乘号 |
/ | 除号 |
% | 取模 |
** | 幂 |
// | 去整除 |
- while循环,for循环
# while循环,只要条件为真,我们就可以执行一组语句
int i=3
while(i<8){
print(i)
i+=i
}
#for循环用于迭代顺序(列表,元组,字典,集合)
fruits=["apple","banbana","cherry"]
for x in fruits:
print(x)
- break和continue
#不打印香蕉
fruits=["apple","banbana","cherry"]
for x in fruits:
if x="banana":
continue
print(x)
#当x为banana时候退出循环
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
break
print(x)