Python有五个标准的数据类型:html
不可变的数据类型,这意味着改变一个新分配的对象的数字数据类型的结果值。python
当分配一个值给他们建立的对象。例如:数组
var1 = 1
var2 = 10
也能够使用del语句删去有关一些对象。 del语句的语法是:函数
del var1[,var2[,var3[....,varN]]]]
也能够使用del语句删除单个或多个对象。例如:ui
del var
del var_a, var_b
Python支持四种不一样的数值类型:spa
int | long | float | complex |
---|---|---|---|
10 | 51924361L | 0.0 | 3.14j |
100 | -0x19323L | 15.20 | 45.j |
-786 | 0122L | -21.9 | 9.322e-36j |
080 | 0xDEFABCECBDAECBFBAEl | 32.3+e18 | .876j |
-0490 | 535633629843L | -90. | -.6545+0J |
-0x260 | -052318172735L | -32.54e100 | 3e+26J |
0x69 | -4721885298529L | 70.2-E12 | 4.53e-7j |
#!/usr/bin/python str = 'Hello World!' print str # Prints complete string Hello World! print str[0] # Prints first character of the string H print str[2:5] # Prints characters starting from 3rd to 5th llo print str[2:] # Prints string starting from 3rd character llo World! print str * 2 # Prints string two times Hello World!Hello World! print str + "TEST" # Prints concatenated string Hello World!TEST
Python字符串: .net
#!/usr/bin/python list = [ 'abcd', 786 , 2.23, 'john', 70.2 ] tinylist = [123, 'john'] print list # Prints complete list ['abcd', 786, 2.23, 'john', 70.200000000000003] print list[0] # Prints first element of the list abcd print list[1:3] # Prints elements starting from 2nd till 3rd [786, 2.23] print list[2:] # Prints elements starting from 3rd element [2.23, 'john', 70.200000000000003] print tinylist * 2 # Prints list two times [123, 'john', 123, 'john'] print list + tinylist # Prints concatenated lists ['abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john']
Python元组:code
序列数据类型htm
列表和元组之间的主要区别是:列表括在括号([])和它们的元素和大小是能够改变的,而元组在圆括号(),不能被更新。元组能够被认为是只读列表。例如:对象
#!/usr/bin/python tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 ) tinytuple = (123, 'john') print tuple # Prints complete list ('abcd', 786, 2.23, 'john', 70.200000000000003) print tuple[0] # Prints first element of the list abcd print tuple[1:3] # Prints elements starting from 2nd till 3rd (786, 2.23) print tuple[2:] # Prints elements starting from 3rd element (2.23, 'john', 70.200000000000003) print tinytuple * 2 # Prints list two times (123, 'john', 123, 'john') print tuple + tinytuple # Prints concatenated lists ('abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john')
如下是元组无效的,由于咱们尝试更新一个元组,这是不容许的。相似的操做在列表中是能够的:
#!/usr/bin/python
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tuple[2] = 1000 # Invalid syntax with tuple
list[2] = 1000 # Valid syntax with list
Python字典:
#!/usr/bin/python dict = {} dict['one'] = "This is one" dict[2] = "This is two" tinydict = {'name': 'john','code':6734, 'dept': 'sales'} print dict['one'] # Prints value for 'one' key This is one print dict[2] # Prints value for 2 key This is two print tinydict # Prints complete dictionary {'dept': 'sales', 'code': 6734, 'name': 'john'} print tinydict.keys() # Prints all the keys ['dept', 'code', 'name'] print tinydict.values() # Prints all the ['sales', 6734, 'john']
函数 | 描述 |
---|---|
int(x [,base]) |
将x转换为一个整数。基数指定为base,若是x是一个字符串。 |
long(x [,base] ) |
将x转换为一个长整数。基数指定为base,若是x是一个字符串。 |
float(x) |
将x转换到一个浮点数。 |
complex(real [,imag]) |
建立一个复数。 |
str(x) |
转换对象x为字符串表示形式。 |
repr(x) |
对象x转换为一个表达式字符串。 |
eval(str) |
计算一个字符串,并返回一个对象。 |
tuple(s) |
把s转换为一个元组。 |
list(s) |
把s转换为一个列表。 |
set(s) |
把s转换为一个集合。 |
dict(d) |
建立一个字典。 d必须的(键,值)元组序列。 |
frozenset(s) |
把s转换为冻结集。 |
chr(x) |
整数转换为一个字符。 |
unichr(x) |
整数转换为一个Unicode字符。 |
ord(x) |
转换单个字符为整数值。 |
hex(x) |
将整数转换为十六进制字符串。 |
oct(x) |
将整数转换为以八进制的字符串。 |
http://www.jb51.net/article/77351.htm
http://blog.sina.com.cn/s/blog_708be8850101b702.html
http://www.cnblogs.com/linjiqin/p/3608541.html
http://www.jb51.net/article/47956.htm