Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the basic functions of Python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge about "Python basic functions". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

Standard function def

You can define a function that you want to function, here are some simple rules:

The function code block begins with the def keyword, followed by the function identifier name and parentheses ().

Any incoming arguments and arguments must be enclosed in parentheses, which can be used to define parameters.

The first line of the function can optionally use a document string-used to store the function description.

Function content starts with a colon and is indented.

return [expression] terminates the function, optionally returning a value to the caller. Return without an expression is equivalent to returning None.

Grammar:

def (arg1, arg2,... argN): return 2. anonymous lambda function

Anonymous means that you no longer define a function in the standard form of a def statement.

lambda is just an expression, and the function body is much simpler than def.

The body of lambda is an expression, not a block of code. Only limited logic can be encapsulated in lambda expressions.

Lambda functions have their own namespace and cannot access parameters outside their own parameter list or in the global namespace.

Although lambda functions appear to be one-line functions, they are not equivalent to inline functions in C or C++, which are designed to increase efficiency by calling small functions without consuming stack memory.

Grammar:

lambda [arg1 [,arg2,..... argn]]:expression

Examples:

#Writeable function description sum = lambda arg1, arg2: arg1 + arg2;#Call sum function print ("sum is: ", sum( 10, 20 ))print ("sum is: ", sum( 20, 20 )) sum is: 30 sum is: 40

The return statement is used to exit a function, optionally returning an expression to the caller. A return statement with no argument value returns None.

Grammar:

return [expression] IV. Scope

The scope of a variable determines which part of the program can access which particular variable name. Python has four scopes:

L (Local) local scope

E (Closure) in functions outside closure functions

G (Global) global scope

B (Built-in) built-in scope

L -> E -> G ->B rule search, that is: if you can't find it locally, you will go to the local outside (such as closure), if you can't find it, you will go to the global search, and then you will go to the internal construction.

x = int(2.9) #built-in scope g_count = 0 #global scope def outer(): o_count = 1 #in functions outside closure functions def inner(): i_count = 2 #local scope

In Python, only modules, classes, and functions (def, lambda) introduce new scopes. Other blocks of code (such as if/elif/else/, try/except, for/while, etc.) do not introduce new scopes. That is to say, variables defined in these statements can also be accessed externally.

Global and local variables

Variables defined inside a function have a local scope, and variables defined outside a function have a global scope.

Local variables can only be accessed inside the function they are declared in, while global variables can be accessed throughout the program. When a function is called, all variable names declared within the function are added to the scope.

global and nonlocal keywords

When an inner scope wants to modify an outer scope variable, the global and nonlocal keywords are used.

global

num = 1def fun1(): global num #requires global keyword declaration print(num) num = 123 print(num)fun1() Output result of the above example: 1123

nonlocal

If you want to modify variables in nested scopes (enclosing scopes, outer non-global scopes), you need the nonlocal keyword.

def outer(): num = 10 def inner(): nonlocal num # nonlocal Keyword Declaration num = 100 print(num) inner() print(num)outer() The output result of the above example: 100100 The content of "What are Python basic functions" is introduced here. Thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report