import copy
#浅拷贝
n1={'k1':'wu','k2':123,'k3':['carl',852]}
n2=n1
n3=copy.copy(n1)
print(id(n1))
print(id(n2))
print(id(n3))
print(id(n1['k3']))
print(id(n3['k3']))
#深拷贝
n4=copy.deepcopy(n1)
print(id(n4))
print(id(n1['k3']))
print(id(n4['k3']))
返回值:
10787656
10787656
11532848
20277688
20277688
11455064
20277688
20276328
二.函数的基本定义
def mail():
def func(name, age = 18):
print"%s:%s" %(name,age)
# 指定参数
func('wupeiqi', 19)
# 使用默认参数
func('alex')
def func(*args):
print args
# 执行方式一
func(11,33,4,4454,5)
# 执行方式二
li = [11,2,2,3,3,4,54]
func(*li
def func(**kwargs):
print args
# 执行方式一
func(name='wupeiqi',age=18)
# 执行方式二
li = {'name':'wupeiqi', age:18, 'gender':'male'}
func(**li)
def show(*arg,**kwargs):
print(arg,type(arg))
print(kwargs,type(kwargs))
show(64,56,99,w=76,p=33)
s1 ="{0} is {1}"
l=['alex','sb']
result=s1.format(*l)
print(result)
s1 = "{name} is {a}"
result=s1.format(name='helen',a=19)
print(result) s1 = "{name} is {a}"
d={"name":"helen",'a':19}
#result=s1.format(name='helen',a=19)
result=s1.format(**d)
print(result)
def func(a):
b=a+1
return b
等于
func=lambda a:a+1
ret=func(5)
print(ret)
三.内置函数
print(ascii(10), ascii(9000000), ascii('b\31'), ascii('0x\1000'))
返回结果:
10 9000000 'b\x19' '0x@0'
#!usr/bin/env python
#coding:utf-8
namespace = {'name':'wupeiqi','data':[18,73,84]}
code = '''def hellocute():return "name %s ,age %d" %(name,data[0],) '''
func = compile(code, '<string>', "exec")
exec func in namespace
result = namespace['hellocute']()
print result
divmod(a,b)方法返回的是a//b(除法取整)以及a对b的余数
返回结果类型为tuple
参数:
a,b可以为数字(包括复数)
版本:
在python2.3版本之前不允许处理复数,这个大家要注意一下
英文说明:
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).
Changed in version 2.3: Using divmod() with complex numbers is deprecated.
python代码实例:
>>> divmod(9,2)
(4, 1)
>>> divmod(11,3)
(3, 2)
>>> divmod(1+2j,1+0.5j)
((1+0j), 1.5j)
enumerate()用于遍历序列中的元素以及它们的下标