本章内容
1.小知识点
2.字符串string
3.字符串的深浅copy
4.列表list
5.元组tuple
6.列表生成式
7.字典dict
8.集合set
9.文件操作
10.字符编码与转码
小知识点
1、raw_input() python2中可用
input() python2/3可用
首先说下,python2中的raw_input(), 用来获取用户的输入,并把所有的输入转换成字符串的形式
python2中的input(),不好用,也建议不要用它, 因为 它这里必须跟可识别的表达式,如果是string,需要加引号,
float、int可用直接输入,如果输入的字符串恰好是个变量,则会输出这个变量。
python 3 中input 取代了raw_input
2. 小练习题, 10000存入银行,利润是0.35%,多少年才可以翻倍?
money = 10000
year = 1
while money <= 20000:
money *=1.035
year +=1
print year
3.如何让多个字符串在同一个同一行中输出?
方法一:
string = ""
string += 'test'
for i in [1,2,3]:
string += str(i)
print (string)
方法二:
import sys
sys.stdout.softspace=0 #这行需要在每个print下面添加。 貌似python3中不能用
4、type(......) #查看 ........变量的数据类型。
5、string是可以遍历的
a = 'hello world'
for i in a:
print i
6、判断变量名字的有效性:
import string
string.ascii_letters #大小写字母
string.digits #0-9
条件:
1、不能以数字开头
2、必须所有的字符都在 a-z,0-9
if I not in string.digits + string.ascii_letters + '_'
8、 len() #查看字符串的长度
max()/min() #查找最大、最小值
max(string.ascii_letters) #比较字母
max(string.digits) #比较数字
max([1,3,6,2,3,66]) #比较列表元素
range(开始:结束:分段)
sum() #计算总和
reversed() -> string[0:0:-1] #区别是前者没有返回值,直接对数据做了修改,后者有返回值,数据没动
sorted() #对列表进行排序,排序后变量并不会改变 如果是 变量.sort() 原数据则会被排序
enumerate #序列化操作
运行结果:
zip:
运行结果:
11. sys 模块是python解释器自带的。使用c语言写的,在环境路径下是找不到这个.py文件的,
字符串string
序列的公共特性:
- 可遍历
- 可切片
- 有索引
- 可连接 + 相加
- 重复
- 成员 in/not in
- 不可修改
string += '1' ,这其实是给string另外开辟了一个新的内存,id(string)可以看到其实变化的。
string的内建函数
a = 'hello world!!'
a.capitalize() #首字母大写
a.title() #每个单词的首字母大写
a.upper() #所有字母都大写
a.lower() #所有字母都小写
a.swapcase() #大小写互换
a.count('c') #统计字母c 出现的次数
a.startwith('h') #判断字母是否已 h 开头 返回True 或者Flase
a.endswith('c') #判断是否以 c 结尾 ,返回同上
a.isalnum() #判断字符串是否全为字母或者数字,返回同上
a.isalpha() #判断是否全为字母
a.isdigit() #判断是否全为数字
a.islower() #判断是否全为小写
a.isupper() #判断是否全为大写
a.replace('a','A') #将 a 替换为 A , 返回替换后的数据,元数据不受影响
a.replace('a'.'A',3) #指定只替换前多少个
a.split(':',max) #已什么为分割位,可以最大输出多少个,返回数组
a.index('f') #查找f的下标是多少,如果没有则返回 substring not found
a.find('f') #查找f并返回下标,如果没有返回 -1
index 和find,查找的时候从左到右去匹配,如果有一个匹配,后面的则不再查找。
a.rfind('f') #从右开始朝赵
a.format() #格式化输出
a.join('') #
a.strip() #去除两边的空格
a.rstrip() #去除右面的空格
a.lstrip() #去掉左面的空格
a.strip('he') #去掉左面的he
a.ljust(width,'xx') #占位width,左对齐,不够xxx来填丛
a.rjust()
a.center(width) #中间对齐,两边不够空格对齐
a.zfill(20) #右面对齐,左面不够 0 补齐。
字符串的深浅copy
a = ['hello','nihao','xiaoming']
b = a
a.append('datou')
print(id(a))
print(id(b))
print(a)
print(b)
4302256648
4302256648
['hello', 'nihao', 'xiaoming', 'datou']
['hello', 'nihao', 'xiaoming', 'datou']
a的值付给b,相当于b的内存地址指向了列表的内存地址,a如果修改数据的话相当于修改了内存地址中的数据,a,b指向相同的内存地址,所以b也是会随之变化的。
浅copy
a = ['hello','nihao','xiaoming',['a','b']]
b = a.copy()
a[2] = 'datou'
a[3][0] = 'c'
print(id(a))
print(id(b))
print(a)
print([id(ele) for ele in a])
print(b)
print([id(ele) for ele in b])
结果:
4301252680
4301253192
['hello', 'nihao', 'datou', ['c', 'b']]
[4300593392, 4300593560, 4301184056, 4301208072]
['hello', 'nihao', 'xiaoming', ['c', 'b']]
[4300593392, 4300593560, 4300640432, 4301208072]
浅copy就是之copy内存的第一层指针,然后让在新的内存地址中,如果a修改第一层的东西,当然影响不到b了,但是a修改第二层的东西时,b就会随之变化,因为b把第二层的指针copy过来的,指针是没有变化的。
实现浅copy的三种方式:
person = [........]
p1 = copy.copy(person)
p2 = person[:]
p3 = list(person)
深copy
import copy
a = ['hello','nihao','xiaoming',['a','b']]
b = copy.deepcopy(a)
a[2] = 'datou'
a[3][0] = 'c'
print(id(a))
print(id(b))
print(a)
print([id(ele) for ele in a])
print(b)
print([id(ele) for ele in b])
print('----------')
结果:
4330176648
4330177096
['hello', 'nihao', 'datou', ['c', 'b']]
[4301641968, 4301642136, 4302231680, 4329896392]
['hello', 'nihao', 'xiaoming', ['a', 'b']]
[4301641968, 4301642136, 4301689008, 4330130696]
相当于如论内存中多少层,都copy一份,你改你的,我不受你的影响。
如同linux中的硬链接和软连接:
Linux 系统中有软链接和硬链接两种特殊的“文件”。
软链接可以看作是Windows中的快捷方式,可以让你快速链接到目标档案或目录。
硬链接则透过文件系统的inode来产生新档名,而不是产生新档案。
小知识点
input 输入一个数字, 可以用isdigit() 来判断是不是数字,这个数字可能是str 型的哦,需要int下
另外在while死循环中,如果想要退出程序,可以用exit()
购物车小练习
1 product_list = [ 2 ('Iphone',5800), 3 ('Mac Pro',9800), 4 ('Bike',800), 5 ('Watch',10600), 6 ('Coffee',31), 7 ('Alex Python',120), 8 ] 9 shopping_list = [] 10 salary = input("Input your salary:") 11 if salary.isdigit(): 12 salary = int(salary) 13 while True: 14 for index,item in enumerate(product_list): 15 #print(product_list.index(item),item) 16 print(index,item) 17 user_choice = input("选择要买嘛?>>>:") 18 if user_choice.isdigit(): 19 user_choice = int(user_choice) 20 if user_choice < len(product_list) and user_choice >=0: 21 p_item = product_list[user_choice] 22 if p_item[1] <= salary: #买的起 23 shopping_list.append(p_item) 24 salary -= p_item[1] 25 print("Added %s into shopping cart,your current balance is 33[31;1m%s 33[0m" %(p_item,salary) ) 26 else: 27 print("