Python custom function basics

Python custom function basics#

Overview##

In order to achieve repetitive operations, simplify labor intensity (for laziness)
Source of the tutorial, python learner at station B

Calculating string functions##

# 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}

Concluding remarks##

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

Python custom function basics
Python basics
Python basics 2
Python basics
Python basics 3
Python basics 4
Python basics 5
Python enumerate() function
Python object-oriented basics
Python function buffer
Join function in Python
Python built-in function -compile()
Python function basic learning
Python data analysis-apply function
Basics of Python syntax
Python3 built-in function table.md
Python Print print timer function
Python defines a function method
Python high-order function usage summary!
Python realizes online translation function
Python tornado upload file function
Python magic function eval () learning
Python multi-process and multi-thread basics
How Python implements FTP function
Python implements image stitching function
Python high-order function usage summary!
Python telnet login function implementation code
Python| function using recursion to solve
How Python implements the mail function
Why doesn't Python support function overloading?
Is there function overloading in python
Learning Python third day one-line function
Python function definition and parameter explanation
Python ATM function implementation code example
Python implements AI face change function
Python realizes image recognition car function
Python implements ftp file transfer function
Python realizes udp transmission picture function
How Python implements the timer function
Learn the basics of python interactive mode
How to run id function in python
How to custom catch errors in python
Two days of learning the basics of Python
What is the id function of python
What is an anonymous function in Python
How does python call its own function
How to add custom modules in Python
python3 realizes the function of mask drawing
What is the function body of python
Is there a helper function in python