一、字典
1、初识字典
'' '
字典(dict)
- 字典属于一种新的数据结构,称为映射(mapping)
- 字典的作用和列表类型,都是用来存储对象的容器
- 列表存储数据的性能很好,但是查询数据的性能很差
- 在字典中每一个元素都有一个唯一的名字,通过这个名字可以快速查找到指定的元素
- 在查询元素时,字典的效率是非常快的
- 在字典中可以保存多个对象,每个对象都会有唯一的名字
这个唯一的名字,我们称为键(key),通过key可以快速的查找到value
这个对象,我们称为值(value)
所以字典,我们可以称为键值对(key:value)结构
'' '
d = { }
d = { 'name' : '孙小姐' ,'age' :18,'gender' : '女' }
print( d,type( d))
print( d[ 'name' ] ,d[ 'age' ] ,d[ 'gender' ] )
print( d[ 'helle' ] )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
2、字典的使用
d = dict( name= '孙小姐' ,age= 18 ,gender= '女' )
print( d)
d = dict( [ ( 'name' ,'孙小姐' ) ,( 'age' ,18) ] )
print( d,type( d))
d = dict( name= '孙小姐' ,age= 18 ,gender= '18' )
print( len( d))
print( 'hello' in d)
print( d[ 'age' ] )
print( d.get( 'name' ))
print( d.get( 'hello' ,'没有' ))
d[ 'name' ] = 'sunwukong'
d[ 'address' ] = 'hangzhou'
print( d)
d = { 'a' :1,'b' :2,'c' :3}
d2 = { 'd' :4,'e' :5,'f' :6,'a' :7}
print( d)
print( d2)
d.update( d2)
print( d)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
del d[ 'a' ]
del d[ 'b' ]
print( d)
result = d.popitem( )
print( result)
print( d)
result = d.pop( 'd' )
result = d.pop( 'z' ,'这是默认值' )
print( result)
d.clear( )
print( d)
d = { 'a' :1,'b' :2,'c' :3}
d2 = d.copy( )
d = { 'a' :{ 'name' : '孙小姐' ,'age' :18} ,'b' :2,'c' :3}
d2 = d.copy( )
d2[ 'a' ] [ 'name' ] = '猪猪'
print( 'd =' ,d,id( d))
print( 'd2 =' ,d2,id( d2))
d = { 'name' : '孙小姐' ,'age' :18,'gender' : '女' }
for k in d.keys( ) :
print( k, d[ k] )
for k,v in d.items( ) :
print( k, '=' , v )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
二、集合
1、初始集合
'' '
集合(set)
集合和列表非常像
不同点:
集合中只能存储不可变对象
集合中存储的对象是无序的(不是按元素的插入顺序保存)
集合中不能出现重复的元素
'' '
s = { 1,20 ,3,6,1}
print( s,type( s))
t = set( )
print( t,type( t))
s = set( [ 1,2 ,3,5,6,8,3,6,732,3] )
print( s)
s = set( { 'a' :1,'b' :2,'c' :5} )
print( s)
print( list( s) [ 2 ] )
print( s)
print( 'p' in s)
print( len( s))
s.add( 'p' )
print( s)
s2 = set( 'hello' )
s.update( s2)
print( s)
s.update(( 10 , 23 , 7 , 3 ))
print( s)
s.update( { 10 :'ab' ,900:'op' } )
print( s)
res = s.pop( )
print( s)
print( res)
s.remove( 900 )
print( s)
print( s,type( s))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
2、集合的运算
s = { 1,2 ,3,4,5}
s2 = { 3,4 ,5,6,7}
result = s & s2
print( 'result = ' ,result)
result = s | s2
print( 'result = ' ,result)
result = s - s2
print( 'result = ' ,result)
result = s ^ s2
print( 'result = ' ,result)
a = { 1,2 ,3}
b = { 1,2 ,3,4}
result = a <= b
print( 'result = ' ,result)
result = { 1,2 ,3} <= { 1,2 ,3}
print( 'result = ' ,result)
result = { 1,2 ,3,4,5} <= { 1,2 ,3}
result = { 1,2 ,3} < { 1,2 ,3}
result = { 1,2 ,3} < { 1,2 ,3,4,5}
print( 'result =' ,result)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
三、函数
1、初始函数
'' '
函数简介
- 函数也是一个对象
- 函数是内存中专门用来存储数据的一块区域
- 函数可以用来保存一些可执行的代码,并且可以在需要时,对这些语句进行多次调用
- 创建函数:
def 函数名( [ 形参1,形参2,.. .形参n] )
代码块
- 函数名必须是要符合标识符的规范
(可以包含字面,数字,下划线,但是不能以数字开头)
- 函数中保存的代码不会立即执行,需要去调用才能被执行
- 调用函数
函数对象( )
- 定义函数一般都是要实现某种功能的
'' '
def fn( ) :
print( 'This is a function' )
print( 'hello' )
print( fn,type( fn))
fn( )
'' '
函数(function)
函数的定义都是要有功能的
- 在定义函数时,可以在函数名后的( ) 中定义数量不等的形参
多个形参之间要使用逗号隔开
- 形参(形式参数),定义参数就是相当于在函数内部声明了变量,但是并不赋值
- 实参(实际参数)
- 如果函数定义时,指定了参数,那么在调用函数时也必须传递实参,
实参将会赋值给对应的形参,简单来说,有几个形参就得传几个实参
'' '
def sum( ) :
print( 1 +1)
sum( )
def fn2( a,b,c) :
print( 'a =' ,a)
print( 'b =' ,b)
print( 'a+b = ' ,a+b)
fn2( 1,2 ,2)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
小练习
def sum( a,b,c) :
print( 'a*b*c = ' ,a*b*c)
sum( 4,5 ,7)
def welcome( username) :
print( '欢迎' ,username+'光临' )
welcome( '孙小姐' )
def user( username) :
if username == 'lai' :
print( 'huanying guike daolai' )
elif username == 'li' :
print( 'huanying VIP daolao' )
else:
print( 'huanying New guke' )
user( 'la' )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
2、函数参数的传递
def fn( a = 5 ,b= 10 ,c= 20 ) :
print( 'a = ' ,a)
print( 'b =' ,b)
print( 'c =' ,c)
fn( 2,3 )
fn( b= 1 ,c= 2 ,a= 3 )
print( 'hello' ,end= '' )
fn( 1 ,c= 30 )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
3、参数的类型
实参可以是任意的数据类型
'' '
参数的类型
'' '
def fn2( a) :
print( 'a =' ,a)
b = 233
fn2( b)
b = 'hello'
fn2( b)
def fn3( a,b) :
print( a+b)
fn3( 123,23 )
def fn4( a) :
a[ 0 ] = 30
print( 'a =' ,a,id( a))
c = 10
c = [ 1,2 ,3]
fn4( c.copy( ))
print( 'c = ' ,c,id( c))
fn4( c[ :] )
print( 'c = ' ,c,id( c))
fn4( c)
print( 'c = ' ,c,id( c))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
4、不定长的参数(装包,解包)
'' '
不定长的参数
'' '
def sum( a,b,c) :
print( a + b + c)
sum( 123,4 ,5)
def sum( *nums) :
print( "nums =" ,nums,type( nums))
result = 0
for n in nums:
result += n
print( result)
sum( 1,2 ,4,6)
def fn2( a,b,*c) :
print( 'a =' ,a)
print( 'b =' ,b)
print( 'c =' ,c)
fn2( 1,2 ,3,4)
def fn2( a,*b,c) :
print( 'a =' ,a)
print( 'b =' ,b)
print( 'c =' ,c)
fn2( 1,2 ,3,c= 4 )
print( '-----------------------' )
def fn5( *,a,b,c) :
print( 'a =' ,a)
print( 'b =' ,b)
print( 'c =' ,c)
fn5( a= 2 ,b= 3 ,c= 4 )
print( '-----------' )
def fn3( b,c,**a) :
print( 'a =' ,a,type( a))
print( 'b=' ,b)
print( 'c=' ,c)
fn3( b= 1 ,d= 2 ,c= 4 )
def fn4( a,b,c) :
print( 'a =' ,a)
print( 'b =' ,b)
print( 'c =' ,c)
t = ( 10,20 ,30)
print( '--------------' )
fn4( *t)
d = { 'a' :100,'b' :200,'c' :300}
fn4( **d)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
5、函数的返回值 return
def sum( *sums) :
result = 0
for n in sums:
result += n
print( result)
sum( 12,3 ,4)
def fn( ) :
return [ 1,2 ,3]
r = fn( )
print( r)
print( '------------' )
def fn( ) :
def fn2( ) :
print( 'hello' )
return fn2
r = fn( )
r( )
def fn2( ) :
a = 10
r = fn2( )
print( r)
def fn3( ) :
print( 'heloo' )
return
print( 'has' )
r = fn3( )
print( r)
print( '-------------' )
def fn4( ) :
for i in range( 5 ) :
if i == 3 :
return
print( i)
print( '循环执行完毕' )
fn4( )
def sum( *sums) :
result = 0
for n in sums:
result += n
return result
r = sum( 12,3 ,4)
print( r + 20 )
def fn5( ) :
return 20
print( fn5)
print( fn5( ))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
6、文档字符串
'' '
文档字符串
help( ) 是python中的内置函数
通过help( ) 函数可以查询python中的函数的用法
语法:help( 函数对象)
help( print)
'' '
def fn( a,b,c) :
return 20
help( fn)
def fn( a:int,b:int,c:str= 'hello' ) :
'' '
这是一个文档字符串的示例
函数的作用: .. ..
函数的参数:
a,作用,类型,默认值
b,作用,类型,默认值
c,作用,类型,默认值
'' '
return 10
help( fn)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
7、作用域global
'' '
作用域( scope)
作用域指的是变量生效的区域
'' '
b = 20
def fn( ) :
a = 10
print( '函数内部:' ,'a =' ,a)
print( '函数内部:' ,'b =' ,b)
fn( )
print( '函数外部:' ,'b =' ,b)
'' '
在python中一共有两个作用域
全局作用域
- 全局作用域在程序执行的时候创建,在程序执行结束时销毁
- 所有函数以为的区域都是全局作用域
- 在全局作用域中定义的变量,都属于全局变量,全局变量可以在任意测程序位置被访问
函数作用域
- 函数作用域在函数调用时创建,在调用结束时被销毁
- 函数每调用一次就会产生一个新的函数作用域
- 在函数作用域中定义的变量,都是局部变量,它只能在函数内部被访问
变量的查找
- 当我们使用变量时,会优先在当前作用域中寻找变量,如果有则使用
如果没有则继续去上一级作用域寻找,如果有则使用
如果依旧没有则继续去上一级作用域寻找,以此类推
直到找到全局作用域,依然没有的话则抛出异常
NameError: name 'b' is not defined
'' '
def fn2( ) :
a = 30
def fn3( ) :
a = 40
print( 'fn3中:' ,'a =' ,a)
fn3( )
fn2( )
def fn3( ) :
global a
a = 10
print( 'a =' ,a)
fn3( )
print( '函数外部:' ,'a =' ,a)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
8、命名空间
'' '
命名空间( namespace)
命名空间指的就是变量存储的位置,每一个变量都需要存储指定的命名空间中
每一个作用域都会有一个它对应的命名空间
全局命名空间,用来保存全局变量,函数的命名空间用来保存函数中的变量
命名空间实际上就是一个字典,是一个专门用来存储变量的字典
locals( ) 用来获取当前作用域的命名空间
如果在全局作用域中调用local( ) 则获取全局命名空间,如果在函数作用域中调用local( ) 则获取函数命名空间
'' '
a = 40
scope = locals( )
print( scope)
print( type( scope))
print( scope[ 'a' ] )
scope[ 'c' ] = 1000
print( c)
def fn4( ) :
a = 10
global_scope = globals( )
global_scope[ 'a' ] = 800
print( scope)
fn4( )
print( a)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
9、递归介绍
'' '
尝试求10的阶层(10!)
'' '
n = 10
for i in range( 1,10 ) :
n *= i
print( n)
def factorial( n) :
'' '
该函数用来求任意数的阶乘
参数:
n 要求阶乘的数字
'' '
result = n
for i in range( 1 ,n) :
result *= i
return result
print( factorial( 10 ))
'' '
递归式函数
递归简单理解就是自己应用自己
递归式函数,在函数中自己调用自己
'' '
def factorial( n) :
'' '
该函数用来求任意数的阶乘
参数:
n 要求阶乘的数字
'' '
if n == 1 :
return 1
return n * factorial( n - 1 )
print( factorial( 10 ))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
练习
def power( n,i) :
'' '
power( ) 用来为任意的数字做幂运算
参数
:param n: 要做幂运算的数字
:param i: 做幂运算的次数
:return:
'' '
if i == 1 :
return n
return n * power( n,i - 1 )
print( power( 2,4 ))
def hui_wen( s) :
'' '
该函数用来检查指定字符串是否是回文字符串,如果是返回True,不是返回False
aram s: 要检查的字符串
:return:
'' '
if len( s) < 2 :
return True
elif s[ 0 ] != s[ -1] :
return False
return hui_wen( s[ 1 :-1] )
print( hui_wen( 'abcba' ))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
10、高阶函数,匿名函数,map
l = [ 1,2 ,3,4,5,6,7,8,9,10]
def fn( lst) :
new_list = [ ]
for n in lst:
if n % 2 == 0 :
new_list.append( n)
return new_list
def fn2( i) :
if i % 2 == 0 :
return True
return False
def fn3( i) :
if i > 5 :
return True
return False
def fn( func,lst) :
new_list = [ ]
for n in lst:
if func( n) :
new_list.append( n)
return new_list
print( fn( fn3,l))
print( fn( fn2,l))
'' '
官网上可以查看 https://docs.python.org/3/library/functions.html
'' '
def fn5( a,b) :
return a + b
lambda a,b:a + b
print( fn5( 3,4 ))
print( type( lambda a,b:a + b))
print(( lambda a, b: a + b) ( 10 , 20 ))
fn6 = lambda a,b:a + b
print( fn6( 4,9 ))
r = filter( lambda i : i % 3 == 0 , l)
print( list( r))
l = [ 1,2 ,3,4,5,6,7,8,9,10]
r = map( lambda i : i + 1 ,l)
print( list( r))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
11、高级函数sort and sorted
l = [ 'bb' ,'aaaa' ,'c' ,'dddd' ,'fff' ]
l.sort( )
print( l)
l.sort( key= len)
print( l)
l = [ 2,5 ,'1' ,3,'6' ,4]
l.sort( key= int)
print( l)
l = "12345678899343224"
print( '排序前:' ,l)
print( sorted( l,key= int))
print( '排序后:' ,l)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
12、闭包
def fn( ) :
def inner( ) :
print( '我是fn2' )
return inner
r = fn( )
r( )
nums = [ 50,30 ,20,10,88]
print( sum( nums) /len( nums))
def make_averager( ) :
nums = [ ]
def averager( n) :
nums.append( n)
return sum( nums) /len( nums)
return averager
averager = make_averager( )
print( averager( 10 ))
print( averager( 10 ))
print( averager( 30 ))
print( averager( 30 ))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44