Python-Day3知識點——深淺拷貝、函數基本定義、內置函數

一.深淺拷貝
 1 import copy
 2 #淺拷貝
 3 n1={'k1':'wu','k2':123,'k3':['carl',852]}
 4 n2=n1
 5 n3=copy.copy(n1)
 6 print(id(n1))
 7 print(id(n2))
 8 print(id(n3))
 9 print(id(n1['k3']))
10 print(id(n3['k3']))
11 #深拷貝
12 n4=copy.deepcopy(n1)
13 print(id(n4))
14 print(id(n1['k3']))
15 print(id(n4['k3']))
返回值:

10787656
10787656
11532848
20277688
20277688
11455064
20277688
20276328python

二.函數的基本定義數組

1.默認參數:
1 def mail():
2 def func(name, age = 18):
3 print"%s:%s" %(name,age)
4 # 指定參數
5 func('wupeiqi', 19)
6 # 使用默認參數
7 func('alex')
2.動態參數序列:
1 def func(*args):
2     print args
3 # 執行方式一
4 func(11,33,4,4454,5)
5 # 執行方式二
6 li = [11,2,2,3,3,4,54]
7 func(*li
3.動態參數字典:
1 def func(**kwargs):
2     print args
3 # 執行方式一
4 func(name='wupeiqi',age=18)
5 # 執行方式二
6 li = {'name':'wupeiqi', age:18, 'gender':'male'}
7 func(**li)
4.序列和字典:
def show(*arg,**kwargs):
print(arg,type(arg))
print(kwargs,type(kwargs))
show(64,56,99,w=76,p=33)
5.使用動態參數對字符串格式化:
 1 s1 ="{0} is {1}"
 2 l=['alex','sb']
 3 result=s1.format(*l)
 4 print(result)
 5 s1 = "{name} is {a}"
 6 result=s1.format(name='helen',a=19)
 7 print(result)
 8 
 9 s1 = "{name} is {a}"
10 d={"name":"helen",'a':19}
11 #result=s1.format(name='helen',a=19)
12 result=s1.format(**d)
13 print(result)
 
6.lambda表達式:
lambda表達式等於簡單函數表達方式
1 def func(a):
2     b=a+1
3 return b
4 等於
5 func=lambda a:a+1
6 ret=func(5)
7 print(ret)

三.內置函數app

abs()絕對值
all()若是傳入的對象元素爲真(不爲空)則爲真
any()一真則真
ascii() 當遇到非ASCII碼時,就會輸出\x,\u或\U等字符來表示
example:
1 print(ascii(10), ascii(9000000), ascii('b\31'), ascii('0x\1000')) 
2 返回結果:
3 10   9000000   'b\x19'   '0x@0'
bin()二進制轉化
bytearray()字符串轉換數組
callable()判斷對象是否可被調用
chr()數字轉爲ascii
ord()ascii轉化爲數字, 寫驗證碼用
compile()字符串轉換爲Python代碼
1 #!usr/bin/env python
2 #coding:utf-8
3 namespace = {'name':'wupeiqi','data':[18,73,84]}
4 code =  '''def hellocute():return  "name %s ,age %d" %(name,data[0],) '''
5 func = compile(code, '<string>', "exec")
6 exec func in namespace
7 result = namespace['hellocute']()
8 print result
complex()負數
delattr/getattr/setattr/hasattr()反射用
dictionary()建立字典
divmod()
中文說明:

divmod(a,b)方法返回的是a//b(除法取整)以及a對b的餘數less

返回結果類型爲tuple函數

參數:

a,b能夠爲數字(包括複數)

版本:

在python2.3版本以前不容許處理複數,這個你們要注意一下spa

英文說明:

Take two (non complex) numbers as arguments and return a pair of numbers consisting of their quotient and remainder when using long division. With mixed operand types, the rules for binary arithmetic operators apply. For plain and long integers, the result is the same as (a // b, a % b). For floating point numbers the result is (q, a % b), where q is usually math.floor(a / b) but may be 1 less than that. In any case q * b + a % b is very close to a, if a % b is non-zero it has the same sign as b, and 0 <= abs(a % b) < abs(b).3d

Changed in version 2.3: Using divmod() with complex numbers is deprecated.code

python代碼實例:

1 >>> divmod(9,2)
2 (4, 1)
3 >>> divmod(11,3)
4 (3, 2)
5 >>> divmod(1+2j,1+0.5j)
6 ((1+0j), 1.5j)
enumerate()用於遍歷序列中的元素以及它們的下標
map()第一個參數接收一個函數名,第二個參數接收一個可迭代對象
filter()過濾
float()
format()
frozenset()凍結集合
globals()全局變量
hash()字典鍵哈希
hex()計算十六進制
locals()本地變量
memoryview()
oct()八進制轉換
pow()冪運算
range()迭代器
round()四捨五入
slice()切片
sorted()排序
str()
sum()求和
super()執行父類
dir()返回key
vars()返回鍵值對
zip()列表壓縮
相關文章
相關標籤/搜索