Python-day3 knowledge points--depth copy, function basic definition, built-in function

Source: Internet
Author: User
Tags abs arithmetic operators

A. Depth copy
1 ImportCopy2 #Shallow Copy3n1={'K1':'Wu','K2': 123,'K3':['Carl', 852]}4N2=N15n3=copy.copy (N1)6 Print(ID (N1))7 Print(ID (n2))8 Print(ID (n3))9 Print(ID (n1['K3']))Ten Print(ID (n3['K3'])) One #Deep Copy An4=copy.deepcopy (N1) - Print(ID (n4)) - Print(ID (n1['K3'])) the Print(ID (n4['K3']))
return value:

10787656
10787656
11532848
20277688
20277688
11455064
20277688
20276328

Two. Basic definitions of functions

1. Default parameters:
1 defMail ():2 defFunc (name, age = 18):3 Print"%s:%s"%(name,age)4 #Specifying Parameters5Func'Wupeiqi', 19)6 #using default Parameters7Func'Alex')
2. Dynamic parameter sequence:
1 def func (*args):2     print  args3#  execution mode one 4 func (11,33,4,4454,5)5#  execution mode two 6 li = [ 11,2,2,3,3,4,54]7 func (*li
3. Dynamic parameter dictionary:
1 defFunc (* *Kwargs):2     Printargs3 #execution Mode one4Func (name='Wupeiqi', age=18)5 #mode of execution two6Li = {'name':'Wupeiqi', Age:18,'Gender':'male'}7Func (**li)
4. Sequence and Dictionary:
def Show (*arg,**Kwargs):print(Arg,type (ARG))print(Kwargs,type ( Kwargs) Show (64,56,99,w=76,p=33)
5. Use dynamic parameters to format strings:
1S1 ="{0} is {1}"2l=['Alex','SB']3Result=s1.format (*l)4 Print(Result)5S1 ="{Name} is {a}"6Result=s1.format (name='Helen', a=19)7 Print(Result)8 9S1 ="{Name} is {a}"Tend={"name":"Helen",'a': 19} One #Result=s1.format (name= ' Helen ', a=19) AResult=s1.format (* *d) - Print(Result)
6.LAMBDA expression:Lambda expression equals simple function expression
1 def func (a): 2     B=a+13return  b4 equals 5 func=Lambda a:a+16 Ret=func (5)7print(ret)

Three. Built-in functions

ABS () absolute value
All () True if the incoming object element is true (not null)Any () One true is trueASCII () When non-ASCII codes are encountered, characters such as \x,\u or \u are output to indicateExample:
1 Print (ASCII (Ten), ASCII (9000000), ASCII ('b\31'), ASCII ('0x\1000'  2 results returned:3   9000000   'b\x19'   '[email protected]'
bin () binary conversionByteArray () string conversion arraycallable () determines whether an object can be calledChr () number to ASCIIOrd () ASCII converts to numbers,Write verification code withcompile () string converted to Python code
1 #!usr/bin/env python2 #Coding:utf-83Namespace = {'name':'Wupeiqi','Data': [18,73,84]}4Code =" "def hellocute (): Return "name%s, age%d"% (Name,data[0],)" "5func = Compile (code,'<string>',"exec")6 execFuncinchnamespace7result = namespace['Hellocute']()8 PrintResult
complex () negative numberdelattr/getattr/setattr/hasattr () for reflectiondictionary () Creating a dictionaryDivmod ()English Description:

The Divmod (A, B) method returns a a//b (division rounding) and a to B remainder

Returns a result type of tuple

Parameters: A, B can be a number (including plural) versions:

It is not allowed to deal with complex numbers before the python2.3 version, so let's pay attention.

English Description:

Take the non complex numbers as arguments and return a pair of numbers consisting of their quotient and remainder when u Sing 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 was (q, a% b), where q is usually Math.floor (A/b) but could be 1 less than that. In any case Q * b + a% B are very close to a, if a% B is non-zero it had 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 code example:
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 () is used to traverse elements in a sequence and their subscriptsmap () The first parameter receives a function name, and the second parameter receives an object that can iteratefilter () Filteringfloat ()format ()Frozenset () Freeze collectionglobals () global variableshash () dictionary key hashesHex () calculates hexlocals () Local variablesMemoryview ()Oct () octal conversionPOW () power operationrange () iteratorround () roundingSlice () slicessorted () sortstr ()sum () sumSuper () executes the parent classdir () returns keyVARs () returns the key value pairzip () List compression

Python-day3 knowledge points--depth copy, function basic definition, built-in function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.