Article directory
Documentation for the function
1. Basic use
def test(a, b):
"用来完成对2个数求和" # 函数第一行写一个字符串作为函数文档
print("%d"%(a+b))
test(11, 22) # 函数可以正常调用
Output result:
33
help(test) # 使用 help 查看test函数的文档说明
Output result:
Help on function test in module __main__:
test(a, b)
用来完成对2个数求和
2. Advanced usage
def get_info(name: str, age: int):
"""
接收用户的名字和年龄,拼接一个字符串并返回
:param name: 接收一个名字
:param age: 接收用户的年龄,必须是 0-200 间的一个整数
:return: 返回拼接好的字符串
"""
return "我的名字叫 %s,今年是 %d 岁" % (name, age)
get_info("吴彦祖", 19)
get_info(520, 19) # 注意,形参上标注的类型只是提高代码的可读性,并不会限制实参的类型
help(get_info)
Output result:
Function app: printing graphs and math calculations
Target
- Feel the nested call of the function
- Feel the idea of program design, decompose complex problems into simple ones
Think & Realize 1
Write a function to print a horizontal line
Print a horizontal line with a custom number of lines
Reference code 1
# 打印一条横线
def printOneLine():
print("-"*30)
# 打印多条横线
def printNumLine(num):
i=0
# 因为printOneLine函数已经完成了打印横线的功能,
# 只需要多次调用此函数即可
while i<num:
printOneLine()
i+=1
printNumLine(3)
Think & Realize 2
Write a function to find the sum of three numbers
Write a function to find the average of three numbers
Reference code 2
# 求3个数的和
def sum3Number(a,b,c):
return a+b+c # return 的后面可以是数值,也可是一个表达式
# 完成对3个数求平均值
def average3Number(a,b,c):
# 因为sum3Number函数已经完成了3个数的就和,所以只需调用即可
# 即把接收到的3个数,当做实参传递即可
sumResult = sum3Number(a,b,c)
aveResult = sumResult/3.0
return aveResult
# 调用函数,完成对3个数求平均值
result = average3Number(11,2,55)
print("average is %d"%result)
nested function calls
def testB():
print('---- testB start----')
print('这里是testB函数执行的代码...(省略)...')
print('---- testB end----')
def testA():
print('---- testA start----')
testB()
print('---- testA end----')
testA()
Output result:
---- testA start----
---- testB start----
这里是testB函数执行的代码...(省略)...
---- testB end----
---- testA end----
Small summary:
-
A function calls another function, which is the so-called function nested call
-
If another function B is called in function A, then the tasks in function B will be executed before returning to the position where function A was executed last time.
Advanced case
[Python] Python realizes the word guessing game-challenge your intelligence and luck!
[python] Python tkinter library implements GUI program for weight unit converter
[python] Use Selenium to get (2023 Blog Star) entries
Use Tencent Cloud Cloud studio to realize scheduling Baidu AI to realize text recognition
[Xiaobai must see] Python word cloud generator detailed analysis and code implementation
[Xiaobai must see] Python crawls an example of NBA player data
[Must-see for Xiaobai] Sample code for crawling and saving Himalayan audio using Python
[Xiaobai must see] Python crawler data processing and visualization
[Must-see for Xiaobai] Python crawler program to easily obtain hero skin pictures of King of Glory
[Must-see for Xiaobai] Use Python to generate a personalized list Word document
Geospatial data processing and visualization using geopandas and shapely (.shp)
Use selenium to crawl Maoyan movie list data
Detailed explanation of the principle and implementation of image enhancement algorithm Retinex
Getting Started Guide to Crawlers (8): Write weather data crawler programs for visual analysis
Getting started with crawlers (4): The best way to crawl dynamic web pages using Selenium and API
Getting Started Guide to Crawlers (3): Python network requests and common anti-crawler strategies
Getting started with crawlers (2): How to use regular expressions for data extraction and processing
Getting started with reptiles (1): Learn the basics and skills of reptiles
Python object-oriented programming basics and sample code
MySQL database operation guide: learn how to use Python to add, delete, modify and query operations
Python file operation guide: encoding, reading, writing and exception handling
Selenium Automation Toolset - Complete Guide and Tutorials
Python web crawler basics advanced to actual combat tutorial
Pandas data processing and analysis tutorial: from basics to actual combat
Detailed explanation of commonly used data types and related operations in Python
[Latest in 2023] Detailed Explanation of Six Major Schemes to Improve Index of Classification Model