数据类型
Python的数据属于某一种类型,相同类型的数据可以做运算,不同类型的数据运算会报错
>> > n = input ( 'number: ' )
number: 10
>> > n + 5
>> > int ( n) + 5
15
>> > n + str ( 5 )
'105'
1)数字
基本数字类型
int:有符号整数 bool:布尔值
float:浮点数 complex:复数
数字表示方式
python默认以十进制数显示(没有前缀) 数字以0o或0O开头表示为8进制数 数字以0x或0X开头表示16进制数 数字以0b或0B开头表示2进制数
2)字符串
python中字符串被定义为引号之间的字符集合 python支持使用成对的单引号或双引号无论单引号,还是双引号,表示的意义相同 python还支持三引号(三个连续的单引号或者双引号),可以用来包含特殊字符 python不区分字符和字符串
>> > s1 = """hello
... nice
... ban"""
>> > print ( s1)
hello
nice
ban
>> > s1
'hello\nnice\nban'
>> > s2 = "tom\njerry\npeppa"
>> > print ( s2)
tom
jerry
peppa
字符串操作
>> > s1 = 'python'
>> > len ( s1)
6
>> > s1[ 0 ]
'p'
>> > s1[ 5 ]
'n'
>> > s1[ 6 ]
>> > s1[ - 1 ]
'n'
>> > s1[ - 6 ]
'p'
>> > s1[ 2 : 4 ]
'th'
>> > s1[ 2 : 6 ]
'thon'
>> > s1[ 2 : 6000 ]
'thon'
>> > s1[ 2 : ]
'thon'
>> > s1[ : 2 ]
'py'
>> > s1[ : ]
'python'
>> > s1[ : : 2 ]
'pto'
>> > s1[ 1 : : 2 ]
'yhn'
>> > s1[ : : - 1 ]
'nohtyp'
>> > 't' in s1
True
>> > 'th' in s1
True
>> > 'to' in s1
False
>> > 'to' not in s1
True
>> > 'hello' + 'world'
'helloworld'
>> > '*' * 50
'**************************************************'
>> > s1 * 5
格式化操作符
格式化字符 转换方式 %c
转换成字符 %s
优先用str函数进行字符串转换 %d
/ %i
转成有符号十进制数 %o
转成无符号八进制数 %x
转成无符号十六进制数 %e
/%e
转成科学计数法 %f
/%F
转成浮点数转换方式
'' % ( )
>> > '%s is %s years old' % ( 'tom' , 20 )
'tom is 20 years old'
>> > 'Hello %s' % ( 'jerry' )
'Hello jerry'
>> > 'Hello %s' % 'jerry'
'Hello jerry'
>> > '%s is %d years old' % ( 'tom' , 20 )
'tom is 20 years old'
>> > '%d is %d years old' % ( 'tom' , 20 )
>> > '%s is %d years old' % ( 'tom' , 20.5 )
'tom is 20 years old'
辅助指令 作用 *
定义宽度或者小数点精度 -
左对齐 +
在正数前面显示加号 <sp>
在正数前面显示空格 #
在八进制数前面显示零0,在十六进制前面显示Ox或者oX’ 0
显示的数字前面填充0而不是默认的空格
>> > '{} is {} years old' . format ( 'tom' , 20 )
'tom is 20 years old'
>> > '{} is {} years old' . format ( 'tom' , 20 )
'tom is 20 years old'
>> > '{1} is {0} years old' . format ( 20 , 'tom' )
'tom is 20 years old'
>> > win_path = 'c:\temp'
>> > print ( win_path)
c: emp
>> > wpath = r'c:\temp'
>> > print ( wpath)
c: \temp
>> > wpath
'c:\\temp'
>> > win_path = 'c:\\temp'
>> > print ( win_path)
c: \temp
>> > s1 = 'HELLO world'
>> > s1. upper( )
'HELLO WORLD'
>> > s1. lower( )
'hello world'
>> > s1. center( 50 )
' HELLO world '
>> > s1. center( 50 , '#' )
'###################HELLO world####################'
>> > s1. ljust( 50 , '*' )
'HELLO world***************************************'
>> > s1. rjust( 50 )
' HELLO world'
>> > s2 = ' hello world\n'
>> > s2. strip( )
'hello world'
>> > s2. lstrip( )
'hello world\n'
>> > s2. rstrip( )
' hello world'
string.capitalize()
:把字符串的第一个字符大写string.center(width)
:返回一个原字符串居中,并 使用空格填充至长度 width的新字符串string.count(str,beg=0,end=len(string))
:返回str在 string里面出现的次数,如果beg或者end指定则返回指定范围内str出现的次数string.endswith(obj,beg=0,end=len(string)
: 检查字符串是否以obj结束,如果beg或者end指定则检查指定的范围内是否以obj结束,如果是,返回True, 否则返回 Falsestring.islower()
:如果 string中包含至少一个区分大小写的字符,并且所有这些字符都是小写,则返回True,否则返回 Falsestring.strip()
:删除string 字符串两端的空白string.upper()
:转换string中的小写字母为大写string.split(str="",num=string.count(str))
:以str为分隔符切片string,如果num有指定值,则仅分隔num个子字符串
3)列表
可以将列表当成普通的“数组”,它能保存任意数量任意类型的 python对象 像字符串一样,列表也支持下标和切片操作 列表中的项目可以改变 属于容器、可变、顺序类型
>> > l1 = [ 10 , 15 , 'tom' , 'jerry' , [ 1 , 2 , 3 ] ]
>> > l1 = [ 10 , 15 , 'tom' , 'jerry' , [ 1 , 2 , 3 ] ]
>> > len ( l1)
5
>> > l1[ 0 ]
10
>> > l1[ - 1 ]
[ 1 , 2 , 3 ]
>> > l1[ 2 : 4 ]
[ 'tom' , 'jerry' ]
>> > 'tom' in l1
True
>> > 2 in l1
False
>> > l1[ - 1 ] = 100
>> > l1
[ 10 , 15 , 'tom' , 'jerry' , 100 ]
>> > l1 + 20
>> > l1 + [ 20 ]
[ 10 , 15 , 'tom' , 'jerry' , 100 , 20 ]
>> > l1 * 2
[ 10 , 15 , 'tom' , 'jerry' , 100 , 10 , 15 , 'tom' , 'jerry' , 100 ]
>> > l1
[ 10 , 15 , 'tom' , 'jerry' , 100 ]
>> > l1 = [ 10 , 8 , 20 , 10 , 35 , 2 ]
>> > l1[ 0 ] = 20
>> > l1[ 1 : 3 ] = [ 10 , 20 , 30 , 40 ]
>> > l1
[ 20 , 10 , 20 , 30 , 40 , 10 , 35 , 2 ]
>> > l1[ 1 : 1 ]
[ ]
>> > l1[ 1 : 1 ] = [ 100 , 200 ]
>> > l1
[ 20 , 100 , 200 , 10 , 20 , 30 , 40 , 10 , 35 , 2 ]
函数 作用 list.append(obj)
向列表中追加一个obj list.extend(seq)
把序列seq的内容添加到列表中 list.count(obj)
返回一个对象obj在列表中出现的次数 list.index(obj)
返回ob对象的下标 list.insert(index,obj)
在索引量为index的位置插入对象obj list.reverse()
原地翻转列表 list.sort()
排序 list.remove(obj)
删除列表中第一个obj list.pop(index)
弹出(删除并返回值)指定下标的obj,index默认为-1
>> > l1. append( [ 1 , 2 , 3 ] )
>> > l1
[ 20 , 100 , 200 , 10 , 20 , 30 , 40 , 10 , 35 , 2 , 20 , [ 1 , 2 , 3 ] ]
>> > l1. extend( [ 1 , 2 , 3 ] )
>> > l1
[ 20 , 100 , 20 , 200 , 10 , 20 , 30 , 40 , 10 , 35 , 2 , 20 , [ 1 , 2 , 3 ] , 1 , 2 , 3 ]
>> > l1. insert( 2 , 20 )
>> > l1. count( 20 )
4
>> > l1. index( 20 )
0
>> > l1. index( 20 , 1 )
>> > l1. remove( 20 )
>> > l1
[ 100 , 20 , 200 , 10 , 20 , 30 , 40 , 10 , 35 , 2 , 20 , [ 1 , 2 , 3 ] , 1 , 2 , 3 ]
>> > l1. pop( )
3
>> > a = l1. remove( 20 )
>> > print ( a)
None
>> > b = l1. pop( )
>> > b
2
>> > l1. pop( - 2 )
[ 1 , 2 , 3 ]
>> > l1
[ 100 , 200 , 10 , 20 , 30 , 40 , 10 , 35 , 2 , 20 , 1 ]
>> > l1. sort( )
>> > l1
[ 1 , 2 , 10 , 10 , 20 , 20 , 30 , 35 , 40 , 100 , 200 ]
>> > l1. reverse( )
>> > l1
[ 200 , 100 , 40 , 35 , 30 , 20 , 20 , 10 , 10 , 2 , 1 ]
>> > l2 = l1
>> > l2
[ 200 , 100 , 40 , 35 , 30 , 20 , 20 , 10 , 10 , 2 , 1 ]
>> > l2. pop( 0 )
200
>> > l2
[ 100 , 40 , 35 , 30 , 20 , 20 , 10 , 10 , 2 , 1 ]
>> > l1
[ 100 , 40 , 35 , 30 , 20 , 20 , 10 , 10 , 2 , 1 ]
>> > l3 = l1. copy( )
>> > l3
[ 100 , 40 , 35 , 30 , 20 , 20 , 10 , 10 , 2 , 1 ]
>> > l3. pop( 0 )
100
>> > l3
[ 40 , 35 , 30 , 20 , 20 , 10 , 10 , 2 , 1 ]
>> > l1
[ 100 , 40 , 35 , 30 , 20 , 20 , 10 , 10 , 2 , 1 ]
>> > l3. clear( )
>> > l3
[ ]
4)元组
可以认为元组是“静态”的列表 元组一旦定义,不可改变
>> > t1 = ( 10 , 15 , 'tom' , 'jerry' , 100 , 15 )
>> > len ( t1)
6
>> > t1[ 0 ]
10
>> > t1[ 2 : 4 ]
( 'tom' , 'jerry' )
>> > t1[ 0 ] = 20
>> > t1 = ( 22 , 10 , 3 , 20 )
>> > t1. count( 10 )
1
>> > t1. index( 3 )
2
如果元组只有一项,必须在元素后面加逗号,否则它不是元组
>> > t2 = ( 10 )
>> > t2
10
>> > type ( t2)
< class 'int' >
>> > t3 = ( 10 , )
>> > type ( t3)
< class 'tuple' >
>> > len ( t3)
1
>> > t3
( 10 , )
5)字典
采用key:val的形式存储各项 属于容器、可变、映射 字典的key是唯一的,不能出现多于一次 字典的key必须时不可变的(数字、字符串、元组)
>> > d1 = { 'name' : 'tom' , 'age' : 20 }
>> > len ( d1)
2
>> > d1[ 'name' ]
'tom'
>> > 'tom' in d1
False
>> > 'name' in d1
True
>> > d1 = { 'name' : 'tom' , 'name' : 'jerry' , 'age' : 20 }
>> > d1
{ 'name' : 'jerry' , 'age' : 20 }
>> > d2 = dict ( [ ( 'name' , 'tom' ) , ( 'age' , 20 ) , ( 'email' , 'tom@xxx.cn' ) ] )
>> > d2
{ 'name' : 'tom' , 'age' : 20 , 'email' : 'tom@tedu.cn' }
>> > d3 = { } . fromkeys( [ 'tom' , 'jerry' , 'bob' , 'alice' ] , 20 )
>> > d3
{ 'tom' : 20 , 'jerry' : 20 , 'bob' : 20 , 'alice' : 20 }
>> > len ( d2)
3
>> > 'tom' in d2
False
>> > 'name' in d2
True
>> > for k in d2:
. . . print ( k, d2[ k] )
>> > d2[ 'age' ] = 25
>> > d2
{ 'name' : 'tom' , 'age' : 25 , 'email' : 'tom@xxx.cn' }
>> > d2[ 'phone' ] = '15012345678'
>> > d2
{ 'name' : 'tom' , 'age' : 25 , 'email' : 'tom@xxx.cn' , 'phone' : '15012345678' }
>> > del d2[ 'phone' ]
>> > d2
{ 'name' : 'tom' , 'age' : 25 , 'email' : 'tom@tedu.cn' }
>> > d2
{ 'name' : 'tom' , 'age' : 25 , 'email' : 'tom@tedu.cn' }
>> > user = d2. get( 'name' )
>> > user
'tom'
>> > password = d2. get( 'password' )
>> > print ( password)
None
>> > email = d2. get( 'email' , 'not found' )
>> > password = d2. get( 'password' , 'not found' )
>> > email
'tom@xxx.cn'
>> > password
'not found'
>> > d2. keys( )
dict_keys( [ 'name' , 'age' , 'email' ] )
>> > d2. values( )
dict_values( [ 'tom' , 25 , 'tom@tedu.cn' ] )
>> > d2. items( )
dict_items( [ ( 'name' , 'tom' ) , ( 'age' , 25 ) , ( 'email' , 'tom@tedu.cn' ) ] )
>> > list ( d2. items( ) )
[ ( 'name' , 'tom' ) , ( 'age' , 25 ) , ( 'email' , 'tom@tedu.cn' ) ]
>> > d2. pop( 'email' )
'tom@tedu.cn'
>> > d2
{ 'name' : 'tom' , 'age' : 25 }
>> > help ( d2. popitem)
>> > d2. update( { 'qq' : '132523452' , 'address' : 'shijiazhuang' } )
>> > d2
{ 'name' : 'tom' , 'age' : 25 , 'qq' : '132523452' , 'address' : 'shijiazhuang' }
6)集合
集合由不同元素构成的 集合元素必须是不可变对象 集合没有顺序 集合就像是一个无值的字典 可变集合是set,不可变集合是frozenset
>> > s1 = { 'tom' , 'jerry' , 1 , 20 }
>> > s1
{ 1 , 'tom' , 'jerry' , 20 }
>> > type ( s1)
< class 'set' >
>> > len ( s1)
4
>> > for data in s1:
. . . print ( data)
>> > s2 = set ( 'abc' )
>> > s2
{ 'a' , 'b' , 'c' }
>> > s3 = set ( 'bcd' )
>> > s3
{ 'b' , 'c' , 'd' }
>> > s2 & s3
{ 'b' , 'c' }
>> > s2 | s3
{ 'a' , 'b' , 'c' , 'd' }
>> > s2 - s3
{ 'a' }
>> > s3 - s2
{ 'd' }
>> > s1. add( 200 )
>> > s1
{ 1 , 200 , 'tom' , 20 , 'jerry' }
>> > s1. pop( )
1
>> > s1. remove( 20 )
>> > s1
{ 200 , 'tom' , 'jerry' }
>> > s1. update( [ 100 , 200 , 300 ] )
>> > s1
{ 100 , 200 , 'tom' , 300 , 'jerry' }
>> > s2. intersection( s3)
{ 'b' , 'c' }
>> > s2. union( s3)
{ 'a' , 'b' , 'c' , 'd' }
>> > s2. difference( s3)
{ 'a' }
>> > s4 = { 100 , 200 }
>> > s4. issubset( s1)
True
>> > s1. issuperset( s4)
True
>> > from random import randint
>> > nums = [ randint( 1 , 50 ) for i in range ( 20 ) ]
>> > set ( nums)
{ 32 , 3 , 35 , 8 , 13 , 46 , 49 , 17 , 20 , 22 , 23 , 25 , 30 , 31 }
>> > nums2 = list ( set ( nums) )
>> > nums2
[ 32 , 3 , 35 , 8 , 13 , 46 , 49 , 17 , 20 , 22 , 23 , 25 , 30 , 31 ]
案例:取出两个文件不同的内容 web服务每天产生大量的日志,每天的日志文件500万行以上。日志中存储了客户端访问web服务的哪个url,每天的url提取到一个文件中,如a.log / b.log,获取哪些url是今天新访问的
[ root@localhost day05]
[ root@localhost day05]
[ root@localhost day05]
>> > with open ( '/tmp/mima1' ) as fobj:
. . . s1 = set ( fobj)
>> > with open ( '/tmp/mima2' ) as fobj:
. . . s2 = set ( fobj)
>> > s2 - s1
{ 'chi le ma?\n' , 'hello world!\n' }
>> > with open ( '/tmp/mima3' , 'w' ) as fobj:
. . . fobj. writelines( s2 - s1)
[ root@localhost day05]
数据类型分类
1)按存储模型分类
2)按更新模型分类
>> > l1 = [ 1 , 2 , 3 ]
>> > s1 = 'python'
>> > l1[ 0 ] = 10
>> > s1[ 0 ] = 'P'
>> > s1 = 'Python'
>> > l1
[ 10 , 2 , 3 ]
>> > l2 = l1
>> > l2
[ 10 , 2 , 3 ]
>> > l2. append( 100 )
>> > l2
[ 10 , 2 , 3 , 100 ]
>> > l1
[ 10 , 2 , 3 , 100 ]
3)按访问模型分类
直接访问:数字 顺序访问:字符串、列表、元组 映射访问:字典
序列(字符串、列表、元组)
序列操作符 作用 seq[ind] 获得下表为ind的元素 seq[ind1:ind2] 获得下标从ind1到ind2之间的元素集合 seq * expr 序列重复expr次 seq1 + seq2 连接序列seq1和seq2 obj in seq 判断obj元素是否在seq中 obj not in seq 判断obj元素是否不包含在seq中
函数 含义 list() 把可迭代对象转换为列表 str() 把对象转换成字符串 tuple() 把一个可迭代对象转换成一个元组对象 len() 返回对象的长度 enumerate() 接收一个可迭代对象作为参数,返回enumerate参数 reversed() 接受一个序列作为参数,返回一个逆序的序列 sorted() 接受一个可迭代对象作为参数,返回一个有序的列表
>> > from random import randint
>> > nums = [ randint( 1 , 100 ) for i in range ( 10 ) ]
>> > nums
[ 32 , 51 , 3 , 96 , 32 , 28 , 19 , 47 , 34 , 60 ]
>> > a = sorted ( nums)
>> > a
[ 3 , 19 , 28 , 32 , 32 , 34 , 47 , 51 , 60 , 96 ]
>> > for i in reversed ( nums) :
. . . print ( i)
>> > enumerate ( nums)
< enumerate object at 0x7f615dd568b8 >
>> > list ( enumerate ( nums) )
>> > for data in enumerate ( nums) :
. . . print ( data)
. . .
( 0 , 32 )
( 1 , 51 )
( 2 , 3 )
( 3 , 96 )
( 4 , 32 )
( 5 , 28 )
( 6 , 19 )
( 7 , 47 )
( 8 , 34 )
( 9 , 60 )
>> > for data in enumerate ( nums) :
. . . print ( data[ 0 ] , data[ 1 ] )
. . .
0 32
1 51
2 3
3 96
4 32
5 28
6 19
7 47
8 34
9 60
>> > for a, b in enumerate ( nums) :
. . . print ( a, b)
. . .
0 32
1 51
2 3
3 96
4 32
5 28
6 19
7 47
8 34
9 60
排序
字典没有顺序,如果为字典排序,需要先将字典转成序列对象 复杂列表排序:根据列表中元组的最后一项排序
列表的sort方法接受一个名为key的变量。 help(list.sort) key变量是一个函数。该函数作用于每个列表项,将列表项作为key函数的参数,key函数的返回值作为排序依据
>> > d1 = { '172.40.58.150' : 10 , '172.40.58.124' : 6 , '172.40.58.101' : 10 , '127.0.0.1' : 121 , '192.168.4.254' : 103 , '192.168.2.254' : 110 , '201.1.1.254' : 173 , '201.1.2.254' : 119 , '172.40.0.54' : 391 , '172.40.50.116' : 244 }
>> > l1 = list ( d1. items( ) )
>> > l1
[ ( '172.40.58.150' , 10 ) , ( '172.40.58.124' , 6 ) , ( '172.40.58.101' , 10 ) , ( '127.0.0.1' , 121 ) , ( '192.168.4.254' , 103 ) , ( '192.168.2.254' , 110 ) , ( '201.1.1.254' , 173 ) , ( '201.1.2.254' , 119 ) , ( '172.40.0.54' , 391 ) , ( '172.40.50.116' , 244 ) ]
>> > def get_last ( seq) :
. . . return seq[ - 1 ]
. . .
>> > l1. sort( key= get_last, reverse= True )
>> > l1
[ ( '172.40.0.54' , 391 ) , ( '172.40.50.116' , 244 ) , ( '201.1.1.254' , 173 ) , ( '127.0.0.1' , 121 ) , ( '201.1.2.254' , 119 ) , ( '192.168.2.254' , 110 ) , ( '192.168.4.254' , 103 ) , ( '172.40.58.150' , 10 ) , ( '172.40.58.101' , 10 ) , ( '172.40.58.124' , 6 ) ]
>> > l1. sort( key= lambda seq: seq[ - 1 ] , reverse= True )