Day02
自学笔记
1. 对于Python,一切事物都是对象,对象基于类创建,对象具有的功能去类里找
name = ‘Young’ - 对象
Li1 = [11,22,33] - 对象
列表创建:
Li = [11,22,33]
也可以这样创建:
Li = list(11,22,3)
字符串:
- S = “fha”
- S = str(‘dd’)
以此类推......
2. int 内部功能介绍
__init__ () 构造方法
比如:
Age = int(19) #执行__init__()方法
Age = 18
Age.__add__(7)
18+7
Age.__divmod__(7)
18/7 = (商,余数)
Age.__rdivmod__(7)
7/18 = (商,余数)
3. str内部功能介绍:
__contains__() -- 1
1 my_string = 'young' 2 result = my_string.__contains__('you') 3 #result = 'you' in my_string 4 print(result)
结果:
True
capitalize -- 2
my_string = 'young' result = my_string.capitalize() print(result)
结果:
Young
center -- 3
1 name = 'Young' 2 result = name.center(40,'*') 3 print(result)
结果:
*****************Young******************
count -- 4
1 name = 'Youngyounggdgfak' 2 #result = name.count('ou') 3 result = name.count('ou',0,5) 4 print(result)
结果:
1
encode -- 5
1 name = '雨纷纷' 2 result = name.encode('gbk') 3 print(result)
结果:
b'xd3xeaxb7xd7xb7xd7'
endswith -- 6
name = 'Young' #result = name.endswith('g') result = name.endswith('un',0,4) # [0,4) print(result)
结果:
True
expandtabs -- 7
name = 'You ng' result = name.expandtabs() print(result)
结果:
You ng
find -- 8
name = 'Young' #result = name.find('g')#返回所在索引的位置,没有找到返回-1 result = name.find('un',0,4) # [0,4) print(result)
结果:
2
index -- 9
1 name = 'Young' 2 #result = name.index('g')#返回所在索引的位置,没有找到报错 3 result = name.index('unc',0,4) # [0,4) 4 print(result)
结果:
Traceback (most recent call last):
File "E:/Pycharm/01_projects/day02/01_examples.py", line 52, in <module>
result = name.index('unc',0,4) # [0,4)
ValueError: substring not found
format -- 10
1 name ="Young {0}" 2 result = name.format("good") 3 print(result)
结果:
Young good
1 name ="Young {0} as {1}" 2 result = name.format("good",'hello') 3 print(result)
结果:
Young good as hello
1 name ="Young {name} as {id}" 2 result = name.format(name="good",id='hello') 3 print(result)
结果:
Young good as hello
类似与 %s ,强过‘+’
join -- 11
1 li = ['g','o','o','d'] 2 #result = "".join(li) 3 result = "_".join(li) 4 print(result)
结果:
#good
g_o_o_d
partition -- 12
1 name ="Youngisgood" 2 result = name.partition('is')#以is将name分割 3 print(result)
结果:
('Young', 'is', 'good')
replace -- 13
1 name ="Youngisgood" 2 #result = name.replace('o','m')#用 ‘m’ 替换 'o' 3 result = name.replace('o','m',2)#用 ‘m’ 替换 'o',转换前两个 4 print(result)
结果:
Ymungisgmod
splitlines -- 14
1 name =''' 2 hello 3 kitty 4 good 5 man 6 ''' 7 #result = name.split(' ')#指定字符分割 8 result = name.splitlines()#按行切割,根据‘ ’ 9 print(result)
结果:
['', 'hello', 'kitty', 'good', 'man']
4. 列表
列表的元素可以是列表
字典的元素也可以是字典
extend
1 li = [1,2,3] 2 print(li) 3 #li.extend([4,5]) 4 li.extend((4,5,))# 5后加上个逗号,大家默认的 5 print(li)
结果:
[1, 2, 3]
[1, 2, 3, 4, 5]
pop
1 li = [1,2,3] 2 print(li) 3 ret = li.pop() # ret 等于被删除的那个数 4 print(li) 5 print(ret)
结果:
[1, 2, 3]
[1, 2]
3
1 li = [1,2,3] 2 print(li) 3 ret = li.pop(1) #pop()里是下标 4 print(li) 5 print(ret)
结果:
[1, 2, 3]
[1, 3]
2
remove 和 reverse
1 li = [11,11,2,3,99] 2 print(li) 3 li.remove(11)# 第一个11 被拿走 4 print(li) 5 li.reverse() 6 print(li)
5. 元组
T1= (1,2,{‘k1’:’v1’})
元组的元素不可以变,但元组的元素的元素可修改
1 t1 = (1,2,{'k1':'v1'}) 2 #del t1[0] 3 #t1[2] = 123 #t1[2] 字典元素 4 t1[2]['k1'] = 2 5 print(t1)
6. 文件操作
读写方式 1
r+ 读写
w+ 写读
read() 2
(注:read是按字符来执行)
1 #coding=utf-8 2 __author__ = 'Young' 3 f =open('test.log','r') 4 #f.write('无hadksfh') # 先写,后读 5 ret = f.read(2) 6 f.close() 7 print(ret)
结果:
无华
tell() 3
(注:tell按照字节来执行的,read是按字符来执行)
1 f =open('test.log','r') 2 print(f.tell()) 3 ret = f.read(2) #加参数指定读取字符 4 print(f.tell()) 5 f.close()
结果:
0
4
seek 和 truncate 4
(注:tell用来查看当前指针位置 seek用来指定当前指针位置 ,truncate截取数据,只保留指针之前的数据)
1 f =open('test.log','r+') 2 f.seek(4) 3 print(f.tell()) 4 #print(f.read()) 5 f.truncate() #截取数据,只保留指针之前的数据 6 print(f.tell()) 7 f.close()
结果:
4
4