1.循坏 .while 循坏
while条件:
代码块(循坏体)
执行流程:
1.判断条件是否为真. 如果真 . 执行代码块
2.再次判断条件是否为真 ......
3.当条件为假 . 执行else跳出循坏. 循坏结束
count = 1
while count <= 8:
print("你是alex么?")
print("你才是太白")
count = count + 1
while True:
s = input("请开始喷:")
if s == 'q':
break # 停止当前循环
过滤掉马化腾
if "马化腾" in s: # 在xxx中出现了xx
print("你输入的内容和草泥马有一拼. 不能输出")
continue # 停止当前本次循环. 继续执行下一次循环
print("喷的内容是:"+s)
输出1-100所有的奇数.
count = 1
while count <= 100:
if count % 2 != 0:
print(count)
count = count + 1
2.格式化输出
%s:字符串的占位符 , 可以放置任何内容(数字)
%d:数字的占位符
a = 108
s = "梁山水泊有%d个牛B的任务" % (a)
print(s)
name = "alex"
print("%s已经喜欢了沙河%%2的女生" % name) # 如果字符串中有了占位符. 那么后面的所有的%都是占位. 需要转义
print("wuse很色.喜欢了昌平%5的女生") # 这句话中没有占位符. %还是%
3.运算符有很多种
1.算数运算 + - * / % ** //
2.比较运算 == != <> < > >= <=
3.逻辑运算
4.赋值运算
5.成员运算
6.身份运算
7.位运算
逻辑运算 :
and 并且的意思 左右两端的值必须都是真 . 运算结果才是真
or 或者的意思. 左右两端有一个是真的. 结果就是真. 全部是假. 结果才能是假
not 非得意思. 原来是假. 现在是真. 非真即假. 非假即真
print(1 < 2 and 3 < 4 or 1>2 ) T
print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1) T
print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8) F
print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)F
print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) F
x or y 如果x==0 那么就是y, 否则是x
print(1 or 2) 1
print(2 or 3) 2
print(0 or 3) 3
print(0 or 4) 4
print(0 or 1 or 3 or 0 or 5) 1
print(1 and 2) 2
print(2 and 0) 0
print(0 and 3) 0
print(0 and 4) 0
print(0 or 4 and 3 or 7 or 9 and 6)
print(2 > 3 and 3) false相当于0
print(2 < 1 and 4 > 6 or 3 and 4 > 5 or 6) 6
count = 1
while count <= 10:
print( count)
count = count + 1
if count == 5:
break # 彻底停止循环. 不会执行后面的else
else: # while条件不成立的时候执行
print("这里是else")
break 结束循坏. 停止当前本次循坏
continue 结束当前本次循坏. 继续执行下一次循坏