标签:partition extend date() ... form rate move 用户输入 each
一、运算符
(一)算术运算:
+ 相加:9+2=11
- 相减:9-2=7
* 相乘:9*2=18
/ 相除:9/2=4.5
% 求余:9%2=1
** 乘方:9**2=81
// 求商的整数部分:9//2=4
(二)比较运算:
== 等于:比较两数是否相等
!= 不等于:比较两数是否不相等
> 大于
< 小于
>= 大于等于
<= 小于等于
(三)赋值运算:
= c=a+b,将a+b的结果赋值给c
+= c+=a,等价于c=c+a
-= c-=a,等价于c=c-a
*= c*=a,等价于c=c*a
/= c/=a,等价于c=c/a
%= c%=a,等价于c=c%a
**= c**=a,等价于c=c**a
//= c//=a,等价于c=c//a
(四)逻辑运算:
and 布尔“与”:(a and b)返回True
or 布尔“或”:(a or b)返回True
not 布尔“非”:a为真,则not a为假;a为假,not a为真
(五)成员运算:
in 如果在指定序列中找到值则返回True,否则返回False
not in 如果在指定序列中没有找到值则返回True,否则返回False
二、基本数据类型
1、数字:int(整型)
如:1、2、134、5643.........
功能:
(1)bit_length():输出表示数字二进制的最少位数
1 i = 4 2 print(i.bit_length()) 3 #结果:3
2、布尔值:bool
3、字符串:str “hello world”
(1)capitalize():首字母变大写
1 a1 = "alex" 2 ret = a1.capitalize() 3 print(ret) 4 #结果:Alex
(2)center():内容居中,空白处填充
a1 = "alex" ret = a1.center(10,‘*‘) print(ret) #结果:***alex***
(3)count():统计子序列在字符串中出现的次数
a1 = "alex is a teacher" ret = a1.count("al") ret = a1.count("a",0,10) #a在0到10个字符之间出现了几次 print(ret) #结果:1 2
(4)endswith():判断一个字符串是否以某个字母结尾
temp = "hello" print(temp.endswith("o")) print(temp.endswith("e")) #获取字符串里大于等于0,小于2的位置中是否以e结尾 print(temp.endswith("e",0,2)) #结果:True False True
(5)expandtabs():将tab转换为空格,默认一个tab为8个空格
temp = "hello\t111" #\t表示tab键 print(temp) print(temp.expandtabs()) print(temp.expandtabs(20)) #结果: hello 111 hello 111 hello 111
(6)find():寻找子序列位置,如果找到,则返回子序列位置,否则,返回-1
s = "hello alex" print(s.find("l")) print(s.find("ley")) #结果: 7 -1
(7)format():字符串格式化,可以动态传参数
s = "hello {0}, age {1}" #{0}、{1}是占位符,可以动态传参数 print(s) print(s.format("alex",19)) #结果: hello {0}, age {1} hello alex, age 19
(8)join():连接多个字符串
li = ["hello","alex"] #列表 li = ("hello","alex") #元祖 s = "_".join(li) s = "**".join(li) print(s) #结果: hello_alex hello**alex
(9)移除空格
s = " alex " #print(s.lstrip()) #移除左空格 #print(s.rstrip()) #移除右空格 print(s.strip()) #移除所有空格 #结果: alex alex alex
(10)partition():分割字符串,以要分割的字符为界限分成3份
s = "alex is a boy" print(s.partition("is")) #结果:(‘alex ‘, ‘is‘, ‘ a boy‘)
(11)replace():替换
s = "alex is a boy" print(s.replace("a","A")) print(s.replace("a","A",1)) #只替换出现的第一个a #结果: Alex is A boy Alex is a boy
(12)split():分割,分割符不再出现在结果中
s = "helloalex" print(s.split("l")) print(s.split("l",1)) #以第一个l为分割线进行分割 #结果: [‘he‘, ‘‘, ‘oa‘, ‘ex‘] [‘he‘, ‘loalex‘]
(13)swapcase():大写变小写,小写变大写
s = "AlEx" print(s.swapcase()) #结果:aLeX
(14)索引
s = "alex" print(s[0]) print(s[1]) print(s[2]) print(s[3]) print(len(s)) #获取字符串的长度 #结果: a l e x 4
(15)切片
s = "alex" print(s[0:2]) #结果:al
(16)for循环
s = "alex" for item in s: print(item) #结果: a l e x
for循环+continue
s = "alex" for item in s: if item == "l": continue #遇到l本次循环结束,不输出l print(item) #结果: a e x
for循环+break
s = "alex" for item in s: if item == "l": break #遇到l循环结束 print(item) #结果:a
4、列表:list
创建列表
1 name_list = ["alex","book","school"] 2 或 3 name_list = list["alex","book","school"]
(1)索引
name_list = ["alex","time","school"] print(name_list[0]) #结果:alex
(2)切片
name_list = ["alex","time","school"] print(name_list[0:2]) #结果:[‘alex‘, ‘time‘]
(3)len:长度
name_list = ["alex","time","school"] print(name_list[2:len(name_list)]) #结果:[‘school‘]
(4)for循环
name_list = ["alex","time","school"] for item in name_list: print(item) #结果: alex time school
(5)append:向后追加,添加元素
name_list = ["alex","time","school"] name_list.append("book") print(name_list) #结果:[‘alex‘, ‘time‘, ‘school‘, ‘book‘]
(6)count:统计元素出现的次数
name_list = ["alex","time","school"] print(name_list.count("time")) #结果:1
(7)extend():批量的往列表中添加内容
name_list = ["alex","time","school"] temp = [11,22,33,44] name_list.extend(temp) print(name) #结果:[‘alex‘, ‘time‘, ‘school‘, ‘book‘, 11, 22, 33, 44]
(8)index():获取指定元素的索引位置
name_list = ["alex","time","school"] print(name_list.index("school")) #结果:2
(9)insert():向指定索引位置插入元素
name_list = ["alex","time","school"] name_list.insert(1,"SB") print(name_list) #结果:[‘alex‘, ‘SB‘, ‘time‘, ‘school‘]
(10)pop():去掉原列表中的最后一个元素,并将其赋值给a1
name_list = ["alex","time","school"] a1 = name_list.pop() print(name_list) print(a1) #结果: [‘alex‘, ‘time‘] school
(11)remove():移除原列表中从左往右的第一个元素
name_list = ["alex","time","school"] name_list.remove(“time”) print(name_list) #结果:[‘alex‘, ‘school‘]
(12)reverse():将列表中的元素进行反转
name_list = ["alex","time","school"] name_list.reverse() print(name_list) #结果:["school","time","alex"]
(13)sort():排序
name_list = ["alex","time","school"] name_list.sort() print(name_list) #结果:[‘alex‘, ‘school‘, ‘time‘]
(14)del:删除指定索引位置的元素
name_list = ["alex","time","school"] del name_list[1] del name_list[1:3] print(name_list) #结果: [‘alex‘, ‘school‘] [‘alex‘]
5、元祖:几乎和列表一样,但列表可以修改,元祖不能修改
创建元祖
name_tuple = ("alex","book")
(1)索引
name_tuple = ("alex","book") print(name_tuple[0]) #结果:alex
(2)len
name_tuple = ("alex","book") print(name_tuple[len(name_tuple)-1]) #结果:book
(3)切片
name_tuple = ("alex","book") print(name_tuple[0:1]) #结果:(‘alex‘,)
(4)for循环
name_tuple = ("alex","book") for i in name_tuple: print(i) #结果: alex book
(5)del:删除:不支持
(6)count():计算元素出现的次数
name_tuple = ("alex","book") print(name_tuple.count("book")) #结果:1
(7)index():获取指定元素的索引位置
name_tuple = ("alex","book") print(name_tuple.index("book")) #结果:1
6、字典:无序的,每一个元素都是一个键值对
创建字典:
user_info = { "name" : "alex", "age" : 27, "gender" : "M" }
(1)索引:必须要索引字典中的key,如果key不存在,则报错
print(user_info["name"]) #结果:alex
(2)切片:没有
(3)for循环:默认输出所有的key
for i in user_info: print(i) #结果: name age gender
(4)keys():获取字典中所有的key,并输出在一个列表中
print(user_info.keys()) #结果:dict_keys([‘name‘, ‘age‘, ‘gender‘])
(5)values():获取字典中所有的值,并输出在一个列表中
print(user_info.values()) #结果:dict_values([‘alex‘, 27, ‘M‘])
(6)items():获取字典中所有的键值对,并输出在一个列表中
print(user_info.items()) for k, v in user_info.items(): print(k,v) #结果: dict_items([(‘name‘, ‘alex‘), (‘age‘, 27), (‘gender‘, ‘M‘)]) name alex age 27 gender M
(7)clear():清除所有内容
user_info.clear()
print(user_info)
(8)get():根据key获取值,如果key不存在,可以指定一个默认值
val = user_info.get("age") print(val) val = user_info.get("age111") print(val) #结果: 27 None
(9)update():更新
test = { "a1" : 123, "a2" : 345, } user_info.update(test) print(user_info) #结果:{‘name‘: ‘alex‘, ‘age‘: 27, ‘gender‘: ‘M‘, ‘a1‘: 123, ‘a2‘: 345}
(10)del:删除
test = { "a1" : 123, "a2" : 345, } del test["a1"] print(test) #结果:{‘a2‘: 345}
三、其他
1、for循环:用户按照顺序循环可迭代对象中的内容。
item = ["11","alex","123"] for i in item: print i
#结果:11
alex
123
2、enumrate:为可迭代的对象添加序号。
item = ["11","alex","123"] for i, j in enumrate(item): print(i,j)
#结果:0 11
1 alex
2 123
3、range和xrange:指定范围,生成指定的数字。
python2.7:range:获取指定范围内的数
xrange:不具体输出范围内的数,只能用for循环才能输出数值
python3.6:range等同于python2.7中的xrange
print(range(1,10)) for i in range(1,10): print i
#结果:1,2,3,4,5,6,7,8,9
四、作业
1、有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。即: {‘k1‘: 大于66的所有值, ‘k2‘: 小于66的所有值}
li = [11,22,33,44,55,66,77,88,99,90] dic= { "k1" : [], "k2" : [], } for i in li: if i <= 66: dic[‘k1‘].append(i) else: dic[‘k2‘].append(i) print(dic)
#结果:{‘k1‘: [11, 22, 33, 44, 55, 66], ‘k2‘: [77, 88, 99, 90]}
2、查找列表中元素,移除每个元素的空格,并查找以 a或A开头 并且以 c 结尾的所有元素。
li = ["alec", " aric", "Alex", "Tony", "rain"]
tu = ("alec", " aric", "Alex", "Tony", "rain")
dic = {‘k1‘: "alex", ‘k2‘: ‘ aric‘, "k3": "Alex", "k4": "Tony"}
3、输出商品列表,用户输入序号,显示用户选中的商品
商品 li = ["手机", "电脑", ‘鼠标垫‘, ‘游艇‘]
4、购物车
功能要求:
标签:partition extend date() ... form rate move 用户输入 each
原文地址:https://www.cnblogs.com/tongjingwen-start/p/9140716.html