How to use python functions

(*-*)浩
Release: 2019-08-02 09:25:38
Original
3791 people have browsed it

The concept of function, a function is a code set that organizes code blocks with independent functions into a whole to make it have special functions

How to use python functions

The role of the function, use the function It can enhance the reusability of code and improve the efficiency of program writing (recommended learning: Python video tutorial)

The use of functions, functions must be created before they can be used, this process It is called function definition. After the function is created, it can be used. The usage process is called function call

Function definition and call:

>>> def 函数名(形参1,形参2):  # 定义
...     函数体
 
函数名(实参1,实参2)    #调用
Copy after login

The comment of the function is written in Below the function definition, use the """content""" method to place the mouse on the place where the pycharm function is called and press Ctrl to quickly view the annotation content of the function

The scope of the function parameters, defined inside the function They are called local variables. Variables outside the function are called global variables. The scope of local variables is limited to use inside the function.

>>> def test(a, b):
...     print(a, b)
...
>>> test(1, 2)
1 2
>>> print(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
形参是外部是无法被调用的
Copy after login

For more Python-related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of How to use python functions. 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
Popular Tutorials
More>
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!