关于内存分配问题
新定义字符串变量默认新开辟一块新的内存空间
其它类似有索引如列表,元组或是字典赋值时其实只是把变量名指向同一地址空间而已,如下所示
1 ##字符串新定义则开辟新的一块内存空间 2 >>> str1 = 'hoho' 3 >>> str2 = str1 4 >>> id(str1),id(str2) #查看内存对象地址,观察内存地址,即str2新开辟了内存空间 5 (140297199501752, 140297199501752) #这里看到是一样的是由于python的一个内部机制导致的,如果字符串足够大的话就会是不一样的,不用纠结 6 >>> str2 = 'heihei' 7 >>> str1 8 'hoho' 9 >>> str210 'heihei'11 >>> id(str1),id(str2) #看,内存地址是不是变了12 (140297199501752, 140297214622552) 13 14 ##非字符器,如列表,元组,字典赋值定义后其实只是把新的变量名(可以理解为标签)指向同一内存地址,以字典为例,如下所示15 >>> dic1 = {'name':'hoho'}16 >>> dic2 = dic117 >>> id(dic1),id(dic2)18 (140297199190088, 140297199190088)19 >>> dic1 = {'name':'hoho'}20 >>> dic2 = dic121 >>> id(dic1),id(dic2) #查看内存对象地址,发现是一样的,故修改dic2事实上dic1也跟着修改了22 (140297199191752, 140297199191752)23 >>> dic2['name'] = 'heihei'24 >>> dic225 {'name': 'heihei'}26 >>> dic127 {'name': 'heihei'}
列表,元组及字典的复制问题(浅复制与深复制 copy模块的使用)
1、列表及元组可使用切片实现浅复制,也可使用 copy模块使用浅复制(包括字典)
2、使用copy.deepcopy() 实例深复制
1 >>> import copy 2 >>> list1 = [1,2] 3 >>> list2 = list1 4 >>> list2[0] = 2 #list2改了,list1跟着变了 5 >>> list1 6 [2, 2] 7 >>> list3 = list1[:] #浅复杂,利用数组切片做浅复制 8 >>> list3 = copy.copy(list1) 9 >>> id(list1),id(list2),id(list3) #这里就可看到地址空间是不一样的10 (140297199250696, 140297199250696, 140297199247560)11 >>>
12 >>> list4 = [1,[2]] ##复杂结构必须用深复制13 >>> list5 = list4[:]14 >>> list515 [1, [2]]16 >>> list5[1][0] = 617 >>> list418 [1, [6]] #从这里可以看到内层的列表事实是没复制的,list4也跟着变了19 >>> list6 = copy.deepcopy(list4) #这里使用深复制20 >>> list6[1][0] = 821 >>> list622 [1, [8]]23 >>> list424 [1, [6]] #这里就可以看出已经复制的了
常用内置函数
python内置函数是非常多的,记住常用的就行了,但会知道怎么查看有哪些内置函数,函数的帮助
正常情况下分3步走
type(变量) ---> 得到变量的所属类
dir(类名) --->查看类下都有哪些方法,其中类似 __abs__ 以双下划线开头的一般都有替代的方法 如: __abs__ <=> abs()
help(类名或函数名) --->查看类下函数用法或是直接查看函数用法
整形
1 >>> s,y = pmod(7,3) ## pmod 返回数据,值为(商,余数),可用于分页2 >>> s,y3 (2, 1)4 >>> a = -25 >>> abs(-2) #abs取绝对值6 27 >>> len(str(-2)) #取速度长度8 2
View Code
浮点
1 >>> a = 7.02 >>> pmod(a,3)3 (2.0, 1.0)4 >>> a = 7.2355 >>> a.__round__(2) #四舍五入6 7.247 >>> a.__trunc__() #取整8 7
View Code
字符串
1 >>> str1 = &#39;this is a string&#39; 2 >>> &#39;is&#39; in str1 #成员判断 3 True 4 >>> str1[1:3] # 切片操作与索引 5 &#39;hi&#39; 6 >>> len(str1) #长度 7 16 8 >>> str1.find(&#39;is&#39;) #查找字符串,返回索引值 9 210 >>> str1.find(&#39;is&#39;,3,9)11 512 >>> str1.find(&#39;iss&#39;) #没有找到返回-1 ,如是index则会报错13 -114 >>> str1.index(&#39;is&#39;,3)15 516 >>> str1.index(&#39;iss&#39;)17 Traceback (most recent call last):18 File "", line 1, in 19 ValueError: substring not found20 >>> str1 = &#39; aaa&#39;21 >>> str1.strip() 去空白,换行,回车22 &#39;aaa&#39;23 >>> str1.lstrip()24 &#39;aaa&#39;25 >>> str1.rstrip()26 &#39; aaa&#39;27 >>> str1 = &#39;duiqi&#39; #对齐操作28 >>> str1.ljust(20)29 &#39;duiqi &#39;30 >>> str1.ljust(20,&#39;*&#39;)31 &#39;duiqi***************&#39;32 >>> str1.rjust(20,&#39;*&#39;)33 &#39;***************duiqi&#39;34 >>> str1.center(20,&#39;*&#39;)35 &#39;*******duiqi********&#39;36 >>> str1 = &#39;this is a string&#39;37 >>> str1.split() ##分割操作38 [&#39;this&#39;, &#39;is&#39;, &#39;a&#39;, &#39;string&#39;]39 >>> str1.splitlines()40 [&#39;this is a string&#39;]41 >>> list1 = [1,2,3]42 >>> &#39;->&#39;.join([str(i) for i in list1]) #连接操作43 &#39;1->2->3&#39;44 >>> str145 &#39;this is a string&#39;46 >>> str1.count(&#39;is&#39;) #计数47 248 >>> str1.replace(&#39;is&#39;,&#39;os&#39;) #替换49 &#39;thos os a string&#39;50 >>> str1.replace(&#39;is&#39;,&#39;os&#39;,1) #替换,只替换1个51 &#39;thos is a string&#39;52 53 str1.startswith(&#39;sub&#39;) #以什么开头54 str1.endswith(&#39;sub&#39;) #以什么结尾55 str1.lower() #转为小写56 str1.upper() #转为大写
View Code
列表与元组(元组不可修改)
1 >>> lst1 = [&#39;a&#39;] 2 >>> lst1.append(&#39;b&#39;) #新增 3 >>> lst1 4 [&#39;a&#39;, &#39;b&#39;] 5 >>> lst2 = [&#39;c&#39;,&#39;d&#39;] 6 >>> lst1.extend(lst2) #扩展新增 7 >>> lst1 8 [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;] 9 >>> lst1.insert(0,&#39;z&#39;) #插入10 >>> lst111 [&#39;z&#39;, &#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]12 >>> lst1.pop() #去除末尾13 &#39;d&#39;14 >>> lst115 [&#39;z&#39;, &#39;a&#39;, &#39;b&#39;, &#39;c&#39;]16 >>> lst1.remove(&#39;z&#39;) #删除指定元素17 >>> lst118 [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]19 >>> lst1 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]20 >>> lst2 = lst1.copy() # 浅复制 python3才有21 >>> lst2 = lst1.copy()22 >>> lst223 [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]24 >>> lst2.clear() #清空列表25 >>> lst226 []27 >>> del lst2 #删除列表28 >>> lst129 [&#39;d&#39;, &#39;c&#39;, &#39;b&#39;, &#39;a&#39;]30 >>> lst1.sort() #排序31 >>> lst132 [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]33 >>> lst1.append(&#39;a&#39;)34 >>> lst1.count(&#39;a&#39;) #计数35 236 >>> lst137 [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;a&#39;]38 >>> len(lst1) #长度39 540 >>> lst1.index(&#39;a&#39;) #索引41 042 >>> lst1.index(&#39;a&#39;,1) #索引43 4
View Code
字典
1 >>> dic1 = {&#39;key1&#39; : &#39;a&#39;,&#39;key2&#39; : &#39;b&#39;} 2 >>> dic1.get(&#39;key1&#39;) #取字典值,没取到默认返回None,也可指定 3 &#39;a&#39; 4 >>> dic1.get(&#39;key3&#39;) 5 >>> dic1.items() 6 dict_items([(&#39;key2&#39;, &#39;b&#39;), (&#39;key1&#39;, &#39;a&#39;)]) #返回元组列表 7 >>> list(dic1.items()) 8 [(&#39;key2&#39;, &#39;b&#39;), (&#39;key1&#39;, &#39;a&#39;)] 9 >>> dic1.keys() #返回keys列表10 dict_keys([&#39;key2&#39;, &#39;key1&#39;])11 >>> dic1.values() #返回值列表12 dict_values([&#39;b&#39;, &#39;a&#39;])13 >>> dic2 = dic1.copy() #浅复制14 >>> dic215 {&#39;key2&#39;: &#39;b&#39;, &#39;key1&#39;: &#39;a&#39;}16 >>> dic1[&#39;key3&#39;] = &#39;c&#39; #赋值(修改)17 >>> dic118 {&#39;key2&#39;: &#39;b&#39;, &#39;key1&#39;: &#39;a&#39;, &#39;key3&#39;: &#39;c&#39;}19 >>> dic1.pop(&#39;key1&#39;) #删除指定的key20 &#39;a&#39;21 >>> dic122 {&#39;key2&#39;: &#39;b&#39;, &#39;key3&#39;: &#39;c&#39;}23 >>> dic1.get(&#39;key1&#39;,&#39;a&#39;) #取值,没有返回&#39;a&#39;24 &#39;a&#39;25 >>> dic126 {&#39;key2&#39;: &#39;b&#39;, &#39;key3&#39;: &#39;c&#39;}27 >>> dic1.setdefault(&#39;key1&#39;,&#39;a&#39;) #设置默认(貌似没什么用)28 &#39;a&#39;29 >>> dic130 {&#39;key2&#39;: &#39;b&#39;, &#39;key1&#39;: &#39;a&#39;, &#39;key3&#39;: &#39;c&#39;}31 >>> dic3 = {&#39;name&#39;:&#39;update&#39;}32 >>> dic1.update(dic3) #更新33 >>> dic1 34 {&#39;key2&#39;: &#39;b&#39;, &#39;name&#39;: &#39;update&#39;, &#39;key1&#39;: &#39;a&#39;, &#39;key3&#39;: &#39;c&#39;}35 >>> del dic3 #删除36 >>> dic137 {&#39;key2&#39;: &#39;b&#39;, &#39;name&#39;: &#39;update&#39;, &#39;key1&#39;: &#39;a&#39;, &#39;key3&#39;: &#39;c&#39;}38 >>> len(dic1) #长度39 4
View Code
以上就是python基础一的内容,更多相关文章请关注PHP中文网(www.php1.cn)!