1、循环正常结束是指没有中间截断,即没有执行break;
for i in range(10)
print(i)
else:
print("循环正常结束")
2、嵌套循环
for i in range(10)
for j in range(10)
print(i,j)
3.break 只会跳出当前循环
for i in range(10)
for j in range(10)
print(i,j)
if j >5:
break
4.for 循环,小循环不走
for i in range(10)
for j in range(10)
print(i,j)
if j >5:
break
print(i,j)
5.continue 跳出当次循环,继续下一次循环
for i in range(10)
for j in range(10)
print(i,j)
if j < 5:
continue
print(i,j)
6.break 跳出当前层循环,只跳一层;,continue跳出当次循环
for i in range(10)
for j in range(10)
print(i,j)
if j < 5:
continue
print(i,j)
7.数据类型
变量:用来记录状态
变量值的变化即状态的变化,程序运行的本质就是来处理一系列状态的变化
数据类型存在的原因:利用不同类型的数据来表示数据变化
8.海峰的博客之 数据类型
python2 对数据长度有限制
python3 对数据长度没有限制
python3整形长度没有限制
9.布尔类型表示
bool(0)& bool(1)
10.浮点型
工资,有零钱有整钱
数据类型:
数字:整形,长整,布尔,浮点,复数
11.复数 x+yj
12.字符串
前面加r,打印引号之内所有内容
print("a\nsdf")
print(r"a\nsdf")
13.单引号套双引号
print("i'm laura")
14.首字母大写capitalize
msg = 'hello world'
print (msg.capitalize())
15.字符串居中显示 center
print (msg.center(30,"*"))
16.count
print (msg.count("l"))
17.位置[]
print (msg[3])
18.count
print (msg.count("l",4,10))
19.顾头不顾尾
打印从头开始,尾号-1的区间,不要尾号
20.endswith
print (msg.endswith("d"))
21.指定空格tab
msg1='a\tb'
print (msg1) #指定空格tab
22.tab长度指定
print (msg1.expandtabs(10))
23.一个tab建,是一个空格 pycharm 把tab建换成4个空格
24.msg.find('l'),找索引
print (msg.find('d'))
25.索引对应字符串
print ('{0}{1}{0}'.format('name','age'))
26.不写索引的数字,但必须全部一一对应
print ('{name}'.format(name = 'alex'))
28.index
msg2='a1234'
print(msg.index('e'))
29.isalnum
print(msg2.isalnum())
30.isalnum
msg3='123aA'
print(msg3.isalnum())
31.isalnum()
msg4='123a_A'
print (msg4.isalnum())
32.isalpha()
msg4='123a_A'
print(msg4.isalpha())
33.isdecimal() 十进制是否
msg5='10'
print (msg5.isdecimal())
34.isdigital isnumeric
msg6='10.2'
print(msg6.isnumeric())
35.isidentifier 单词是否包含关键字
msg7='if'
print(msg7.isidentifier())
36.islower(0
msg7='if'
print(msg7.islower())
37.isspace() 是否空格
msg9='\t'
print(msg8.isspace())
print(msg9.isspace())
38.istital () 单词首字母都是大写
msg10='Haibao'
print(msg10.istitle())
39.isupper()
msg10='Haibao'
print(msg10.isupper())
40.ljust 左对齐
msg10='Haibao'
print(msg10.ljust(10,'*'))
41..rjust()右对齐
msg10='Haibao'
print(msg10.rjust(10,'*'))
42..center()居中
msg10='Haibao'
print(msg10.center(10,'*'))
43.lower 转换成小写
msg10='Haibao'
print (msg10.lower())
44.upper()全转换成大写
msg10='Haibao'
print (msg10.upper())
45.find index 区别
msg14='hello'
print(msg14.find('w')) -->-1
print(msg14.index('o')) -->4
46. strip去掉空格
msg11=' woaini '
print (msg11.strip())
47.strip()去掉右边空格
msg11=' woaini '
print (msg11.rstrip())
48.strip()去掉左边空格
msg11=' woaini '
print (msg11.lstrip())
49.maketrans
msg12='my name is laura'
table=str.maketrans('laura','haiba')
table1=str.maketrans('l','k')
print(msg12.translate(table))
print(msg12.translate(table1))
50.maketrans 部分
msg13='my name is laura'
table=str.maketrans('m','q')
51.zfill 右对齐,不够用0填充
msg13=' abc'
print(msg13.zfill(20)) #--->00000000000000000abc
print(msg13.ljust(20,'0')) #--->abc00000000000000
52打印索引
msg13=' abceddd'
print(msg13[4])
53.打印切片 顾头不顾尾
msg13=' abceddd'
print(msg13[0:5])
print(msg13[0:-1])
54.完全切分
print(msg13[:])
55.:冒号切分
print(msg13[:])
56.步长
msg13=' abceddd'
print(msg13[2:4:2]) #步长 是 2
57.len
msg13=' abceddd'
print(len(msg13))
58.round
msg13=' abceddd'
print(round(2.5))
print(round(2.6))
59.运算符
print(1+2)
print(1-2)
print(1*5)
print(3/2)
print(3**2)
60.地板除
print(3.4//2) #地板除
61.取余数
print(5%3)
62.比较 ==
print(3==2)
print(2==2)
****** 以上练习
msg = 'hello world'
print (msg.capitalize())
print (msg.center(30,"*"))
print (msg.count("l"))
print (msg[3])
print (msg.count("l",4,10))
print (msg.endswith("d"))
msg1='a\tb'
print (msg1) #指定空格tab
print (msg1.expandtabs(10))
print (msg.find('d'))
print ('{0}{1}{0}'.format('name','age'))
print ('{name}'.format(name = 'alex'))
print (msg.index('e'))
msg2= 'a124'
print (msg2.isalnum())
msg3='123aA'
print(msg3.isalnum())
msg4='123a_A'
print (msg4.isalnum())
print(msg4.isalpha())
msg5='10'
print (msg5.isdecimal())
msg6='10.2'
print(msg6.isnumeric())
msg7='if'
print(msg7.isidentifier())
print(msg7.islower())
msg8='f\n'
msg9='\t'
print(msg8.isspace())
print(msg9.isspace())
msg10='Haibao'
print(msg10.istitle())
print(msg10.isupper())
print(msg10.ljust(10,'*'))
print(msg10.rjust(10,'*'))
print(msg10.center(10,'*'))
print (msg10.lower())
print (msg10.upper())
msg11=' woaini '
print (msg11.strip())
print (msg11.rstrip())
print (msg11.lstrip())
msg12='my name is laura'
table=str.maketrans('laura','haiba')
table1=str.maketrans('l','k')
print(msg12.translate(table))
print(msg12.translate(table1))
msg13=' abc'
print(msg13.zfill(20))
print(msg13.ljust(20,'0'))
print(msg13[4])
print(msg13[0:5])
print(msg13[0:-1])
print(msg13[:])
print(msg13[2:4:2]) #步长 是 2
print(len(msg13))
print(round(2.5))
print(round(2.6))
print(1+2)
print(1-2)
print(1*5)
print(3/2)
print(3**2)
print(3.4//2) #地板除
print(5%3)
print(3==2)
print(2==2)
******
63.比较运算符号
print(3 > 2)
print(3 < 2)
print(3 >= 2)
print(3 <= 2)
64.赋值运算
print(a+=10) #a+=1 <== > a=a+1
65.age=age/3 与 age/=3一样
66.位运算 二进制按位置进行对比 异或运算
>>> 7&5
5
>>> bin(7)
'0b111'
>>> bin(5)
'0b101'
>>> 7|5
7
67.<< 左移动运算符
3<<2
12
68.~ 按位取反运算
~6
-7
69.逻辑
a=1
a>1
False
70.and or
a=1
b=3
a>1 and b<4
False
71.所有数字类型都自带布尔值 非0的数字的布尔值都是true,0=false
>>> bool(0)
False
>>>bool(5)
True
72. 1自带布尔值true
if 1:
print('ok')
....
ok
73 if 0:
print('ok')
....
74.空格 true,空=false
msg=' ' <==> True
msg='' <==> False
75.true and true
>>> 10 and 32
32
76.true and false
>>> 10 and 0
77成员运算 只能用于字符串
>>> msg = 'abce'
>>> '3'in msg
False
>>> 'a' in msg
True
78.缓存策略
>>> age=10
>>> id(age)
490865392
>>> age1=10
>>> id(age1)
490865392
>>> age1=10 -----> age1=age
79.太大了,就缓存
>>> age=10
>>> id(age)
490865392
>>> age1=10
>>> id(age1)
490865392
>>> age1=10 -----> age1=age
80.is内存 是否存入缓存 ,长的地址不一样,短的就一样,小数据缓存机制,大数据估计都要另辟内存。
>>> name='haibao'
>>> name1=name
>>> id(name)
18066096
>>> id(name1)
18066096
>>>
******
>>> age=111111111111
>>> age1=111111111111
>>> age is age1
False
>>> age3=10
>>> age4=10
>>> age3 is age4
True
>>>
81.死循环 while loop
count=0
while True:
print("i am laura")
count+=1
82. 打印占用时间
count=0
while True:
if count == 10000000
print("i am laura")
break
count+=1
83.while 加条件
count=0
while count < 1000:
print("i am laura")
break
count+=1
84.判断输入的不是数字,则继续
guess_age = input("age:")
if guess_age.isdigit():
guess_age = int(guess_age)
else:
continue
85.猜年龄 while 循环
count=0
oldboy_age = 56
while count < 3:
guess_age = input("age:")
if guess_age.isdigit():
guess_age = int(guess_age)
else:
continue
if guess_age == oldboy_age:
print('you are right')
break
elif guess_age > oldboy_age:
print('guess smaller')
else:
print('guess a little bigger')
count +=1
86. 取名字方法之 笨方法
>>> names = "luhaibao,luhailing,luhaiwen"
>>> names 'luhaibao,luhailing,luhaiwen'
>>> names[9] 'l'
>>> names[9:19]
'luhailing,'
87.空列表 列表类型
>>> names = []
>>>type(names)
>>><class'list'>
88.取names 第一个名字
>>>names = ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wuqi']
>>>names[1]
>>>'lisi'
89.切片取
>>>names = ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wuqi']
>>>names[1:3]
>>>'lisi', 'wangwu'
90.增加同时只能插入一个
names = ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wuqi']
names.append('laura')
print(names)
92.定位插入 同时只能插入一个
names = ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wuqi']
names.insert(2, 'loofoo')
print(names)
93.删除
names = ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wuqi']
names.remove('laura')
94.删除 三种方式
names = ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wuqi']
names.remove('laura')
del names[0]
print(names)
print(names.pop(2))
print(names)
95.改名字
names = ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wuqi']
names[2] = 'haibao'
96.查询
names = ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wuqi']
print(names[-3])
97.跳跃1一个
names = ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wuqi']
print(names[0::2])
********
98.最后3个
names = ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wuqi']
print(names[-3:])
99.取前三个
names = ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wuqi']
print(names[0:3])
100.取元素下标
names = ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wuqi']
print(names.index('zhaoliu'))
101.切片打印的位置是第二个位置
names = ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wuqi']
print(names.index('zhaoliu'))
first_index = names.index('zhaoliu')
second_index = names.[first_index+1:].index('wuqi')
print('second',second_index)
102.查询其他的
names = ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wuqi']
print(names.index('zhaoliu'))
first_index = names.index('zhaoliu')
second_index = names.[first_index+1:].index('wuqi')
print('second',second_index+first_index+1)
103.count
names = ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wuqi']
print('count',names.count('zhaoliu'))
104.清空列表
names = ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wuqi']
names.clear()
print(names)
105.extend 计算机认为字符串本身也是一个列表,所以也能合并,合并 extend namez 仍然存在
names = ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wuqi']
namez= ['zhenwwgsan', 'liwwi', 'wangwwwu', 'zhwwaoliu', 'wwwuqi']
names.extent(namez)
print(names)
106.reverse
names = ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wuqi']
print(names)
names.reverse()
print(names)
108.python3 字符串和数字无法排序
names = ['zhangsan',99,658, 'lisi', 'wangwu', 'zhaoliu', 'wuqi']
print(names)
names.sort()
print(names)
109.sort 按ascii 顺序编写
names = ['zhangsan','99','658', 'lisi', 'wangwu', 'zhaoliu', 'wuqi']
print(names)
names.sort()
print(names)
110.copy
names = ['zhangsan','99','658', 'lisi', 'wangwu', 'zhaoliu', 'wuqi']
print(names)
namec=names.copy()
print(namec)
111.两种方法区别
names = ['zhangsan','99','658', 'lisi', 'wangwu', 'zhaoliu', 'wuqi']
print(names)
namec=names.copy()
namef=names
print(namec,id(names),id(namec))
print(namef,id(names),id(namef))
112 n4=names names变,n4也变,即相当于别名,软连接,用的是同一份数据。
copy出来的数据,复制一份,保留了原来数据,被copy的数据有变化,新copy的数据没有变化
113.不能这样打下标
names = ['zhangsan','99','658', 'lisi', 'wangwu', 'zhaoliu', 'wuqi']
for i in names:
print(i,names.index(i))
114打印下标正确方法
names = ['zhangsan','99','658', 'lisi', 'wangwu', 'zhaoliu', 'wuqi']
for i in enumerate(names):
115.打印下标正确方法
names = ['zhangsan','99','658', 'lisi', 'wangwu', 'zhaoliu', 'wuqi']
for i in enumerate(names):
116.获取编号
names = ['zhangsan','99','658', 'lisi', 'wangwu', 'zhaoliu', 'wuqi']
for i ,ele in enumerate(names):
117.获取编号二
names = ['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'wuqi']
good = ['good','bad','just soso']
for i ,ele in enumerate(names):
print(i,ele,good[i])
output:
0 zhangsan good
1 lisi bad
3 wangwu justsoso
118.嵌套列表 在a的【1】(即第二位)位置 写入一个列表
>>> a = ['haibao','hailing','haiwen']
>>> a
['haibao', 'hailing', 'haiwen']
>>> a[1]
'hailing'
>>> a[1]=['hailing','doctor','doctor']
>>> a
['haibao', ['hailing', 'doctor', 'doctor'], 'haiwen']
>>> a[1][1]
'doctor'
>>>
119.enumerate
products =[
['iphone4',4800],
['iphone5',5800],
['iphone6',6800],
['iphone7',7800],
]
for i, ele in enumerate(products):
print(i,ele[0],ele[1])
output:
0 iphone4 4800
1 iphone5 5800
2 iphone6 6800
3 iphone7 7800
121.三级菜单
menu ={
"北京市":{
"东城区":["东华门街道","景山街道","交道口街道","安定门街道","北新桥街道","朝阳门街道","东四街道","建国门街道","东直门街道","前门街道","崇文门外街道","东花市街道","龙潭街道","体育馆路街道","天坛街道","永定门外街"],
"西城区":["西长安街街道"," 新街口街道","月坛街道","展览路街道","德胜街道","金融街街道","什刹海街道","大栅栏街道","天桥街道","椿树街道","陶然亭街道","广内街道","牛街街道","白纸坊街道","广外街道"],
"朝阳区":["建外街道","朝外街道","呼家楼街道","三里屯街道","左家庄街道","香河园街道","和平街街道","安贞街道","亚运村街道","小关街道","酒仙桥街道","麦子店街道","团结湖街道","六里屯街道"],
"丰台区":["右安门街道","太平桥街道","西罗园街道","大红门街道","南苑街道","东高地街道","东铁匠营街道","卢沟桥街道","丰台街道","新村街道","长辛店街道","云岗街道","方庄地区","宛平城地区","马家堡街道","和义街道","卢沟桥地区","花乡地区","南苑地区","长辛店镇","王佐镇"],
"通州区":["中仓街道","新华街道","北苑街道","玉桥街道","永顺街道","梨园街道","宋庄镇","张家湾镇","漷县镇","马驹桥镇","西集镇","台湖镇","永乐店镇","潞城镇","于家务回族乡"],
"石景山区":["八宝山街道","老山街道","八角街道","古城街道","苹果园街道","金顶街街道","广宁街道","五里坨街道","鲁谷街道"],
"海淀区":["万寿路街道","永定路街道","羊坊店街道","甘家口街道","八里庄街道","紫竹院街道","北下关街道","北太平庄街道","学院路街道","中关村街道","海淀街道","青龙桥街道","清华园街道","燕园街道","香山街道","清河街道","花园路街道","西三旗街道","马连洼街道","田村路街道","上地街道","万柳地区","东升地区","曙光街道","温泉镇","四季青镇","西北旺镇","苏家坨镇","上庄镇"],
"房山区":["城关街道","新镇街道","向阳街道","东风街道","迎风街道","星城街道","良乡地区","周口店地区","琉璃河地区","拱辰街道","西潞街道","阎村镇","窦店镇","石楼镇","长阳镇","河北镇","长沟镇","大石窝镇","张坊镇","十渡镇","青龙湖镇","韩村河镇","霞云岭乡","南窖乡","佛子庄乡","大安山乡","史家营乡","蒲洼乡"],
"门头沟区":["大峪街道","城子街道","东辛房街道","大台街道","王平街道","潭柘寺镇","永定镇","龙泉镇","军庄镇","雁翅镇","斋堂镇","清水镇","妙峰山镇"],
"大兴区":["兴丰街道","林校路街道","清源街道","亦庄地区","黄村地区","旧宫地区","西红门镇","瀛海地区","观音寺街道","天宫院街道","青云店镇","采育镇","安定镇","礼贤镇","榆垡镇","庞各庄镇","北臧村镇","魏善庄镇","长子营镇"],
"怀柔区":["怀柔地区","泉河街道","龙山街道","雁栖地区","庙城地区","北房镇","杨宋镇","桥梓镇","怀北镇","汤河口镇","渤海镇","九渡河镇","琉璃庙镇","宝山镇","长哨营满族乡","喇叭沟门满族乡"],
"平谷区":["滨河街道","兴谷街道","渔阳地区","峪口镇","马坊地区","金海湖地区","东高村镇","山东庄镇","南独乐河镇","大华山镇","夏各庄镇","马昌营镇","王辛庄镇","大兴庄镇","刘家店镇","镇罗营镇","黄松峪乡","熊儿寨乡"],
"密云区":["鼓楼街道","果园街道","檀营地区","密云镇","溪翁庄镇","西田各庄镇","十里堡镇","河南寨镇","巨各庄镇","穆家峪镇","太师屯镇","高岭镇","不老屯镇","冯家峪镇","古北口镇","大城子镇","东邵渠镇","北庄镇","新城子镇","石城镇"],
"延庆区":["百泉街道","香水园街道","儒林街道","延庆镇","康庄镇","八达岭镇","永宁镇","旧县镇","张山营镇","四海镇","千家店镇","沈家营镇","大榆树镇","井庄镇","大庄科乡","刘斌堡乡","香营乡","珍珠泉乡"],
"顺义区":["胜利街道","光明街道","仁和街道","后沙峪街道","天竺街道","杨镇街道","牛栏山街道","南法信街道","马坡街道","石园街道","空港街道","双丰街道","旺泉街道","高丽营镇","
作业
1.购物车
2.三级菜单