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

The scope of Python function and the detailed explanation of built-in function

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "the scope of Python function and the detailed explanation of built-in function". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow Xiaobian's train of thought to study and learn "the scope of Python function and the detailed explanation of built-in function".

Catalogue

1. The scope of a function

two。 Detailed explanation of function call

3. Built-in function

1. The scope of a function

-built-in

-- overall situation, write at the top

-- Local, inside the function

A = 34 # global variable def run (): B = 44 # local variable print (a) print (b) # cannot call local variable directly and report an error

Running result:

C = 12def run (a): print (a) b = 33run (5) # one global variable, c # two local variables, the parameters of the function are also local variables.

Running result:

five

Local VS global

-- Local space (within the function) to get global variables, Yes

-- all space (outside the function) gets the local variable, No, but it can be obtained indirectly by returning the value return

-- modify local variables in global space (outside the function), No

-- modify all variables in the local space (within the function), Yes, but you must declare the global global variable name

Siwei = 99def run (): print ('function execution') print (siwei) # calls global variables. You can use run () normally.

Running result:

Function execution

ninety-nine

Siwei = 99def run (): print ('function execution') a = 88 print (siwei) # calls the global variable. You can normally use return a # to return the value to the global variable resultresult = run () print (result)

Running result:

Function execution

ninety-nine

eighty-eight

Siwei = 99def run (): print ('function execution') a = 88 print (siwei) # calls the global variable. You can normally use return a # to return the value to the global variable resultresult = run () print (result)

Running result:

one hundred

two。 Detailed explanation of function call

Note:

-- the function must be defined before calling

-- the order of different function definitions is independent.

-- the function itself can be called in the function body, but it is easy to make mistakes if it is not used in this way.

# call directly before defining a function, so you will get an error offer ('lili',20,' spicy gluten') def eat (name,food): print ('{} favorite {} '.format (name,food)) def offer (name,money,food): print (' Congratulations {} get {} k offer'.format (name,money)) eat (name,food))

Running result:

# one function is the def eat (name,food) that can call another function: print ('{} favorite {} 'format (name,food)) def offer (name,money,food): print (' Congratulations {} get {} k offer'.format (name,money)) eat (name,food) offer ('lili',20,' spicy gluten'))

Running result:

Congratulations to lili on getting 20k offer.

Lili likes spicy gluten best.

# def offer (name,money,food): print ('Congratulations {} get {} k offer'.format (name,money)) eat (name,food) def eat (name,food): print (' {} favorite food {} '.format (name,food)) offer (' lili',20,' spicy gluten')

Running result:

Congratulations to lili on getting 20k offer.

Lili likes spicy gluten best.

You can take a detailed look at the running process of the code through the Debug mode

The first line of the offer and eat functions is executed, and the code in the body of the function is not executed

When offer is called, the code automatically finds the offer function, enters the function body, and then executes the eat function

The code automatically finds the eat function and then enters the function body.

# the function itself can call itself, but it will report a recursive error def run (): print ('running!') Run () run ()

Running result:

RecursionError: maximum recursion depth exceeded while calling a Python object

Recursive error: the maximum recursive depth exceeded when calling the Python object

3. Built-in function

Enumrate (): gets the index value and element value of the list

List1 = ['Abercrombie for i in enumerate (list1)] for i in enumerate (list1): # input result is that the data type is a tuple print (I) for index,value in enumerate (list1): # output result: the index value is the int type, and the element value is the data type print (index,value)

Running result:

(0,'a') = > tuple

(1,'b')

(2, 1)

(3, 2)

0 a = > 0 int a str

1 b

2 1 int 1 int

3 2

Eval (): remove quotation marks on both sides of the string

String ='1 + 1'string1 = '7.8 + 4.5'string2 =' (1) print (string,type (string)) # removing quotation marks is equivalent to becoming an arithmetic operation print (eval (string), type (eval (string)) print (eval (string1), type (eval (string1) # removing quotation marks is equivalent to becoming a tuple print (eval (string2), type (eval (string2))

Running result:

1 + 1

two

12.3

(1, 2, 3)

Zip (): used to package iterable objects as parameters into groups by index number

Title = ['id','name','url'] row = [' 1l 'http://www.baidu1.com']# zip iterates over each element, packages it into a group by index number, and then converts it through dict into a dictionary result = dict (zip (title,row)) print (result) # list Tuples can be converted into the dictionary title1 = ('id','name','url') row1 = [' 2 zip zip (title1,row1)) print (result1)

Running result:

{'id':' 1', 'name':' lili', 'url':' http://www.baidu1.com'}

{'id':' 2', 'name':' lili', 'url':' http://www.baidu2.com'}

A = [1meme 2jue 3] b = ('axiao recorder) c') c = 'qaz'result = dict (zip (amemc)) result1 = dict (zip (ameme b)) result2 = dict (zip (bmenc)) print (result) print (result1) print (result2)

Running result:

{1:'Q, 2:'a, 3:'z'}

{1:'a', 2: 'baked, 3:' c'}

{'axiao:' Qothers, 'baked:' asides, 'canals:' z'}

Sum (iterable [, start]): summation, summation type must be a number

Terable-iterable objects, such as: list (list), tuple (tuple), collection (set), dictionary (dict)

Start-specifies the parameter to be added. If this value is not set, the default is 0.

So sum is wrong, sum (must be an iterable object)

List1 = [2je 3jingmei 4] tuple1 = (1Jet 1) dict1= {60.5 print (sum (list1,1) # list 2Jing 3 plus the value of start 1print (sum (tuple1)) # tuple 1Jing 1 plus the default start value 0print (sum (dict1)) # the value added in the dictionary is the key value, if key is not a number, print (sum (set1)) # set must be a layer. Cannot nest other layers

Running result:

ten

three

130.5

six

Max (): find the maximum

Min (): find the minimum

List1 = [print (max (list1)) print (max (3)) print (min (list1)) print (min (4)

Running result:

three

five

one

four

Id (): view memory address

A = 10b = 1 immutable type (string, tuple, etc.), memory address is the same print (id (a)) print (id (b)) # variable type (list, dictionary), memory address is different c = [1jue 2jue 3] d = [1je 2je 3] print (id (c) print (id (d))

Running result:

140716744443840

140716744443840

1975790732416

1975791019584

Thank you for your reading, the above is the content of "the scope of Python function and the detailed explanation of built-in function". After the study of this article, I believe you have a deeper understanding of the scope of Python function and the detailed explanation of built-in function, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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