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)
产生随机数的方法:
import random
ra=random.randint(1,10)
print ra
使用PyQt4.QtCore 包含对象和函数的 方法
from PyQt4.QtCore import *
x = QString()
y = QDate()
请和上面的使用方式进行对比
import PyQt4
x = PyQt4.QtCore.QString()
y = PyQt4.QtCore.QDate()
chr(int)
print chr(97) #返回 字符'a',数据类型为str
unichr(int)
print unichr(8364) #返回unicode字符,数据类型为str
ord(char)
print ord('a') #打印出 97,打印出字符对应的 数字编码值
关于子字符串
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()函数
a.title()
字符串的格式化输出
stra="hello %s,%d,%i,%f" %("baby",33,44,44.4)
print stra #hello baby,44.400000
子字符串a,是否在字符串s中存在
if a in s:
if a not in s:
字符串的拼接
a+s
字符重的多次重复
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() #是否 全为数字
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发生了改变
In [28]: a=[1,1,5]
In [29]: a.count(1) #统计a中 元素 1的个数
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()
关于list的 shadow copy
seaweed = ["Aonori","Carola","Dulse"]
macroalgae = seaweed
print seaweed,macroalgae
macroalgae[2] = "Hijiki"
print seaweed,macroalgae
下面是输出结果,可以看到改变macroalgae时,seaweed也发生了改变,这是由于python默认使用的shadow copy,可以认为对于list,macroalgae 为seaweed的别名
['Aonori','Carola','Dulse'] ['Aonori','Dulse']
['Aonori','Hijiki'] ['Aonori','Hijiki']
如果重新复制一个完全一新的list出来,如下操作
b=[1,2334,35534]
a=b[:]
如上,这样改变a中的元素时,也不影响b,此时a对象使用的内存空间完全不同于b
创建dict变量更直观的方法
d=dict(a=3,b=4,c=5,e="hello")
print d
{'a': 3,'c': 5,'b': 4,'e': 'hello'}