1. Formal parameters and arguments
def myfun (x): return x * *3 = 3print(myfun (y))
X is the formal parameter and y is the argument.
2. Function document
A piece of text enclosed in single quotation marks within a function is not output when the function is called, and the writing function document can be used to help others understand it, or it can be viewed or named by the function name. __doc__ Print Functions Document
def myfirstfunction (name): ' The function document is represented in the first part of the function definition with an anonymous string ' Print ('I love you! ')
3. Parameters
1) keyword Parameters
Call the function in order to specify which parameter to call when calling parameters, and to ignore parameters.
def saysome (name, words): Print ' - ' + words' >>> saysome (words=' let programming change the world!) ', name='you')
2) Default parameters
Give the parameter default value, use the default value when no function argument is given
3) Collect parameters: Do not know how many parameters to use to collect parameters, when additional parameters need to use the keyword parameter
def BC (*prama,base=3):' the function implementation will pass in every argument multiplied by 3, you can change the base value by the keyword parameter ' for in prama: print(base*>>> BC (1,2,3,4,5) 3691215>>> BC (1,2,3,4,5,base=5)510152025
4. The number of daffodils
defNarcissus (): foreachinchRange (100, 1000): Temp=Each sum=0 whileTemp:sum= Sum + (temp%10) * * 3Temp= temp//10#Notice that the floor is removed with ifsum = =Each :Print(Each, end='\ t')Print("the number of daffodils is:", end="') Narcissus ()
def NCs (low=0,high=1000): for in Range (low,high): if (each//100) **3+ (EACH%100//10) **3+ (each%10) **3== each: print(each)
5. Write the number of substring lengths with a statistic length of 2 within the target string
deffindStr (Desstr, SUBSTR): Count=0 Length=Len (DESSTR)ifSubStr not inchDesstr:Print('The string was not found in the target string!') Else: forEach1inchRange (length-1): ifDESSTR[EACH1] = =Substr[0]:ifDesstr[each1+1] = = substr[1]: Count+ = 1Print('substring appears%d times in target string'%count) Desstr= Input ('Please enter a target string:') SubStr= Input ('Please enter a substring (two characters):') findStr (Desstr, SUBSTR)
deffindstr (DIS_STR,SUB_STR): Count=0if notSub_strinchDis_str:Print("substring not found in target string") Else: forIinchRange (len (dis_str)-1): ifdis_str[i]+dis_str[i+1]==Sub_str:count+=1Print("substring appears in target string", Count,"Times") Dis_str= Input ("Please enter a target string:") Sub_str= Input ("Please enter a substring (two characters):")
6. Functions and processes
A function has a return value, and the procedure is not a return value.
Strictly speaking Python is a function, there is no process, should be no matter whether the function has a return value Python will return something, no return to return a None object
def Hello (): Print ('Hello! ' )>>> temp = Hello () Hello fishc! >>> tempprint(temp) None
Python can return multiple types of different values
def Mfun (): return ' come on ',' pursue the life you want! >>> Mfun () (' refueling ' Pursuit of the life you want! ')
7. Variables
1) Local variables: variables in the function
2) Global variables: variables outside the function
Note: Modifying a global variable in a function is not valid, and Python creates a new local variable with the same name as the global variable
def Fun (Var): = 1314 print(Var, end="= 520 Fun(var)print(VAR)
var ='Hi'deffun1 ():Globalvar var='Baby' returnFun2 (Var)defFun2 (Var): Var+='I Love You'Fun3 (Var)returnvardefFun3 (Var): Var='Little Angel'Print(FUN1 ()) Baby I Love You>>>var'Baby'
Do not use global variables unless you have to! It's easier to make a bug with more.
8, judge whether it is back to Federation
defpalindrome (String): Length=len (string) last= Length-1length= 2Flag= 1 foreachinchRange (length):ifString[each]! =String[last]: Flag=0 Last-= 1ifFlag = = 1: return1Else: return0string= Input ('Please enter a sentence:')ifPalindrome (String) = = 1: Print('It's back to Federation!')Else: Print('not back to Federation! ')
def elf (x): x=list (x) rex=x[:] rex.reverse () if rex==x: Print (' It's back to Federation! ' Else: print(' not back to Federation! ') x=input (" Please enter a sentence:") Elf (x )
9. Statistics string
defCount (*STRs): #estr every str cstr=0 forEstrinchStrs:cstr+=1Cnum=0 CChar=0 Cempty=0 Celse=0 forEcharinchEstr:ifechar.isdigit (): Cnum+=1elifEchar.isalpha (): CChar+=1elifEchar = =' ': Cempty+=1Else: Celse+=1Print('section%d'% CStr,'strings in total: English letters of%d'%cchar,', number%d'%Cnum,', a space of%d'%cempty,', other characters%d characters'%celse)
defCOUNT (*param): Length=len (param) forIinchRange (length): Letters=0 Space=0 Digit=0 Others=0 foreachinchParam[i]:ifEach.isalpha (): Letters+ = 1elifeach.isdigit (): Digit+ = 1elifeach = =' ': Space+ = 1Else: Others+ = 1Print('The %d strings are in total: The English alphabet%d, the number%d, the space%d, the other characters%d. '% (i+1, letters, digit, space, others))
Functions of Python3 (2)