1 >>>7+3 #加法 2 10 3 >>>7-3 #减法 4 4 5 >>>7*3 #乘法 6 21 7 >>>7/3 #除法 8 2 9 >>>5**2 #乘方10 2511 >>>7%3 #求余数12 113 >>>a=7.0/2 #float除以int,结果为float14 >>>PRint a,type(a)15 3.5 <type 'float'>
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)排序
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的拷贝,即新的list11 [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的立方为6417 64 18 >>>cubes[3] = 6419 >>>cubes20 [1,8,27,64,125]21 >>>cubes.append(216)22 >>>cubes.append(7**3)23 >>>cubes24 [1,8,27,64,125,216,343]25 26 >>>letters = [‘a’,’b’,’c’,’d’,’e’,’f’,’g’]27 >>>letters28 [‘a’,’b’,’c’,’d’,’e’,’f’,’g’]29 >>>letters[2:5] = [‘C’,’D’,’E’]30 >>>letters31 [‘a’,’b’,’C’,’D’,’E’,’f’,’g’]32 >>>letters[2:5]=[]33 >>>letters34 [‘a’,’b’,’f’,’g’]35 >>>letters[:]=[]36 >>>letters37 []38 39 >>>letters = [‘a’,’b’,’c’,’d’]40 >>>len(letters)41 442 43 >>>a=[‘a’,’b’,’c’]44 >>>n=[1,2,3]45 >>>x=[a,n]46 >>>x47 [[‘a’,’b’,’c’],[1,2,3]]48 >>>x[0]49 [‘a’,’b’,’c’]50 >>>x[0][1]51 ‘b‘
变量不需要声明,不需要删除,可以直接回收使用。
type(): 查询数据类型
String和List区别:String只能读取,不能用等号(=)赋值替换,List即可读取,也可以直接用等号(=)赋值替换
新闻热点
疑难解答