python的内存回收机制_关于python的变量使用回收机制
a=3
print type(a) #a為整型
a=3L
print type(a) #a為長整型
a=2.3
print type(a) #float
a=2.3e10
print type(a) #float
a="2.3e10"
print type(a) #string
a="3.12e-1"
print a,type(a) # "3.12e-1
a=float(a)
print a,type(a) #0.312
a=3.12e10
print type(a) #
a=4+3j
print a,type(a)
(4+3j)
產(chǎn)生隨機數(shù)的方法:
import random
ra=random.randint(1,10)
print ra
使用PyQt4.QtCore 包含對象和函數(shù)的 方法
from PyQt4.QtCore import *
x = QString()
y = QDate()
請和上面的使用方式進行對比
import PyQt4
x = PyQt4.QtCore.QString()
y = PyQt4.QtCore.QDate()
chr(int)
print chr(97) #返回 字符'a',數(shù)據(jù)類型為str
unichr(int)
print unichr(8364) #返回unicode字符,數(shù)據(jù)類型為str
ord(char)
print ord('a') #打印出 97,打印出字符對應(yīng)的 數(shù)字編碼值
關(guān)于子字符串
a="hello world"
a[:3] #前3個字符
a[-3:] #最后3個字符
a[5:7] #5,6
字符串或字符的查找,找不到匹配字符時,返回 -1 (也可使用a.index("is"))
a="hello today is a good day"
print a.find("is")
print a[a.find("is")]
12
i
英文句子首字母大字,title()函數(shù)
a.title()
字符串的格式化輸出
stra="hello %s,%d,%i,%f" %("baby",33,44,44.4)
print stra #hello baby,44.400000
子字符串a(chǎn),是否在字符串s中存在
if a in s:
if a not in s:
字符串的拼接
a+s
字符重的多次重復(fù)
a="234"
a*3
len(s)
s.count("ab")
s="hello ll ll ab ll"
print s.count('ll') #打印 4
s.endswith(x)
s.startxwith(x)
s.find(x)
s.rfind(x) #從右邊開始找
s.isdigit() #是否 全為數(shù)字
s.isalpha() #是否 全為字母
s.title()
s.lower()
s.upper()
s="who is on duty today"
print s,s.replace("who is","I am") #who is on duty today I am on duty today #注意 ,不會改變原來的s字符串
s.strip() #去掉首尾的whitespace
In [14]: a=("helo") #還是str類型
In [15]: type(a)
Out[15]: str
In [16]: a=("helo",) #tuple類型
In [17]: type(a)
Out[17]: tuple
In [18]: a="ell","dsf",4,4 #這種也是tuple類型
In [19]: type(a)
Out[19]: tuple
tuple,list,dict這三類容器都是可以嵌套的
x in List
x not in list
L +m
下面是list擴展的方法
In [20]: a=[1,2,3]
In [21]: b=[4,5,6]
In [22]: a+b
Out[22]: [1,3,6]
In [23]: a.extend(b)
In [24]: a
Out[24]: [1,6] #可以看到a發(fā)生了改變
In [28]: a=[1,1,5]
In [29]: a.count(1) #統(tǒng)計a中 元素 1的個數(shù)
Out[29]: 4
list.index(x)
list.append(x)
list.extend(m)
list.insert(i,x) #position i
l.remove (x) #最左邊遇到的x
list.pop() #將list最右邊的元素作為返回值彈出 Returns and removes the rightmost item of list L
L.pop(i) #Returns and removes the item at index position int i in L
In [31]: a
Out[31]: [1,4]
In [32]: a.reverse()
In [33]: a
Out[33]: [4,1]
list.sort()
關(guān)于list的 shadow copy
seaweed = ["Aonori","Carola","Dulse"]
macroalgae = seaweed
print seaweed,macroalgae
macroalgae[2] = "Hijiki"
print seaweed,macroalgae
下面是輸出結(jié)果,可以看到改變macroalgae時,seaweed也發(fā)生了改變,這是由于python默認使用的shadow copy,可以認為對于list,macroalgae 為seaweed的別名
['Aonori','Carola','Dulse'] ['Aonori','Dulse']
['Aonori','Hijiki'] ['Aonori','Hijiki']
如果重新復(fù)制一個完全一新的list出來,如下操作
b=[1,2334,35534]
a=b[:]
如上,這樣改變a中的元素時,也不影響b,此時a對象使用的內(nèi)存空間完全不同于b
創(chuàng)建dict變量更直觀的方法
d=dict(a=3,b=4,c=5,e="hello")
print d
{'a': 3,'c': 5,'b': 4,'e': 'hello'}
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的python的内存回收机制_关于python的变量使用回收机制的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java高并发抢单编程模型_Java高并
- 下一篇: 爬虫python软件准备_工具准备的差不