• 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
    20276328

    二.函数的基本定义

    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)

    三.内置函数

    abs()绝对值
    all()如果传入的对象元素为真(不为空)则为真
    any()一真则真
    ascii()当遇到非ASCII码时,就会输出x,u或U等字符来表示
    example:
    1 print(ascii(10), ascii(9000000), ascii('b31'), ascii('0x1000')) 
    2 返回结果:
    3 10   9000000   'bx19'   '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的余数

    返回结果类型为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代码实例:

    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()列表压缩
     
  • 相关阅读:
    Java8新特性(一)_interface中的static方法和default方法
    从ELK到EFK演进
    使用Maven构建多模块项目
    maven 把本地jar包打进本地仓库
    在基于acpi的linux系统上如何检查当前系统是否支持深度睡眠?
    linux内核中#if IS_ENABLED(CONFIG_XXX)与#ifdef CONFIG_XXX的区别
    linux内核睡眠状态解析
    如何在linux中测试i2c slave模式驱动的功能?
    insmod内核模块时提示"unknown symbol ..."如何处理?
    insmod某个内核模块时提示“Failed to find the folder holding the modules”如何处理?
  • 原文地址:https://www.cnblogs.com/carl-angela/p/5413961.html
Copyright © 2020-2023  润新知