Numberint int2 147 483 648 2 147 483
数字Number-整数int 整数int表示的范围-2, 147, 483, 648到 2, 147, 483, 647 例如: 0, 100, -100, 012(八进制的10), 0 x 14(十六进制的20) Int的范围示例: >>> num=2147483647 >>> type(num) <type 'int'> >>> num=2147483648 >>> type(num) <type 'long'>
数字Number-浮点float 例如: 0. 0, 12. 0, 18. 8, 3 e+7等 示例: >>> num=0. 0 >>> type(num) <type 'float'> >>> num=12 >>> type(num) <type 'int'> >>> num=12. 0 >>> type(num) <type 'float'> >>> num=-18. 8 >>> type(num) <type 'float'> >>> num=3 e+7 >>> type(num) <type 'float'> >>> num 30000000. 0 >>> print num 30000000. 0
数字Number-复数型complex Python对复数提供内嵌 支持,这是其他大部分软 件所没有的; 复数举例: 3. 14 j, 8. 32 e 36 j 示例: >>> num=3. 14 j >>> type(num) <type 'complex'> >>> num 3. 1400000001 j >>> print num 3. 14 j >>> num=8. 32 e-36 j >>> num 8. 3199999993 e-036 j >>> type(num) <type 'complex'> >>> print num 8. 32 e-036 j
单引号和双引号的示例: >>> print 'spam eggs' spam eggs >>> print 'doesn't' doesn't >>>print ‘doesn’t’ Syntax. Error: invalid syntax >>> print "doesn't" doesn't >>> print ‘ "Yes, " he said. ' "Yes, " he said. >>> print “ "Yes, " he said. “ Syntax. Error: invalid syntax >>> print ""Yes, " he said. " ‘ "Yes, " he said. ' >>> print ‘ "Isn't, " she does’t said. ' "Isn't, " she said. 单引号和双引号的区别 都是成对出现的 可以互相包含 >>> print ' "a" >>> print " 'a' 可以解析转义字符 >>> print ' does't ' does't >>> print " does't " does't 单引号 字符串的定义和使用 Strval = “hello world!” Print Strval[1] Print Strval[1: 10] Print Strval[2: 1]
示例 >>> b = True >>> id(b) 1041552 >>> bb = b >>> id(bb) 1041552 >>> bb = True >>> id(bb) 1041552 >>> b = true Traceback (most recent call last): File "<stdin>", line 1, in ? Name. Error: name 'true' is not defined True/False是Python语言内定的布尔值,使用 true/false, 或者TRUE/FALSE是无效的。 使用布尔表达式 >>> b = 100 < 101 >>> print b True
列表的基本操作: 访问/遍历 >>> list 1[0] 1 >>> list 1[2] 3 >>> list 1 [1, 2, 3, 4, 5] 添加 >>> list 1. append(9) >>> list 1 [1, 2, 3, 4, 5, 9] 修改 >>> list 1[1] = 7 >>> list 1 [1, 7, 3, 4, 5, 9] 删除 >>> list 1. remove(list 1[3]) >>> list 1 [1, 7, 3, 5, 9] 列表的基本运算: 求长: len() >>> list 1 [1, 7, 3, 5, 9] >>> len(list 1) 5 L连接: + >>>list 12=[11, 12, 13] >>> list 1+list 12 [1, 7, 3, 5, 9, 11, 12, 13] 最大值:max() >>> max(list 1) 9 最小值:min() >>> min(list 1) 1
元组的示例 >>> tup 1=(1) >>> tup 1 1 >>> type(tup 1) <type 'int'> >>> tup 1=(1, ) >>> type(tup 1) <type 'tuple'> >>> tup 1[0] 1 >>> tup 1[1] Traceback (most recent call last): File "<pyshell#117>", line 1, in <module> tup 1[1] Index. Error: tuple index out of range >>> tup 1[-1] 元组的访问方式举例: 1 >>> tup 1 (1, )
>>> i = 100 # Create an int object whose value is 100 >>> type(i) <type 'int'> >>> f = 100. 0 >>> type(f) <type 'float'> >>> s = ‘AAAAA’ >>> type(s) <type ‘str’>
谢谢 Thank you!
- Slides: 25