数据类型(Data Type)

1、Numbers


 

 1 >>>7+3      #加法
 2 10
 3 >>>7-3      #减法
 4 4
 5 >>>7*3      #乘法
 6 21
 7 >>>7/3      #除法
 8 2
 9 >>>5**2     #乘方
10 25
11 >>>7%3      #求余数
12 1
13 >>>a=7.0/2     #float除以int,结果为float
14 >>>print a,type(a)
15 3.5 <type 'float'>

2、Strings


 1 >>>word=’Python’
 2 >>>word[0]
 3 ‘P’
 4 >>>word[5]
 5 ‘n’
 6 >>>word[-1]
 7 ‘n’
 8 >>>word[-2]
 9 ‘o’
10 >>>word[-6]
11 ‘P’
12 
13 >>>word[0:2]
14 ‘Py’
15 >>>word[2:5]
16 ‘tho’
17 >>>word[:2]
18 ‘Py’
19 >>>word[-2:]
20 ‘on’
21 >>>word[:2]+word[2:]
22 ‘Python’
23 >>>len(word)
24 6

+---+---+---+---+---+---+

 | P | y | t | h | o | n |

 +---+---+---+---+---+---+

 0   1   2   3   4   5   6

-6  -5  -4  -3  -2  -1

 

字符串“Python”的序号,从左到右按0-6排序,从右到左(-1)-(-6)排序

 

3、Lists


 

 1 >>>squares =[1,4,9,16,25]
 2 >>>squares
 3 [1,4,9,16,25]
 4 >>>squares[0]
 5 1
 6 >>>squares[-1]
 7 25
 8 >>>squares[-3:]
 9 [9,16,25]
10 >>>squares[:]         #返回一个squares的拷贝,即新的list
11 [1,4,9,16,25]
12 >>>squares+[36,49,64,81,100]
13 [1,4,9,16,25,36,49,64,81,100]
14 
15 >>>cubes = [1,8,27,65,125]
16 >>>4**3                #4的立方为64
17 64 
18 >>>cubes[3] = 64
19 >>>cubes
20 [1,8,27,64,125]
21 >>>cubes.append(216)
22 >>>cubes.append(7**3)
23 >>>cubes
24 [1,8,27,64,125,216,343]
25 
26 >>>letters = [‘a’,’b’,’c’,’d’,’e’,’f’,’g’]
27 >>>letters
28 [‘a’,’b’,’c’,’d’,’e’,’f’,’g’]
29 >>>letters[2:5] = [‘C’,’D’,’E’]
30 >>>letters
31 [‘a’,’b’,’C’,’D’,’E’,’f’,’g’]
32 >>>letters[2:5]=[]
33 >>>letters
34 [‘a’,’b’,’f’,’g’]
35 >>>letters[:]=[]
36 >>>letters
37 []
38 
39 >>>letters = [‘a’,’b’,’c’,’d’]
40 >>>len(letters)
41 4
42 
43 >>>a=[‘a’,’b’,’c’]
44 >>>n=[1,2,3]
45 >>>x=[a,n]
46 >>>x
47 [[‘a’,’b’,’c’],[1,2,3]]
48 >>>x[0]
49 [‘a’,’b’,’c’]
50 >>>x[0][1]
51 ‘b‘

 

4、总结


变量不需要声明,不需要删除,可以直接回收使用。

type(): 查询数据类型

String和List区别:String只能读取,不能用等号(=)赋值替换,List即可读取,也可以直接用等号(=)赋值替换

posted on 2015-09-14 16:59  hailunyoo  阅读(357)  评论(0编辑  收藏  举报

导航