current position:Home>Python notes - Day7 - Fundamentals of functions
Python notes - Day7 - Fundamentals of functions
2022-05-15 06:02:45【Wandering mage 12】
Preface
python Grammar learning , Leave it to those who need it , Understand everything !!
# coding=utf8
# @time:2022/4/6 20:04
# Author Haoyu
# 1. The concept of function
'''''''''
1) Definition ( What is a function ): A function is the encapsulation of code that implements a specific function
( The essence of function is function , Different functions have different functions )
2) Classification of functions :
System function :python Functions that have been defined , Programmers only need to call these functions when they need them ; Columns such as :print、input wait
Custom function : Functions defined by the programmer
'''''''''
# 2. Definition of function
'''''''''
grammar :
def Function name ( Parameter list ):
Function documentation
The body of the function
explain :
1)def - keyword : Fixed writing
2) Function name - The programmer named it himself
All the letters are lower case , Separate words with underscores
See the name and know the meaning ( See the function name and know the function of this function )
Do not use the system function name 、 Class name and module name
3)() - Fixed writing
4) Parameter list - With ’ Variable 1‘,’ Variable 2‘,’ Variable 3‘ There is a form of , You can also have no formal parameters
The pain of formal parameters is to transfer the data outside the function to the function , Do you need formal parameters when defining a function , How many do you need , See whether the function of the function needs to be realized, while the external data needs several
5) Function documentation - Equivalent to the instructions of the machine , Used to help others use the current function more conveniently
6) The body of the function - and def One or more statements that hold an indent
The function body is the code segment that realizes the function of the function ( The core )
'''''''''
'''''''''
# Write a function , Find the sum of two numbers
def sum2(value1,value2):
"""""""""
( Function area description ) Find the sum of two specified numbers
:param value1: The first of the numbers required and
:param value2: The second digit of the required sum
:return:( Return value description )
"""""""""
print(value1+value2)
'''''''''
# practice 1: Write a function to find the factorial of an integer
# N!=1*2*3*....(N-1)*N
def factorial(n):
sum1 = 1
for x in range(1,n+1):
sum1*=x
print(f'{n}! yes :{sum1}')
# practice 2: Write a function , Deletes all characters specified in the specified string ?
def del_char(string,char):
new_str = ''
for x in string:
if x != char:
new_str += x
print(new_str)
# practice 3: Write a function , Count the number of specified values in a dictionary
def value_count(dic,value):
count = 0
for x in dic:
if dic[x] == value:
count += 1
print(count)
# 3. Function call
'''''''''
( important !) When defining a function, the function body is not executed , Only when the function is called will the function body execute !
1) Call function
grammar :
Function name ( Argument list )
explain :
Function name - The name of the function to be used ( The function name must be the function name of the defined function )
Argument list - Multiple data are well separated by ; The specific number depends on the number of formal parameters of the corresponding function
Arguments are the data that is passed to the function through formal parameters when using the function
Be careful : The same function can be called multiple times
2) Function call procedure
First step : Return to the position defined by the function
Second parts : The ginseng ( Assign values to formal parameters with arguments , Ensure that each parameter has a value )
The third step : Execute function body
Step four : After executing the function body, return to the position of the function call, and then execute later
'''''''''
def fun1():
list1=[1,2,3]
print(list1[1])
print('======')
fun1()
factorial(5)
del_char('how are you',' ')
More secure sharing , Please pay attention to 【 Security info】 WeChat official account !
copyright notice
author[Wandering mage 12],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/131/202205110607425015.html
The sidebar is recommended
- Python development alert notification SMS alert
- How to configure Python environment library offline in FME
- Python: fastapi - beginner interface development
- Generate password based on fast token and fast token
- [Django CI system] use of json-20220509
- [Django CI system] if the front-end date is complete, it will be fully updated to the back-end; If the front-end date is incomplete, the date will not be updated to the back-end-20220510
- [Django CI system] echarts dataset standard writing - 20220509
- [Django CI system] obtain the current time, the first day and the last day of the month, etc. - 20220510
- wxPython wx. Correction of font class · Wx Font tutorial
- NCT youth programming proficiency level test python programming level 3 - simulation volume 2 (with answers)
guess what you like
Design of personal simple blog system based on Django (with source code acquisition method)
[Python Script] classify pictures according to their definition
Wu Enda's classic ml class is fully upgraded! Update to Python implementation and add more intuitive visual teaching
Six built-in functions called immortals in Python
Some insights of pandas in machine learning
Introduction to Python [preliminary knowledge] - programming idea
Stay up late to tidy up! Pandas text processing Encyclopedia
Python recursion to find values by dichotomy
Open 3D Python Interface
[true title 02 of Blue Bridge Cup] Python output natural number youth group analysis of true title of Blue Bridge Cup Python national competition
Random recommended
- Introduction to the differences between Python and Java
- Explain Python CONDA in detail
- The pycham downloaded by MAC reports an error as soon as it is opened. The downloaded Python interpreter is also the latest version
- From entry to mastery, python full stack engineers have personally taught Python core technology and practical combat for ten years
- Python is used to detect some problems of word frequency in English text.
- How to choose between excel, database and pandas (Python third-party library)?
- WxPython download has been reporting errors
- Pyside6 UIC and other tools cannot be found in the higher version of pyside6 (QT for Python 6). How to solve it?
- About Python Crawlers
- Successfully imported pandas, unable to use dataframe
- How to extract some keywords in the path with Python
- Python encountered a problem reading the file!
- When Python is packaged into exe, an error is reported when opening assertionerror: C: \ users \ Acer \ appdata \ local \ temp\_ MEI105682\distutils\core. pyc
- Eight practical "no code" features of Python
- Python meets SQL, so a useful Python third-party library appears
- 100 Python algorithm super detailed explanation: a hundred dollars and a hundred chickens
- [fundamentals of Python] Python code and so on
- When Python uses probit regression, the program statement is deleted by mistake, and then it appears_ raise_ linalgerror_ Unrecognized error of singular
- Python testing Nicholas theorem
- Accelerating parallel computing based on python (BL) 136
- Python dynamic programming (knapsack problem and longest common substring)
- Django uses queryset filter save, and an 'queryset' object has no attribute 'Save' error occurs. Solution?
- Analysis of built-in functions in Python learning
- Python office automation - 90 - file automation management - cleaning up duplicate files and batch modifying file names
- Python office automation - 91 - word file Automation - word operation and reading word files
- After python, go also runs smoothly on the browser
- Self taught Python 26 method
- Summary of Python Tkinter component function examples (code + effect picture) (RadioButton | button | entry | menu | text)
- Python implementation of official selection sorting of Luogu question list
- Application of Django template
- Get project root path and other paths in Python project
- Get, rename, and delete file names in Python projects
- How to set the width and height of Python operation table
- Python string preceded by 'f' R 'B' U '
- JSON and other types convert to each other in Python
- Key value of key combination in pynput in Python
- Conversion of Python PDF file to word file
- Interface testing uses Python decorators
- Get the current time in Python
- Python course notes -- Python string, detailed explanation of related functions