python basic function

Keywords: Python Programming less encoding

function

1 ternary operation (trinomial operation)

v = true to the preceding value if condition else false to the following value

Do it first and think about how to simplify it.

2 function

Functional programming:

Essence: Take multiple lines of code elsewhere, name them individually, and then execute them with that name.

Scenario: Repeated execution of code

The code is too much to be partitioned

Basic Format of 2.1 Function

def Function name ():
    #Function content
    pass
#Execution of functions
#Function name ()
#Note: Internal code will never execute if the function is not called

2.2 parameter

def hanshuming(aaa): Formal parameter
    v = (22,3,4,5)
    print(v[aaa])
    pass
hanshu(1)Actual parameters

Example:

info = [11,22,44,555,666,]
def gg(s):
    c = 0
    for i in s:
        c += i
    print(c)
    pass
gg([11,33,44,555,666,7777,])
2.2.1 Position Reference
def hanshuming(a1,a2): Position parameter
    print(a1,a2)
    pass
hanshu(1,2)#Strictly according to the position parameter a1 equals 1, a2 equals 2, can not pass more or less
2.2.2 Keyword Reference
def hanshuming(a1,a2)#Formal parameter
    print(a1,a2)
    pass
hanshu(a1=1,a2=2)Keyword parameter, position can be changed
  • Keyword parameters can be mixed with location parameters (position parameters should be in front, keyword parameters should be in the back, they should be combined with the number of equal parameters)
def hanshuming(a1,a2,a3)#Formal parameter
    print(a1,a2,a3)
    pass
hanshuming(1,2,a3=9)
2.2.3 Default parameter [definition]
def funt(a1,a2=9):#In the example a2=9 is the default parameter
    pass
#a2=9 means that this parameter can be passed or not, and the default is 9.
2.24 Universal Parameters (Dispersion)
def hanshuming(*args)#* Universal parameter
    print(*args)
    pass
hanshuming(1,2,9,33,"eee")#Output (1, 2, 9, 33,"eee"), can accept n parameters

def hanshuming(*args)#* Arbitrary position can be accepted and converted into tuples
    print(*args)
    pass
hanshuming((1,2,9,33,"eee"))#Output ((1, 2, 9, 33,"eee") that can scatter the loop to the * args parameter
hanshuming(*(1,2,9,33,"eee"))#Output (1, 2, 9, 33,"eee"), which can scatter the loop to the * args parameter
*args Keyword parameterization is not supported

def hanshuming(**kwargs)#Can accept any keyword reference and translate into a dictionary
    print(**kwaras)
    pass
print(k=2)#Output {"k":2}
hanshuming (**{1:2,2:3})#Output {1:2,2:3}

#Note that * args**kwargs can be used in combination
def hanshuming(*arge,**kwargs)
     print(*arge,**kwargs)
           pass
print(2,3,4,5,6,7,k=2,r=3,t=5,)

Return value of 2.3 function

def fff(age):
    #A series of codes
    return 9 #Return value is 9, default none is not defined to represent no return value
val = fff("")
#Functions are not executed when return s are encountered during execution
#Return can return any return
#return 5, 8, "asd" returns tuples

Example:

def ll():
    with open("1.txt",mode="r",encoding="utf-8") as f:
        s = []
        e = f.read().split("\n")
        for i in range(len(e)):
            r = e[i].split("|")
            s.append(r)
        return s
    pass

2.4 Scope

  • A py file is a global scope and a function is a local scope

    a = 1
    def s1():#Functions are local domains in the global domain
      x1 = 666
      print(x1)
      print(a)
    print(a)
    s1()
  • Scope Search Data Rules: Priority in their own scope, they did not go to the parent scope to find.

    x = 10
    def func():#If you cannot find the corresponding value of the x parameter in your domain, you will go to the parent directory to find it.
      print(x)
    func()
  • The child scope can only be searched by the parent, and cannot be reassigned to the parent variable by default. But find a variable type that can be modified

  • If you have to reassign global variables, you need to use global

    name = "fmx"
    def ii():
    global name
      name = "alex"
    ii()
    print(name)#Output alex
  • If you want to modify the parent variable, you need to use nonlocal

    name = "fmx"
    def ii():
      name = "alex"
      def jj()
        nonlocal name
        name = "hhh
      print(name)
    ii()
    print(name)#Output hhh,fmx

Posted by SpasePeepole on Wed, 10 Apr 2019 20:36:31 -0700