Parisgabriel:python Full Stack engineer (0 basics to Mastery) Tutorial 14th (function Reference)

Source: Internet
Author: User
Tags for in range

ParisgabrielThank you for your support.



                 Stick to a daily subscription every day. Grey often thanks for being a dead powder also wide to

                     Python ai from beginner to proficient

"\ n" Linux line break
"\ r \ n" Windows line break


Parameter passing of the function:
Method of Transmission: (2 kinds)
Positional arguments (including relationships)
Sequence Pass parameter
Keyword arguments (including relationships)
Dictionary key word Transfer
Location parameters:
The corresponding relationship between the actual parameters (arguments) and the formal parameters (parameter) corresponds to the position of an Ann.
Description
Arguments and parameters are passed and matched by location
The number of arguments must be the same as the number of formal parameters
Sequence pass-through parameters:
A sequence parameter refers to the way in which a sequence is passed by position during a function call by using a * to disassemble it.
Description
When a sequence is passed, the position of the sequence disassembly corresponds to the parameter one by one
Position information for the sequence corresponds to the position of the corresponding parameter
Keyword Pass-through:
A keyword parameter is a parameter assignment, an argument, and a parameter are matched by the parameter name by the name of the parameter.
Dictionary keyword parameters:
Refers to a dictionary that uses "* *" to disassemble the dictionary and then use the keyword to pass the argument.
Description
The key name of the dictionary must be the same as the formal parameter name
The key name of the dictionary must be a string and an identifier
Dictionary amount key name to be present in the formal parameter
General parameters:
The method of the function can be arbitrarily combined in the case that the parameter can be uniquely matched to the corresponding argument.
Description
Positional arguments (sequence parameters) must be on the left side of the keyword argument (dictionary keyword pass)

Example:

defFu (A, B, c):Print("a=", a)Print("b=", B)Print("c=", c) Fu (100, *[200, 300]) Fu (*[100, 200], 300) Fu (100, 200, 300) Fu (b=200, c=300) Fu (100, **{"C": 200,"b": 300})

The formal parameter definition of the--------------------function--------------------
Default parameters for the function:
Grammar:
def function name (parameter name 1 = default argument 1, Parameter 2 = default argument 2)
Statement block
Description
The default parameter must be right-to-left, if a parameter has a default parameter,
All parameters on the right must have default parameters
The formal parameter definition of the function:
1. Positional parameters
2. Asterisk tuple parameter
3. Named key glyph parameters
4. Double Star Dictionary parameters
Positional parameters:
Grammar:
def function name (parameter 1, parameter 2 ...). )
Statement block
Tuple parameters:
Grammar:
def function name (* tuple parameter name):
Statement block
Role:
Collect redundant location-pass parameters
Named key glyph Parameters:
Grammar:
def function name (*, name Key glyph parameter):
Statement block
Or
def function name (*args, named key glyph parameter):
Statement block
(Key glyph parameters must be passed with the keyword parameter)
Role:
Force all parameter parameters to be passed with the keyword argument or dictionary keyword
Double Star Dictionary Reference:
Grammar:
def function name (* * Dictionary parameter name):
Statement block
Role:
Collect extra keyword Passes
The dictionary parameter name is usually named: Kwargs
Parameter description of the function:
Positional parameter, asterisk tuple parameter, named key glyph parameter, double star dictionary parameter can be mixed
The order of functions from left to right is:
1. Positional parameters
2. Asterisk tuple parameter
3. Named key glyph parameters
4. Double Star Dictionary parameters
For example:
Def FX (A, B, *args, C, **kwargs):
Pass
FX (+, +, +, c= "C", d= "D")
Variable length parameters of the function:
Asterisk tuple parameter, double star dictionary parameter
Description
can receive arbitrary location parameters and keyword parameters
For example:
DEF fn (*args, **kwargs)
Pass

Global variables and local variables:
Locals: local variable
1. Define a variable within a function as a local variable (the function's formal parameter is also a local variable)
2. Local variables can only be used inside a function
3. Local variables can be created at function call and destroyed after function call
Global variables: Globals variable
Variables inside a module are defined as global variables outside of the function.
Global variables all functions can be accessed directly (but cannot be assigned directly within the function)

Practice:
1. Write a function Minmax (a, B, c) with three parameters, which returns the minimum and maximum values in the three parameters,
requirements, the tuple is formed, the minimum value is in front, the maximum value is after, such as:

Def Minmax (A, B, c):
...

t = Minmax (11, 33, 22)
Print (t) # (one, one) # <<< printing tuples
Xiao, da = Minmax (6, 5, 4)
Print (' min: ', Xiao, ' Max: ', DA)

Answer:

 def   Minmax (A, B, c): x  = a  if  x > b:x = b  if  x >  c:x  = c z  = a  if  z < b:z  = b  if  z < C:z = c L  = [X, z]  print   (L) Minmax ( 2, 1, 3) 

Method Two:
def Minmax (A, B, c):     = [min (A, B, c), Max (A, B, c)]    print(L) Minmax (2, 3, 1)

In fact, the method of two this is also possible, but it is a bit unkind, this does not belong to way merchants

2. Write a function Myadd, this function can calculate two number of the and, can also calculate three number of the and

Def myadd (...):
....
Print (Myadd (10, 20)) # 30
Print (Myadd (100, 200, 300)) # 600

Answer:

def Myadd (A, B, c=0):    return A + B + cprint(Myadd (+)) 
    Print(Myadd (100, 200))
def mysum (*args):    return sum (args)

It's a nice little leather. MySum = Sum

3. Write a function, print_odd, that the function can pass an argument, or pass two arguments, which represents the end value when passing an argument
When passing two arguments, the first argument represents the starting value, and the second argument represents the end value
Print out all the odd numbers in this interval, not including the ending number:

Print_odd (10) # 1 3 5 7 9
Print_odd (11, 20) # 11 13 15 17 19

Answer:

def print_odd (A, b=none):    if b = = None        := a        = 0       for in range (A, b):        if x% 2! = 0            :Print (x, end="") print_odd (1)#  3 5 7 9print_odd (11, 20) #  from

4. Write a function that mysum can pass in any number of arguments, returning all arguments and

def mysum (*args):
...
Print (MySum (1, 2, 3, 4)) # 10
Print (MySum (10, 20, 30)) # 60

Answer:

def mysum (*args):    = 0    for in  args:        + = x     return  iprint#  tenprint#  60 

6. Create a global variable L = []
Write a function:
Def input_number ():
....
Each call to this function will be read from the keyboard with some numbers appended to the list L

such as:
L = []
def input_number ():
....
Input_number ()
Print (L) # [.... These are the number you entered from the keyboard ...]

L = []def  input_number ():    while  True:        = Int (input (" Please input ( -1 over): "               )        if a < 0: Break        L.append (a)     return  = input_number ()print#  [.... These are the number you entered from the keyboard ...]

7. Write a function IsPrime (x) to determine if X is a prime number. Returns true if it is a prime number, otherwise false

Answer:

defIsPrime (x):ifX > 2:         forIinchRange (2, x):ifX% i = =0:s=False Break        Else: S=TrueElse:        if1 < x < 3: S=TrueElse: S=FalsereturnSX= IsPrime (4)Print(x)

8. Write a function prime_m2n (m, n) returns the number of primes starting with M, to the end of n (not containing n), returns a list of these primes and prints
Such as:
L = prime_m2n (5, 10)
Print (L) [5, 7]

Answer:

def prime_m2n (M, N):     = []    for in  Range (M, n):        if isprime (x) = = True:            L.append (x)    return= prime_m2n (5, ten)print(L)

9. Write a function pimes (n) to return a list of all primes less than n and print these primes
Such as:
L = primes (10)
Print (L) # [2, 3, 5, 7]
1) Print all primes within 100
2) Print the total number of primes within 200 and

Answer:

def pinmes (n):     return prime_m2n (1= Pinmes ()print#  [2, 3, 5, 7]Print (Pinmes ())print(sum (pinmes (200)))


10. Write the function fn (n) This function returns the sum of the following series:
fn (n) = 1/(1*2) + 1/(2*3) + ....
+ 1/(n (n-1)) and

Print (FN (100))

Answer:

def FN (n):     = 0    for in range (1, N):        + = 1/(I * (i + 1 )    )return c12> sprint(FN (100))

A wave of experience plus buff: (cough C-Wave cow b)
start a computer, A keyboard level 999,999,999,999,999
You're going to be confused and dizzy and you can't touch the tail. Add an XP buff
because a lot of the jargon is more abstract and the translation is different, so many people know the terminology.
What's called Generally easy to remember and memory of the longest should be in their own way to the reality or some examples of comparison
The more the vernacular the better remember what the gaudy terms know what is OK (remember Say it as much as possible.
Just a corresponding instance relationship in your mind is like a variable in memory .... Off the topic, slipped away)

What are arguments, arguments, and parameters?
Arguments are parameters when a function is called
A pass is a process that passes a parameter to an argument
Parameters are arguments that define (encapsulate) a function
The dictionary keyword is used to split the dictionary into formal parameters and arguments.
Keys represent keywords (formal parameters, immutable)
Values represent parameters (argument, variable)
A key corresponds to a value a keyword corresponds to a parameter
Lists, tuples are all part of the sequential parameter first (*) split after the argument
Keyword arguments must have a keyword (parameter) and parameters (argument)
Collation of the parameters of the pass:
From left to right 1. Positional parameter 2. Asterisk tuple parameter 3. Named key glyph parameter 4. Double Star Dictionary parameter
Variable nothing to say is equivalent to a public phone (global) and private phone (local)
Everyone can use the public phone, your private phone, you will let everyone use it? I don't think so.

Parisgabriel:python Full Stack engineer (0 basics to Mastery) Tutorial 14th (function Reference)

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.