directory

 

The iterator

function

Parameter passing

Keyword parameter

The default parameters

Indefinite length parameter

Anonymous functions


The iterator

Iterable: Any object that can be directly applied to the for loop is an iterable

(Iterable) : You can use isinstance() to determine whether an object is an Iterable

The types of data that can be directly used in the for loop generally fall into two categories:

  1. Set data types: list, tuple, dict, set, string
  2. Generator Generator: Includes generators and generator functions with yield

Judge the iterator:

# import packages
from collections.abc import可迭代print(isinstance([], Iterable))
print(isinstance((), Iterable))
print(isinstance("", Iterable))
print(isinstance({}, Iterable))
print(isinstance((x for x in range(10)), Iterable))
print(isinstance(3, Iterable))
Copy the code

 

Iterator: Not only on the for loop, it can also be called by the next() function and return the next value until a StopIteration error is raised indicating that the next value cannot be returned

Judgment iterators:

# import packages
from collections.abc import Iterator

print(isinstance([], Iterator))
print(isinstance((), Iterator))
print(isinstance("", Iterator))
print(isinstance({}, Iterator))
print(isinstance((x for x in range(10)), Iterator))
print(isinstance(3, Iterator))
Copy the code

To convert an iterator to an iterator:

a = [1.2.3.4.5]
b = iter(a)
print(next(b))
Copy the code

Enter a few lines of data until you type end, printing out all the previous input

# import packages
from collections.abc import Iterator

end = "end"
str = ""

for line in iter(input,end):
    str += line + "\n"

print(str)
Copy the code

 

function

Format:

Def function name (argument 1, argument 2….. Parameters n)

statements

Return the expression

Def: Function code blocks start with the def keyword

Function names follow the identifier rules

def myprint() :
    a = "boy"
    print(a)

myprint()
Copy the code

def sum(a,b) :
    print(a+b)

sum(2.5)
Copy the code

Parameter passing

Value passing: Passes immutable types

String, the tuple, number the immutable

def sum(n) :
    print(id(n))
    n = 10
    print(n)
    
n = 20
sum(n)
print(n)
Copy the code

 

Pass by reference: The mutable type of pass

List, dict, set the variable

def sum(n) :
    n[0] = 10
    print(n)

n = [1.2.3.4]
sum(n)
Copy the code

 

Keyword parameter

Allows functions to be called in a different order than when defined

def sum(n,m) :
    print(n,m)

sum(m=10,n=20)
Copy the code

 

The default parameters

When a parameter is called, if no parameter is passed, the default parameter is used

If you want to use default parameters, put the default parameters last

def sum(n='aaa',m='ccc') :
    print(n,m)

sum(m=10,n=20)
Copy the code

def sum(n='aaa',m='ccc') :
    print(n,m)

sum(a)Copy the code

 

Indefinite length parameter

Can handle more parameters than when defined

* can hold all unnamed variable arguments, or an empty tuple if no argument is specified

def sum(*arr) :
    print(arr)

sum('aaa'.'bbb'.'ccc'.'ddd')
Copy the code

def sum(*arr) :
    print(arr)

sum(a)Copy the code

Variables with ** generate dictionaries, and ** represents key-value pair arguments

def dict(**arr) :
    print(arr)

dict(a=1,b=2,c=3)
Copy the code

 

Anonymous functions

When we pass in a function, sometimes it’s easier to pass in an anonymous function without having to explicitly define the function. One advantage of using anonymous functions is that they don’t have names, so you don’t have to worry about function name conflicts. In addition, an anonymous function is also a function object. It is also possible to assign an anonymous function to a variable and use the variable to call the function.

Instead of using def to define functions, use lambda to define functions

 

Features:

Lambda is just an expression. The body of the function is simpler than def

2. A lambda body is just an expression, not a code block, that encapsulates only simple logic in a lambda

3. Lambda has its own namespace and cannot access parameters other than free parameters or global namespace parameters

4. Although lambda is an expression and can only be written on one line, unlike inline functions in C/C++. Inline functions are designed to take up less space and increase operating efficiency

 

Format: lambda argument 1, argument 2, argument 3…. Parameter n:expression

sum = lambda n,m,k : n+m-k
print(sum(3.4.5))  # 3 + 4 to 5 = 2
Copy the code