一、流程控制之while循环
语法:
while 条件:
循环体
else:
else语句(当条件不成立的时候执行这里 和break没关系)
判断条件是否成立。 如果成立执行循环体。然后再次判断条件,。。。。。直到条件不成立的时候跳出循环
break :终止当前本层循环(直接跳到循环的末尾)

while True:
content = input("请输入你要喷的内容,输入Q停止喷人:")
if content == 'Q':
# 退出循环
break # 彻底的打断当前本层循环
else:
print("发送给打野", content)
break示例
1 while True: 2 content = input("请输入你要喷的内容,输入Q停止喷人:") 3 if content == 'Q': 4 # 退出循环 5 break # 彻底的打断当前本层循环 6 else: 7 print("发送给打野", content)
continue:停止当前本次循环(跳到循环的开头)

num = 1
while num <= 10:
if num == 8:
num = num + 1
continue # 在continue之前改变循环变量
print(num)
num = num + 1
1 num = 1 2 while num <= 10: 3 if num == 8: 4 num = num + 1 5 continue # 在continue之前改变循环变量 6 print(num) 7 num = num + 1
pass 不表示任何内容,只是为了代码完整性占位而已

a = 1
while a < 11 :
if a == 8 :
pass
else:
print("你好")
a = a + 1
print(a)
1 a = 1 2 while a < 11 : 3 if a == 8 : 4 pass 5 else: 6 print("你好") 7 a = a + 1 8 print(a)
运行结果:
二、格式化输出
格式化输出用的是占位符——用%表示
%s 表示字符串占位符
%d 表示数字占位符
在用时只要在末尾加%加对应数据类型的变量即可

1 print("我叫%s,今年%d岁了,喜欢%s" % ("alex", 56, "银王"))
1 print("我叫%s,今年%d岁了,喜欢%s" % ("alex", 56, "银王"))
一句话中使用了格式化输出. 那么%就是占位。如果你的百分号不是作为占位。 要写成%%

1 print("你的名字是%s,你已经完成了今天80%%的工作" % name)
1 print("你的名字是%s,你已经完成了今天80%%的工作" % name)
三、基本运算符
计算机可以进行的运算分为:算术运算、比较运算、逻辑运算、赋值运算、成员运算、身份运算、位运算(后三个后面学)
算术运算
算术运算符:
+ 加, - 减,* 乘, / 除, % 取模 返回除法的余数,** 幂, // 取整除,返回商的部分
比较运算
比较运算符:
== 相于, != 不等于, <> 不等于, > 大于, < 小于, >= 大于等于, <= 小于等于
赋值运算
赋值运算符:
= 单个等号表示赋值,双等号表示比较 a = 10表示把10 赋值给变量a
+= a = a+1 <=> a += 1
-= a = a-1 <=> a -= 1
*= a = a*c <=> a *= c
/= a = a/c <=> a /= c
%= a = a%c <=> a %= c
**= a = a**c <=> a **= c
//= a= a//c <=> a //= c
逻辑运算
逻辑运算符:
and 两边都为真才真(一假则假)
or 两边都为假才假(一真则真)
not 取反
注意: 1、 python中逻辑运算的本质是取运算符两边的一个值返回,而且存在短路逻辑,即false开头的and都会被处理成false true开头的or都会被处理成true
2、0相当于false,1相当于true
3、运算符优先级顺序 从高到低 () -> not -> and -> or

print(0 or 1) # 1
print(0 or 2) # 2
print(1 or 0) # 1
print(0 and 1) # 0
print(1 and 0) # 0
print(1 or 2 and 3) # 1
print(0 or 1 and 4 > 3 or 5 < 7 and 4) # 1
1 print(0 or 1) # 1 2 print(0 or 2) # 2 3 print(1 or 0) # 1 4 print(0 and 1) # 0 5 print(1 and 0) # 0 6 print(1 or 2 and 3) # 1 7 print(0 or 1 and 4 > 3 or 5 < 7 and 4) # 1
四、字符串编码
1. ASCII 8bit 1byte 记住:大写字母A从65开始,小写字母a从97开始,数字0从48开始
2. GBK 16bit 2byte
3. Unicode 32bit 4byte
4. UTF-8 可变长度的unicode
英文: 8bit 1byte
欧洲文字: 16bit 2byte
中文 : 24bit 3byte
补充:
in 和 not in
可以判断xxx字符串是否在xxxx字符串中
1 content = input("输入内容") 2 if "擦" in content or "滚" in content : 3 print("你输入的文字含有不合法内容")
python基本数据类型回顾
1. int 整数
2. str 字符串. 不会用字符串保存大量的数据
3. bool 布尔值. True, False
4. list 列表(重点) 存放大量的数据
5. dict 字典 key: value 查找会比较快
6. set 集合 无序 不能重复
7. bytes 一堆字节。 我们程序处理的最小单位
8. tuple 元组 不可变的列表
一、int(整数)
对于一个数字除了运算几乎没有其他的操作,如果想找的话倒是有一个bit_lenght(),可以用来计算这个数的二进制长度
二、bool 布尔值
bool没有什么方法,这里说一下数据类型转换问题
x 转成 y类型 => y(x)
比如:
x 转成 int => int(x)
x 转成 str => str(x)
x 转成 bool => bool(x)
在bool转成int时,int(Ture) 是1 int(False)是0
三、字符串
字符串由 ', ", ''', """组成
字符:你能看到的单一文字符号
字符串:字符连成串,有固定顺序的
注意: 字符串是一个不可变的数据类型,不可变数据类型还有数字、元组
什么是可变数据类型和不可变数据类型?
我们知道数据在创建的的时候都会申请一个内存空间用来存储它,变量名只是指向这个内存空间。对于不可变数据类型来说,对变量的操作会改变它的内存空间(id),而对可变数据类型的操作则不会改变内存空间id

>>>a = 1
>>>id(a)
4297537952
>>> a = a + 1
>>> id(a)
4297537984
#列表
>>>line=[1,2,3,4]
>>>id(line)
4392665160
>>>line[1] = 5
>>>line
[5,2,3,4]
>>>id(line)
>>>4392665160
1 >>>a = 1 2 >>>id(a) 3 4297537952 4 >>> a = a + 1 5 >>> id(a) 6 4297537984 7 8 #列表 9 >>>line=[1,2,3,4] 10 >>>id(line) 11 4392665160 12 >>>line[1] = 5 13 >>>line 14 [5,2,3,4] 15 >>>id(line) 16 >>>4392665160
1.1 索引
索引就是字符串中每个元素所对应的位置,索引从0开始

1 >>>str = hello 2 >>>str[o] = h 3 >>>str[1] = e 4 >>>str[-1] = o
1 >>>str = hello 2 >>>str[o] = h 3 >>>str[1] = e 4 >>>str[-1] = o
1.2 切片
切片语法:
str[start:end:step]
start是开始索引
end是结束索引,但结束索引对应元素取不到(顾头不顾腚)
step 表示步长,即多少元素中取一个,如果是2的话就是两个元素中取一个
步长是整数表示从左往右取,是负数表示从右往左取,默认是1

>>>str = "hello world"
>>>str[0:3]
hel
>>>str[0: ]
hello world
>>>str[-3:-1]
rld
>>>str[ : :-1]
dlrow olleh
1 >>>str = "hello world" 2 >>>str[0:3] 3 hel 4 >>>str[0: ] 5 hello world 6 >>>str[-3:-1] 7 rld 8 >>>str[ : :-1] 9 dlrow olleh
1.3 大小写转来转去
s ="hello old boy"
s.capitalize() 将句子首字母转成大写
s.title() 将特殊符号隔开的单词首字母都转为大写
s.upper() 全部转为大写
s.lower() 全部转为小写
s.casefold() 全部转为小写 和lower()的区别是 lower()对某些字母支持不够好,而这个支持所有字母,比如东欧一些字母
s.swapcase() 大小写互相转换
1.4 切来切去
str = "old boy "
str.center(10 , *) 居中 把str扩展到长度为10,用*填充
str.strip() 去掉字符串两端的空白(包括空格,换行符, ),
str.lstrip() 去掉左边的空白
str.rstrip() 去掉右边的空白
str.strip("s") 去掉两边的指定字符序列,只要是两端有序列中的内容都会去掉

1 name = "aleX leNb" 2 print(name.strip("ab")) #移除开头的a和结尾的b
1 name = "aleX leNb" 2 print(name.strip("ab")) #移除开头的a和结尾的b
str.replace("old", "new") 字符串替换 把old替换成new
str.split(" x ") 根据 x 对字符串进行切割,得到的是一个列表,如果切割符在左右两端. 那么 一定会出现空字符串。(深坑请留意)
如果split()没给值,会根据空白切割得到一个列表,并将空字符串从列表中删除(在操作文件时有时候会很方便)
1.5 格式化字符串方式二
使用.format()方法

# 格式化输出
s12 = "我叫%s, 今年%d岁了, 我喜欢%s" % ('sylar', 18, '周杰伦')
# 之前的写法
print(s12)
s12 = "我叫{}, 今年{}岁了, 我喜欢{}".format("周杰伦", 28, "周润发")
# 按位置格式化
print(s12)
s12 = "我叫{0}, 今年{2}岁了, 我喜欢{1}".format("周杰伦", "周润发", 28)
# 指定位置
print(s12)
s12 = "我叫{name}, 今年{age}岁了, 我喜欢{singer}".format(name="周杰伦", singer="周润
发", age=28)
# 指定关键字
print(s12)
1 # 格式化输出 2 s12 = "我叫%s, 今年%d岁了, 我喜欢%s" % ('sylar', 18, '周杰伦') 3 # 之前的写法 4 print(s12) 5 s12 = "我叫{}, 今年{}岁了, 我喜欢{}".format("周杰伦", 28, "周润发") 6 # 按位置格式化 7 print(s12) 8 s12 = "我叫{0}, 今年{2}岁了, 我喜欢{1}".format("周杰伦", "周润发", 28) 9 # 指定位置 10 print(s12) 11 s12 = "我叫{name}, 今年{age}岁了, 我喜欢{singer}".format(name="周杰伦", singer="周润 12 发", age=28) 13 # 指定关键字 14 print(s12)
1.6 查找
str = "我喜欢python"
str.startswith("x") 是否以 x 开头
str.endswith("x") 是否以 x 结尾
str.count("x") x在str中出现的次数
str.find("x") 查找str中是否存在 x ,有就返回索引值,没有返回-1
str.find("x", a , b) 切片查找
str.index("x") 查找 x 的索引值,有就返回,没有报错
1.7 条件判断
str.isalnum() 是否由字母和数字组成
str.isdigital() 是否由数字组成
str.isdecimal() 检查字符串是否只包含十进制字符
str.isnumeric() 是否由数字组成,比较厉害,中文数字都能识别
str.isalphl() 是否由字母组成
1.8 计算字符串长度
len() 函数
str = "我是不是你最爱的人"
print(len(str)) #输出str长度
1.9 迭代
for 变量 in 可迭代对象:
循环体(break,continue)
else:
else语句

'''用for循环对s="321"进行循环,打印的内容依次是:"倒计时3秒","倒计时
2秒","倒计时1秒","出发!"。
'''
s = "321"
for c in s:
# print("倒计时%s秒" % c)
print("倒计时{}秒".format(c))
else:
print("出发")