kizcorp,唐代四大类书,海城市人才网
目录
for 变量名(会拿到容器类元素的每一个值) in 容器类元素: print(变量名)
for i in range(5): print(i) # 打印结果: 0 1 2 3 4
while可以循环一切事物
for 循环提供了一种手段,不依赖索引取值
for i in range(1, 101): # 取1-100,range顾头不顾尾 if i == 50: break # 中断循环,只取到1-49 print(i)
for i in range(1, 101): if i == 51: continue # continue跳出本次循环,不执行下面代码 print(i)
for i in range(1, 101): if i == 51: break print(i) print('没有被break中断我就出来')
for循环不被break终止就执行else后面的代码,否则就不执行
import time print('loading', end='') for i in range(10): print('.', end='') time.sleep(0.2)
print(1, end='*') # print后面的end控制打印的状态 print(1, end='*') print(1, end='*') print(1, end='*') # 打印结果:1*1*1*1*
作用:描述年龄
定义方式:x = 10
使用方法:+ - * / % // **
有序or无序:数字类型没有有序无序一说
可变不可变:(值变id值变,为不可变;值变id值不变,为可变)整型不可变
作用:描述薪资
定义方式:x = 2.1
使用方法:+ - * / % // **
有序or无序:数字类型没有有序无序一说
可变不可变:(值变id值变,为不可变;值变id值不变,为可变)浮点型不可变
作用:描述姓名
定义:单引号 / 双引号 / 三单引号 / 三双引号
使用方法:
优先掌握:
s = 'hello world' print(s[1]) # 取到e
s = 'hello world' print(s[0:4]) # hell print(s[4:0:-1]) # olle print(s[:]) # hello world
s = 'hello world' print(len(s)) # 字符长度为11
s = 'hello world' print('hello' in s) # true print('hello' not in s) # false
s = ' hello world ' print(s.strip()) # hello world 默认去除两端空白 s = ' **hello world **' print(s.strip('* ')) # hello world
s = 'cwz|19|180|140' print(s.split('|')) # ['cwz', '19', '180', '140']
s = 'cwz123' for i in s: print(i) # 打印结果: c w z 1 2 3
需要掌握:
s = 'hello world' print(s.startswith('hello')) # true print(s.endswith('world')) # true
s1 = '**hello world**' s2 = '**hello world**' print(s1.lstrip('*')) # hello world** print(s2.rstrip("*")) # **hello world
lower或upper
s3 ='hello world' print(s3.lower()) print(s3.upper())
rsplit
s4 = 'cwz|123|neo|140' print(s4.split('|',1)) # ['cwz', '123|neo|140'] print(s4.rsplit('|',1)) # ['cwz|123|neo', '140']
s4 = 'cwz|123|neo|140' s_list = s4.split('|') print('*'.join(s_list) # cwz*123*neo*140
s5 = 'hello world' print(s5.replace('h', '6')) # 6ello world
s6 = '122324' print(s6.isdigit()) # true 判断是否全为数字 print(s6.isalpha()) # false 判断是否全为字母
其他操作:
s = 'my name is cwz, age is 20' print(s.find('m')) # 0 find查找索引值,找到第一个就不往后找了,找不到返回-1 print(s.rfind('s')) # 21 rfind从右往左查找索引值 print(s.index('s')) # 9 返回索引值 print(s.rindex('s')) # 21 print(s.count('s')) # 2 计数
s = 'hello world' print(s.center(20,'*')) # ****hello world***** 居中 print(s.ljust(20, '*')) # hello world********* 居左 print(s.rjust(20, '#')) # #########hello world 居右 print(s.zfill(20)) # 默认用0填充 000000000hello world
s = 'aaa\tbbb' print(s.expandtabs()) # aaa bbb 默认制表符8个字符
s = 'hello world' print(s.capitalize()) # hello world 首字母大写,其他小写 print(s.swapcase()) # hello world 大小写转换 print(s.title()) # hello world 每个单词首字母大写
有序or无序: 字符串可以索引,有序
可变or不可变:字符串值变id值改变,--》字符串不可变
作用:[]内存储多个值,各元素以逗号隔开
定义方式:lt = [1,23,4]
使用方法:
优先掌握:
lt = [1,2,3,4,5] print(lt[1]) # 2 lt[0] = 66 print(lt) # [66, 2, 3, 4, 5]
lt = [1,2,3,4,5] print(lt[0:4]) # [1, 2, 3, 4]
lt = [1,2,3,4,5] print(1 in lt) # true print(0 in lt) # false
lt = [1,2,3,4,5] for i in lt: print(i)
lt = [1,2,45] lt.append([12,3]) # [1, 2, 45, [12, 3]] print(lt)
lt = [1,2,3,4,5] del lt[4] print(lt) # [1,2,3,4]
lt = [1,23,4,[8,5],'www'] print(len(lt)) # 5
需要掌握:
# sort lt = [1,23,8,9,4,21,412] lt.sort() # 排序 print(lt) # [1, 4, 8, 9, 21, 23, 412]
# reverse 反转 lt = [9,23,1,4,0,8] lt.reverse() # 列表反转 print(lt) # [8, 0, 4, 1, 23, 9]
# remove lt = [9,23,1,4,0,8] lt.remove(9) # 按值删除 print(lt) # [23, 1, 4, 0, 8]
lt = [9,23,1,4,0,8] lt.pop(1) # 按索引删除值 print(lt) # [9, 1, 4, 0, 8]
# extend lt1 = [9,23,1,4,0,8] lt2 = [1111,2222] lt1.extend(lt2) # 扩展列表 print(lt1) # [9, 23, 1, 4, 0, 8, 1111, 2222]
# copy print([1,23,4,0].copy()) # [1, 23, 4, 0]
# clear lt = [9,23,1,4,0,8] lt.clear() # 清空列表 print(lt) # []
# count lt = [9,23,1,4,0,1,23,4] print(lt.count(1)) # 计数 2
# index lt = [9,23,1,4,0,8] print(lt.index(0)) # 返回索引值 4
# insert lt = [9,23,1,4,0,8] lt.insert(0, 777) # 按索引前插入值 print(lt) # [777, 9, 23, 1, 4, 0, 8]
有序or无序:能索引取值,列表有序
可变or不可变:列表可变
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
新手学习Python2和Python3中print不同的用法
Python基于os.environ从windows获取环境变量
网友评论