程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Definition of Python function

編輯:Python

About bloggers : Former Internet manufacturer tencent staff , Network security giant Venustech staff , Alibaba cloud development community expert blogger , WeChat official account java Quality creators of basic notes ,csdn High quality creative bloggers , Entrepreneur , Knowledge sharers , Welcome to your attention , give the thumbs-up , Collection .

Catalog

      • One 、 background
      • Two 、 Definition of function
      • 3、 ... and 、 Reference resources
      • Four 、 summary


One 、 background

In the actual development process , You will often encounter many identical or very similar operations , At this time , Code that implements similar operations can be encapsulated as functions , Then call the function where you need it . This can not only realize code reuse , It can also make the code more organized , Increase code reliability . Now let's introduce python Definition of .


Two 、 Definition of function

  • A function is a segment of 、 Reusable code snippets , It can improve the modularity of program and the reuse rate of code .
  • Python Provides a lot of built-in functions ( Such as print()、input()、int() Functions, etc )
  • Standard library functions ( Such as math In the library sqrt() function )
  • Users can also write their own functions , It's called a custom function .

stay Python in , The general form of defining a function is :

  • Use def Keyword to define the function
  • The function name can be any valid Python identifier
  • In parentheses after the function name is a list of formal parameters ( It's called formal parameter for short ), The formal parameter list is the value passed to the function when it is called , There can be zero 、 One or more , When multiple parameters are passed, they are separated by commas .
  • The function body is the code executed each time a function is called , Consisting of one or more lines of statements .
def Function name ([ List of formal parameters ]):
The body of the function

Be careful :
(1) Even if the function doesn't need to take any arguments , You also have to keep a pair of empty parentheses .
(2) The colon after the bracket cannot be omitted .
(3) The function body is relative to def Keywords must keep a certain space indentation .
for example : Definition is named “print_text()” Function of , The function is output “Hello world!”

def print_text():
print("Hello world!")


3、 ... and 、 Reference resources

1、 Liao Xuefeng's official website
2、python Official website
3、Python Programming case tutorial


Four 、 summary

The above is about Python Definition of , You can refer to it , If you think it's good , Welcome to thumb up 、 Collection 、 Looking at , Welcome to wechat search java Basic notes , Relevant knowledge will be continuously updated later , Make progress together .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved