一、for循环与range联用
1、while循环与for循环对比
count = 0
while count <= 3:
代码1
代码2
count+=1
for count in range(3)
代码1
代码2
2、range用法
range(start,end,step)
end不能取到
range(start,end)
省略步长,默认为1
range(end)
省略起始位置和步长,默认起始位置为0,步长为1
for i in range(0,5,1)
print(i)
二、可变类型与不可变类型
可变类型:值改变,id不变,证明就是在改变原值,是可变类型
不可变类型: 值改变,id也变了,证明是产生了新值,并没有改变原值,原值是不可变类型
可变类型:列表、字典
不可变类型:整型、字符串、元组、集合
三、数字类型
1、整型(int)
用途:年龄、个数、号码、出生年等
定义方式
age = 18 # age = int(18)
# int功能可以把纯数字的字符串转换成int类型
res = int("18")
#res = int("1.8") 浮点数字符串不能转换整型
print(type(res))
了解(***)
print(bin(11)) # 0b1011
print(oct(11)) # 0o13
print(hex(11)) # 0xb
2、浮点型float
1、用途:薪资、身高、体重等
2、定义方式
salary = 3.1 # salary = float(3.1)
float功能可以把浮点数组成的字符串转换成float类型
res = float("1.8")
print(type(res))
四、字符串类型
1、用途:记录描述性质的状态,例如名字、性别、国籍等
2、定义方式:在引号('',"",'''''',""""""")内包含一串字符串
s = "hello" # s = str("hello")
3、str功能可以把任意类型转换成str类型
res=str([1,2,3]) # "[1,2,3]"
print(type(res))
4、按索引取值(正向取+反响取) *字符串只能取值,不能修改也不能添加
5、切片(顾头不顾尾,步长)
s = 'hello world'
new_s = s[:]
print(new_s) #只留一个冒号,完整拷贝字符串
6、长度len
s = "hello world"
print(len(s))
7、成员运算in和not in
s = "hello world"
print("hel" in s)
print("egon" not in s) # 语义明确,推荐使用
print(not "egon" in s)
8、移除空白strip
s = " \n hel lo \t "
new_s = s.strip()
print(new_s)
print(s) # 没有改变原字符串
去除左右两边的非空白字符
print("**+=-%^#****he**llo**%^#**+=**".strip("*+=-%^#"))
9、切分split:把字符串按照某个分隔符切成一个列表
userinfo = "egon_dsb:123:18:3.1"
res = userinfo.split(":")
print(res[0])
print(res)
print("-".join(res))
10、 纯字符串组成的列表
l = ["aaaa", "bbb", "ccc"]
res = ":".join(l)
print(res, type(res))
字符串内置方法
1、strip,lstrip,rstrip
print("hello".strip(""))
print("hello".lstrip(""))
print("hello".rstrip("*"))
2、lower,upper
msg = "AbCDEFGhigklmn"
res = msg.lower()
res = msg.upper()
print(res)
res=msg.swapcase() #大小写对调
print(res)
3、startswith,endswith
msg = "sb is lxx sb"
print(msg.startswith("sb"))
print(msg.endswith("b"))
print(msg.endswith("c"))
4、split,rsplit
userinfo="egon:123:18"
print(userinfo.split(":"))
print(userinfo.split(":",1))
print(userinfo.rsplit(":",1))
5、join
6、replace
msg = "egon hello"
res=msg.replace('','').replace(' ','')
res=msg.strip('').replace(' ','')
print(res) # replace(原有内容,替代内容)
s="lxx hahah wocale lxx sb 666"
res=s.replace('lxx','sb')
res=s.replace('lxx','sb',1)
print(res)
print(s)
7、format的三种玩法
7.1 %s的方式
name = "egon"
age = 18
res1="my name is %s my age is %s" % (name,age)
print(res1)
7.2 format的方式
name = "egon"
age = 18
res1="my name is {} my age is {}".format(name,age)
res1="{0}{0}{0}{1}".format(name,age)
res1="my name is {name} my age is {age}".format(age=18,name="egon")
print(res1)
7.3 f''
name = "egon"
age = 18
res1 = f"my name is {name} my age is {age}"
print(res1)
了解:f搭配{}可以执行字符串中的代码
res=f'{len("hello")}'
print(res)
f'{print("hello")}'
# f包含的字符串可以放到多行
name = "egon"
age = 18
res1 = f"my name is {name} " \
f"my age is {age}"
{}内不能有\以及#
print(f'my name is {{egon}}')
print('胜率是 %s%%' %70) #%%->%不转义
了解:https://zhuanlan.zhihu.com/p/110406030
8、isdigit:判断字符串是否由纯数字组成
print("adsf123".isdigit())
print("123".isdigit())
print("12.3".isdigit())
age = input('>>>: ') # age = " 18 "
if age.isdigit():
age=int(age)
if age > 18:
print('猜大了')
elif age < 18:
print('猜小了')
else:
print('猜对了')
else:
print('必须输入数字,小垃圾')
有序or无序
可变or不可变(1、可变:值变,id不变。可变==不可hash 2、不可变:值变,id就变。不可变==可hash)