标签:xrange app 存在 逗号 name 相互 logs key pytho
1. 数据类型
int
str
list
tuple
dict
2.for和while
1 #continue 2 #break 3 4 for i in range(1,10): 5 print(i) 6 for i in range(10,20): 7 print(i) 8 9 for i in range(1,10): 10 print(i) 11 break 12 for i in range(10,20): 13 print(i) 14 for i in range(1,10): 15 print(i) 16 for i in range(10,20): 17 print(i) 18 break 19 20 for i in range(1,10): 21 print(i) 22 for i in range(10,20): 23 print(i) 24 break 25 break 26 27 for i in range(1,10): 28 print(i) 29 for i in range(10,20): 30 print(i) 31 break 32 33 flag = False 34 for i in range(1,10): 35 print(i) 36 if flag: 37 break 38 for i in range(10,20): 39 print(i) 40 flag = True 41 break 42 43 ================== 44 for i in range(1,10): 45 print(i) 46 continue 47 for i in range(10,20): 48 print(i) 49 50 for i in range(1,10): 51 print(i) 52 for i in range(10,20): 53 continue 54 print(i) 55 =================== 56 for i in range(1,10): 57 print(i) # 1,2 58 for j in range(i,10): # 2,9 59 print(j) 60 61 62 63 for i in range(1,10): 64 print(i) 65 for j in range(10,i,-1): 66 print(j)
3. 编码
ascii - 8
unicode - 16,32
utf-8 - 8 ~ 32
gbk - 16
4. 字符串格式化
- "asdf%s,sdfsdf%d" %(‘11‘,4)
- "asdf%s,sdfsdf" %‘11‘
- "asdf%s,sdfsdf%%" %‘11‘
1 tpl = "我叫%sfdf" 2 name1 = tpl %(‘骑兵‘,) 3 name2 = tpl %(‘步兵‘,) 4 print(name1,name2)
5. 变量
name = ‘xxx‘
注意:
不要使用python中已经使用的变量:list,str,tuple,dict,bool,int
6. 条件语句
if xxx:
pass
elif xxxx:
pass
else:
pass
7. 长度
len(xxxx)
8. 索引操作:
str:
v = "xxxx"
v[0]
tuple:
v = (1,2,3)
v[0]
list:
v = [1,2,3]
v[0]
dict:
v = {
‘k1‘:‘val1‘,
‘k2‘:‘val2‘,
‘k3‘:‘val3‘
}
v[‘k1‘]
9. 运算符
- 除法
- 字符串相乘
- += -+
- or and 顺序
- in
dic = {
‘k1‘: ‘v1‘,
‘k2‘: ‘v2‘
}
li = [11,22,33,44]
# 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‘} 的值中。
1 val = "v1" 2 for val in dic: 3 for val in dic.values(): 4 for v in dic.values(): 5 if val in v: 6 print(True) 7 #龙哥,老狗,成绩,海角 8 9 result = False 10 for item in dic.values(): 11 if item == val: 12 result = True 13 break 14 15 print(result)
10. py2,py3
-
raw_input()
input()
-
range()
xrange()
range()
-
ascii(#-*-encoding:utf-8 -*-)
utf-8
-
print "alex"
print(‘alex‘)
11. 字符串
- 大小写
- 首字母大写
- 分割
a,b = split(‘*‘,1)
- 去除空白
- 替换
- "xx".join(["11","22",])
- 是否是十进制数
12. 列表
- 追加
- 插入
- 反转
长度
for循环
del li[0]
li[0:3:1]
嵌套:
[
1,
[33,44],
"xxx"
]
补充:
1. 索引为奇数值,删除
# 删除列表元素时,会影响列表长度,从而使得索引取值时,容易出现错误。
li = [11,22,33,44,66]
1 li = [11, 22, 33, 44, 66] # 0 2 # 索引为奇数值,删除 3 for i in range(0,len(li)): # 4,0 4 del li[i] 5 print(li)
1 # 解决方案一: 2 li = [11, 22, 33, 44, 66] # 0 3 new_list = [] 4 # 索引为奇数值,删除 5 for i in range(0,len(li)): 6 if i%2 == 0: 7 new_list.append(li[i]) 8 li = new_list 9 print(li) 10 11 # 解决方案二: 12 li = [11, 22, 33, 44, 66] # 0 13 #索引为奇数值,删除 14 for i in range(len(li)-1,-1,-1): # 4,0 15 if i%2 == 1: 16 del li[i] 17 print(li) 18 19 #切片+步长 20 li = [11, 22, 33, 44, 66] 21 del li[0:4] 22 del li[0:4:2] 23 print(li)
12. tuple
- 元素,不可变
- 只有一个元素时,一定加逗号
# val = (1,2,3,4,555,6)
# print(val,type(val))
# val = (1)
# print(val,type(val))
# val = (1,)
# print(val,type(val))
13. 字典
dic = {‘k1‘:"v1",‘k2‘:‘v2‘}
#方法
data = dic.get(‘kk1‘) # None
len
dict[‘k1‘] = "vv1"
for
val = "v1" in dic # False
# 出错点
# 题:dic = {‘k1‘:"v1",‘k2‘:‘v2‘}把key="k1",键值对删除
# del dic[‘k1‘]
# 题:dic = {‘u1‘:"v1",‘k2‘:‘v2‘,‘k3‘:‘v3‘}把key中存在k,键值对删除
1 dic = {‘u1‘:"v1",‘k2‘:‘v2‘,‘k3‘:‘v3‘} 2 # 不要在循环当前字典的过程中,修改当前字典大小 3 4 # 错误 5 for key in dic: 6 if ‘k‘ in key: 7 del dic[key] 8 9 # 正确方式 10 dic_key_list = [] 11 for key in dic: 12 dic_key_list.append(key) 13 14 for row in dic_key_list: 15 if ‘k‘ in row: 16 del dic[row] 17 print(dic)
14. int
- 最少用多少二进制位表示
15. 字符串和布尔值相互转换
v = "123"
val = int(v)
v = 123
val = str(v)
list
v = "礼券"
val = ["礼","券"]
v = (1,2,3,)
val = list(v)
此功能相当于:
# val = []
# for item in v:
# val.append(item)
tuple
v = [11,22,33,44]
val = tuple(v)
16. 布尔值
- True,False
- False: 0,None,"",{},[],()
17. 字符串功能
for循环
while循环
if else
字典
标签:xrange app 存在 逗号 name 相互 logs key pytho
原文地址:http://www.cnblogs.com/wangqingshuai/p/7412679.html