In order to achieve repetitive operations, simplify labor intensity (for laziness)
Source of the tutorial, python learner at station B
# Realize the function of len function
# len is the length of the returned string
s='sunqi'
length =0for i in s:
length +=1#Equivalent to length=length+1print(length)
5
# Function definition and call
# Encapsulate the above content in a function
# return can return multiple values
def mylen():"""Calculate string length"""
s='sunqi'
length =0for i in s:
length +=1
# return is used to return, assign external variables
return length
# Call functions
mylen()
# The above function implements a simple call, but does not implement parameter input
# Cannot be applied to other strings
# So create a function with parameters
def mylen(s):
length =0for i in s:
length +=1return length
# Call functions
mylen("helloo")
# Pass multiple values
def mymax(x,y):"""Compare the size of two numbers"""
themax=x if x>y else y
return themax
mymax(1,4)
# Points to note
# When the calling parameter is a variable value, the function will save the result of the last run
def mytest(a,l=[]):
# At this time l is a variable list
l.append(a)print(l)mytest(1)
# Call again,Will add the value of the second call
mytest(2)
[1][1,2]
# Dynamic parameters
# * args
def mysum(a,b,c):
thesum=a+b+c
return thesum
mysum(22,33,44)
# The above parameters are fixed, if you change the number of parameters, an error will be reported
# Need dynamic parameters at this time,You can add any number of parameters
def mysum(*args):
thesum=0for i in args:
thesum+=i
return thesum
mysum(22,33,44,33)
# ** kwargs
def info(**kwargs):"""Print information"""print(kwargs)
# transfer
info(name="sunqi",sex="male",age=18)
{' name':'sunqi','sex':'male','age':18}
In today’s statistical software, there are a total of five, stata, R, python, SAS and SPSS. After reading it many times, they are all big wheels, one wheel and another. It’s difficult and annoying. Take a look at these five software. Which one is not an excellent batch? Which one is not a research pillar? They are difficult, my heart is broken, I thought for a long time, I am heartbroken, I am ashamed of myself.
love & peace
Recommended Posts