for while循环,编码,元组
1. for和while
continue
break
1.
for i in range(1,10):
print(i)
for i in range(10,20):
print(i)
输出:1,10,11,12,13,14,15,16,17,18,19,2,10,11,12......
2.
for i in range(1,10):
print(i)
break #跳出循环,终止所有循环!
for i in range(10,20):
print(i)
输出:1
3.
for i in range(1,10):
print(i)
for i in range(10,20):
print(i)
break
输出:1,10,11,12,13,14,15,16,17,18,19
4.易错题!
for i in range(1,10):
print(i)
for i in range(10,20):
print(i)
break
break
输出:1,10
5.易错题!
for i in range(1,10):
print(i)
for i in range(10,20):
print(i)
break
输出:1,10,2,10,3,10,4,10,5,10,6,10,7,10,8,10,9,10
6.
flag = False
for i in range(1,10):
print(i)
if flag: #为True则进入
break
for i in range(10,20):
print(i)
flag = True #flag值改为True
break
输出:1,10,2
7.
for i in range(1,10):
print(i)
continue #跳到第一个for循环,永远执行不到下面
for i in range(10,20):
print(i)
输出:1,2,3,4,5,6,7,8,9
8.
for i in range(1,10):
print(i)
for i in range(10,20):
continue
print(i)
输出:1,2,3,4,5,6,7,8,9
9.
for i in range(1,10):
print(i) # 1,2
for j in range(i,10): # 2,9
print(j)
输出:1,1,2,3,4,5,6,7,8,9,2,2,3,4,.....
10.
for i in range(1,10):
print(i)
for j in range(10,i,-1):
print(j)
输出:1,10,9,8,7,6,5,4,3,2,2,10,9,8,....
2. 编码
ascii - 8
unicode - 16,32
utf-8 - 8 ~ 32
gbk - 16
3.,
# 1.列表,查看是否存在其中
if 11 in li:
pass
# 2.字典,查看key是否在其中
"k1" in dic
for item in dic:
print(item)
for k in dic.keys():
print(k)
if "k1" in dic:
pass
# 3. 字典,查看value是否在其中
查看v是否存在,v1 in dic.values():
# 4. 字典,查看value是否在其中
循环实现,检查“v1”是否在字典 dic = {'k1': 'v1','k2': 'v2'} 的值中。
val = "v1"
# for val in dic:
# for val in dic.values():
# for v in dic.values():
# if val in v:
# print(True)
result = False
for item in dic.values():
if item == val:
result = True
break
print(result)
4.py2和py3区别:
-
raw_input()
input()
-
range()
xrange()
range()
-
ascii(#-*-encoding:utf-8 -*-)
utf-8
-
print "alex"
print('alex')
5.
1. 索引为奇数值,删除
# 删除列表元素时,会影响列表长度,从而使得索引取值时,容易出现错误。
li = [11,22,33,44,66]
li = [11, 22, 33, 44, 66]
# 索引为奇数值,删除
for i in range(0,len(li)): #程序会报错!删除列表元素时,会影响列表的长度,从而使得索引取值时,容易出现错误
del li[i]
print(li)
解决方案一:
li = [11, 22, 33, 44, 66] new_list = [] # 索引为奇数值,删除 for i in range(0,len(li)): if i%2 == 0: #删除索引值为奇数的,所以把索引值为偶数的添加到新列表! new_list.append(li[i]) li = new_list print(li)
解决方案二: 从后往前删!
li = [11, 22, 33, 44, 66] # 索引为奇数值,删除 for i in range(len(li)-1,-1,-1): if i%2 == 1: del li[i] print(li)
方法三:切片加步长:
li = [11, 22, 33, 44, 66] del li[0:4] del li[0:4:2] print(li)
6.
1.tuple
- 元素,不可变
- 只有一个元素时,一定加逗号
2.字典
# 出错点
# 题:dic = {'k1':"v1",'k2':'v2'}把key="k1",键值对删除
# del dic['k1']
# 题:dic = {'u1':"v1",'k2':'v2','k3':'v3'}把key中存在k,键值对删除
#
dic = {'u1':"v1",'k2':'v2','k3':'v3'}
# 不要在循环当前字典的过程中,修改当前字典大小
# 错误
# for key in dic:
# if 'k' in key:
# del dic[key]
# 正确方式
dic_key_list = [] #创建一个空列表
for key in dic:
dic_key_list.append(key)#把所有的键加入到空列表
for row in dic_key_list: #循环查找空列表
if 'k' in row: #如果键中含字符'k'
del dic[row] #删除键值对
print(dic)
7.数据类型转换
v = ('周', 2, 3,)
val = list(v)
print(val)
v = ['周', 2, 3]
val = tuple(v)
print(val)
v = '周军豪'
v1 = list(v)
print(v1)
['周','军','豪']
8.布尔值
- True,False
- False: 0,None,"",{},[],()
练习题补充:
1
# 如果用户输入的内容中存在任意关键字,则提示用户重新输入
# 否则,打印结束
li = ['老狗','成绩','海角']
li = ['老狗', '成绩', '海角']
while True:
result = True
cun = input('qingshurunri')
for i in li:
if i in cun:
print('请重新输入')
result = False
break
if result == True:
print(cun)
break
2.
li = ['eric','alex']
v = li.append('seven')
print(v) # None
print(li) # ['eric','alex','seven']
3.
li = [11,22,33,44,55,66,77,88,99]
dic = {'k1':[],'k2':[]} #创建一个字典,分别把k1,k2的值创建一个空列表
for item in li:
if item > 66:
dic.get('k1').append(item)
# dic['k1'].append(item)
elif item < 66:
dic.get('k2').append(item)
4.把类表中小于66的值放到字典的'k1'中,将大于66的值放到字典的'k2'中
li = [11, 22, 33, 44, 55, 66, 77, 88, 99]
dic = {}
for i in li:
if i < 66:
if 'k1' not in dic: #如果字典dic中没有'k1'这个键,则创建'k1','k1'对应的值是一个列表,然后把第一个循环的值放入列表
dic['k1'] = [i,]
else:
dic['k1'].append(i)#向'k1'对应的值的列表加入
if i > 66:
if 'k2' not in dic:
dic['k2'] = [i,]
else:
dic['k2'].append(i)
print(dic)
3.
既然选择了远方,便是风雨兼程...