How to call custom functions in python

Release: 2019-07-08 09:08:16
Original
12731 people have browsed it

How to call custom functions in python

Python calls a custom function:

1. First, you need to customize a function. After defining the function, you can call it directly

Example :

#!/usr/bin/python # -*- coding: UTF-8 -*- # 定义函数 def printme( str ): "打印任何传入的字符串" print str; return; # 调用函数 printme("我要调用用户自定义函数!"); printme("再次调用同一函数");
Copy after login

The output result of the above example:

我要调用用户自定义函数! 再次调用同一函数
Copy after login

You can define a function with the function you want. The following are simple rules:

The function code block ends with def key word, followed by the function identifier name and parentheses ().

Any incoming parameters and independent variables must be placed between parentheses. Parameters can be defined between parentheses.

The first line of a function can optionally use a documentation string - used to store function descriptions.

The function content starts with a colon and is indented.

return [expression] Ends the function and optionally returns a value to the caller. Return without an expression is equivalent to returning None.

Grammar:

def functionname( parameters ): "函数_文档字符串" function_suite return [expression]
Copy after login

For more Python related technical articles, please visit thePython Tutorialcolumn to learn!

The above is the detailed content of How to call custom functions in python. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!