Home > Backend Development > Python Tutorial > Python zero-based introduction to six python functions

Python zero-based introduction to six python functions

黄舟
Release: 2017-01-16 14:07:28
Original
1303 people have browsed it

Today I mainly learned the relevant knowledge of Python functions. The definition of Python functions is very different from the definitions of language functions that I have learned before. Let’s go directly to the topic.
1. Definition of function
The definition of function uses the keyword def, the specific syntax:
def function name (parameter 1, parameter 2,...):
The function to be implemented
2. Function call
Function name (parameter 1, parameter 2,...)
3. Function parameters
This point of Python is basically the same as other languages. The parameters can be divided into formal parameters and actual parameters. , ()(1) Keyword parameters
Keyword parameters are to define parameters, so as to avoid error results due to errors in passing parameters.
(2)Default parameters: Functions that define default parameters, It is to output the default parameters if no parameters are given, and output the parameters if there are parameters.
(3) Collect parameters: use when you don’t know how many parameters there are

3. Example code
(1) Parameterless function

def MyFirstFunction():
     print("这是我创建的第一个函数")
     print("我的心情是很激动的")
     print("\n")
MyFirstFunction()
Copy after login

Python zero-based introduction to six python functions

(2) Function with parameters

def MySecondFunction(name):
    print("我的名字是"+name)
MySecondFunction("YaoXiangxi")
print("\n")def add(num1,num2):
    return (num1+num2)
print(add(3,4))
print("\n")
Copy after login

Python zero-based introduction to six python functions

(3) Keyword parameters

def saySomething(name,word):
     print(name+"->"+word)
saySomething("小甲鱼","让编程改变世界")#万一函数的参数传递顺序错误,则导致结果的输出错误print("\n")
saySomething("让编程改变世界","小甲鱼")
print("\n")#可以使用关键字参数避免上述问题saySomething(word="让编程改变世界",name="小甲鱼")
print("\n")
Copy after login

Python zero-based introduction to six python functions

(4 )Default parameters

def saysomething(name="小甲鱼",word="让编程改变世界"):
     print(name+"->"+word)
saysomething()
saysomething("YaoXiangxi")
saysomething(word="编程让生活更加美好")
print("\n")
Copy after login

Python zero-based introduction to six python functions

(5)Collect parameters

def test(* parans):
     #又get了print函数的新技能,若打印的数据类型不冲突
     #可以添加用逗号隔开继续打印,相当于打印一个元组
     print("参数的长度是:",len(parans))
     print("第二个参数是:",parans[1])
test(1,2,3,4,5,6)
Copy after login

Python zero-based introduction to six python functions

The above are the six python functions for getting started with Python. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template