for 循环提供了一种手段,能够不依靠索引取值。git
语法:api
''' for 变量名(会拿到容器类元素的每个值,没有了就结束循环) in 容器类元素: print(变量名) '''
例如:app
打印一个列表内的全部元素:函数
lt = [1, 2, 3, 4] for i in lt: print(i)
运行结果:code
1 2 3 4 Process finished with exit code 0
打印字典内的元素:对象
dic = {'a': 1, 'b': 2, 'c': 3} count = 0 for i in dic: # 对于字典,for循环只能拿到key print(i, dic[i]) count += 1
运行结果:排序
a 1 b 2 c 3 Process finished with exit code 0
range()索引
# for i in range(1,10,2): # range(起始,结束,步长),顾头不顾尾 # print(i)
运行结果:ip
1 3 5 7 9 Process finished with exit code 0
for…break:提早终止循环
只打印1-10的前两个数:
for i in range(1,11): if i == 3: break # 中断循环 print(i)
运行结果:
1 2 Process finished with exit code 0
for…continue:跳出本次循环,不执行下面的代码
打印1-5,可是不打印3:
for i in range(1,6): if i == 3: continue # 跳出本次循环,不执行下面的代码 print(i)
运行结果:
1 2 4 5 Process finished with exit code 0
for…else(了解:for循环不被break终止就执行else下的代码,不然不执行)
例如:
打印1-5:
for i in range(1,6): print(i) else: print('没有被break,我就能出来。')
运行结果:
1 2 3 4 5 没有被break,我就能出来。
打印1-5,当打印到2的时候,终止循环:
for i in range(1,6): if i == 3: break print(i) else: print('没有被break,我就能出来。')
运行结果:
1 2 Process finished with exit code 0
练习
使用代码是实现 Load......
的动态效果。
实现代码:
import time print('Loading', end='') for i in range(6): print('.', end='') time.sleep(0.5)
整形:
做用
年龄、ID……
定义方式
a = 1
或 a = int(1)
使用方法
+ - * / % // ** < <= > >= == !=
有序 or 无序(有索引的就有序,无索引的就无序)
整型没有有序无序的概念。
可变 or 不可变(值变id不变是可变,值变id变是不可变)
整形是不可变类型。
浮点型:
做用
薪资……
定义方式
a = 1.1
或 a = float(1.1)
使用方法
+ - * / % // ** < <= > >= == !=
有序 or 无序(有索引的就有序,无索引的就无序)
浮点型没有有序无序的概念。
可变 or 不可变(值变id不变是可变,值变id变是不可变)
浮点型是不可变类型。
做用
姓名……
定义方式: str
单引号/双引号/三单引号/三双引号:s = '字符串'
使用方法
优先掌握
索引取值:[字符位置]
正序从第一个字符以 0 开始;倒序从最后一个字符开始以 -1 开始
s = '一二三四' print(s[0]) # 取出第一个字符:一
结果:
一 Process finished with exit code 0
切片:[起始位置:结束位置:步长]
s = '一二三四五六七八九十' print(s[0:4]) # 顾头不顾尾 print(s[0:8:2]) # 步长2,隔一个取一个 print(s[4:0:-1]) # +从左到右,-表示从右到左 print(s[2:]) # 左边的不写取到最左边,右边的不写取到最右边
运行结果:
一二三四 一三五七 五四三二 三四五六七八九十 Process finished with exit code 0
遍历:for 循环
s = '一二三' for i in s: print(i)
运行结果:
一 二 三 Process finished with exit code 0
成员运算: in
/ not in
(返回一个布尔值 True 或 False )
s='一二三四' print('一' in s) # 判断:一是否是在字符串s里面 print('二' not in s) # 判断:二是否是不在字符串s里面
运行结果:
True False Process finished with exit code 0
strip()
:默认去除两端空格,能够指定去除的字符,能够指定多个字符同时去掉
s = ' 一二三**' print(s.strip()) print(s.strip(' 一*'))
运行结果:
一二三** 二三 Process finished with exit code 0
切割: split()
按照对象切割字符串,获得的一个列表
s = '一|二|三|四' print(s.split('|'))
运行结果:
['一', '二', '三', '四'] Process finished with exit code 0
长度: len()
:得到字符串的长度
s = '一二三四' print(len(s))
运行结果:
4 Process finished with exit code 0
须要掌握
lstrip
和 rstrip
s = '**tom**' print(s.lstrip('*')) print(s.rstrip('*'))
运行结果:
tom** **tom Process finished with exit code 0
lower
和 upper
s = 'Tom' print(s.lower()) # 改成全小写 print(s.upper()) # 改成全大写
运行结果:
tom TOM Process finished with exit code 0
startswith
和 endswith
s = 'Tom' print(s.startswith('T')) # 以……开始,返回布尔值 print(s.endswith('f')) # 以……结束,返回布尔值
运行结果:
True False Process finished with exit code 0
rsplit
s = '一|二|三|四' print(s.rsplit('|', 2)) # 2表示只切割两个对象 print(s.split('|', 2))
运行结果:
['一|二', '三', '四'] ['一', '二', '三|四'] Process finished with exit code 0
join
:拼接列表内的字符串
s = '1|2|3|4|5' lt = s.split('|') # 将字符串切割成列表 print('*'.join(lt)) # 将列表内的元素用*链接起来
运行结果:
1*2*3*4*5 Process finished with exit code 0
replace
s = '一二三' s = s.replace('一', '五') print(s)
运行结果:
五二三 Process finished with exit code 0
isdigit
和 isalpha
s = '123456' print(s.isdigit()) # 判断字符串内字符是否都为数字,返回布尔值 print(s.isalpha()) # 判断字符串内字符是否都为字母,返回布尔值
运行结果:
True False Process finished with exit code 0
了解
find|rfind|index|rindex|count
s = '一二三四五一一一' print(s.find('二', 1, 5)) # 查找字符串的索引位置(1,5表示从索引1开始查找到索引5) print(s.rfind('二')) # 和 find 的做用彻底同样 print(s.find('六')) # 当查找不到字符时,返回 -1 print(s.index('二')) # 查找字符的索引位置 print(s.count('一')) # 计算字符的个数 # print(s.index('六')) # 当查找不到字符时,程序会报错
运行结果:
1 1 -1 1 4 Process finished with exit code 0
center|ljust|rjust|zfill
s = 'title' print(s.center(10, '*')) # 设置输出长度和填充字符,将内容居中打印 print(s.ljust(10, '*')) # 设置输出长度和填充字符,将内容居左打印 print(s.rjust(10, '*')) # 设置输出长度和填充字符,将内容居右打印 print(s.zfill(10)) # 设置输出长度,在内容前面用0填充
运行结果:
**title*** title***** *****title 00000title Process finished with exit code 0
expandtabs
s = 'a\ta' print(s) print(s.expandtabs(6)) #调整缩进的尺寸
运行结果:
a a a a Process finished with exit code 0
captalize|swapcase|title
s = 'tom bruce' print(s.capitalize()) # 将字符串中的英文改成首字母大写 print(s.swapcase()) # 将字符串中的英文大写改成小写,小写改成大写 print(s.title()) # 将字符串中的每一个单词的首字母大写
运行结果:
Tom bruce TOM BRUCE Tom Bruce Process finished with exit code 0
is系列…
有序 or 无序(有索引的就有序,无索引的就无序)
有序。
可变 or 不可变(值变id不变是可变,值变id变是不可变)
不可变。
做用
存储任意类型的多个元素。
定义方式: list
lt = [1, 2, 3, 4]
使用方法
优先掌握
索引取值: [元素位置]
lt = [1, 2, 3, 4] print(lt[1])
运行结果:
2 Process finished with exit code 0
索引修改值: [元素位置] = 新值
lt = [1, 2, 3, 4] lt[1] = 3 print(lt)
运行结果:
[1, 3, 3, 4] Process finished with exit code 0
切片:[起始位置:结束位置:步长]
lt= [1,2,3,4,5,6,7] print(lt[:]) # 不写默认从开始到结尾 print(lt[2:5]) # 顾头不顾尾 print(lt[1:6:2]) # 从位置1开始到位置6结束,每隔1个元素取一个
运行结果:
[1, 2, 3, 4, 5, 6, 7] [3, 4, 5] [2, 4, 6] Process finished with exit code 0
遍历:for 循环
lt = [1, 2, 3, 4] for i in lt: # 将列表内的元素逐一取出打印 print(i)
运行结果:
1 2 3 4 Process finished with exit code 0
成员运算: in
/ not in
(返回一个布尔值 True 或 False )
lt = [1, 2, 3, 4] print(1 in lt) print(5 in lt)
运行结果:
True False Process finished with exit code 0
长度: len()
:得到字符串的长度
lt = [1, 2, 3, 4] print(len(lt))
运行结果:
4 Process finished with exit code 0
添加元素: append(值)
lt = [1, 2, 3, 4] lt.append(5) print(lt)
运行结果:
[1, 2, 3, 4, 5] Process finished with exit code 0
删除元素: del[位置]
lt = [1, 2, 3, 4] del lt[0] print(lt)
运行结果:
[2, 3, 4] Process finished with exit code 0
须要掌握
插入元素:insert(插入位置,插入元素)
lt = [1, 2, 3, 4] lt.insert(2, 0) # 在位置 2 前插入 0 print(lt)
运行结果:
[1, 2, 0, 3, 4] Process finished with exit code 0
按照索引删除值: pop(位置)
lt = [1, 2, 3, 4] lt.pop(2) print(lt)
运行结果:
[1, 2, 4] Process finished with exit code 0
按照值删除值: remove(删除对象)
lt = [1, 2, 3, 4] lt.remove(2) print(lt)
运行结果:
[1, 3, 4] Process finished with exit code 0
计数: count()
lt = [1, 1, 1, 2] print(lt.count(1))
运行结果:
3 Process finished with exit code 0
寻找值的索引: index()
lt = [1, 2, 3, 4] print(lt.index(1)) # 找到了返回索引值,找不到报错
运行结果:
0 Process finished with exit code 0
清空列表: clear()
lt = [1, 2, 3, 4] lt.clear() print(lt)
运行结果:
[] Process finished with exit code 0
拷贝列表: copy()
lt = [1, 2, 3, 4] lt1 = lt.copy() print(lt1)
运行结果:
[1, 2, 3, 4] Process finished with exit code 0
扩展列表: extend()
lt1 = [1, 2, 3] lt2 = [4, 5, 6] lt1.extend(lt2) print(lt1)
运行结果:
[1, 2, 3, 4, 5, 6] Process finished with exit code 0
反转列表: reverse()
lt = [1, 2, 3, 4] lt.reverse() print(lt)
运行结果:
[4, 3, 2, 1] Process finished with exit code 0
元素排序: sort()
lt = [2, 3, 1, 0, 4] lt.sort() # 从小到大排序 # lt.sort(reverse=True) # 从大到小排序 print(lt)
运行结果:
[0, 1, 2, 3, 4] # [4, 3, 2, 1, 0] Process finished with exit code 0
有序 or 无序(有索引的就有序,无索引的就无序)
有序。
可变 or 不可变(值变id不变是可变,值变id变是不可变)
可变。
方法是解释器内部定义好的函数。
例如咱们能够本身写一个排序方法:
class SelfList(list): def self_sort(self): for i in range(len(self)): # [0,1,2,3,4,5] for j in range(len(self)): # [0,1,2,3,4,5] if self[i] < self[j]: self[i], self[j] = self[j], self[i] lt = SelfList([2, 0, 4, 3, 5]) lt.sort() print(lt)
运行结果:
[0, 2, 3, 4, 5] Process finished with exit code 0