Home>Article>Backend Development> How to call custom functions in python

How to call custom functions in python

尚
Original
2019-06-27 17:11:56 12752browse

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("再次调用同一函数");

The output result of the above example:

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

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]

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!

Statement:
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