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

Python Notes-function

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Function definition

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

Any passed parameters and arguments must be placed in the middle of parentheses. Parentheses can be used to define parameters.

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

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.

Def functionname (parameters):

"function _ document string"

Function_suite

Return [expression]

Function call: write the function name (parameter) directly

Def printtest (str):

Print (str)

Return

Printtest ('aa')

Modifiable (mutable) and immutable (immutable) objects

Changeable: lists and dictionaries

Immutable: strings, tuples, and numbers are immutable objects

Everything in python is an object. Strictly speaking, we can't say value passing or reference passing, we should say that passing immutable objects and passing mutable objects

Pass an immutable object instance

#! / usr/bin/python

Def ChangeInt (a):

A = 10

B = 2

ChangeInt (b)

Print b # result is 2

Pass a variable object instance

#! / usr/bin/python

Def changeme (mylist):

"modify incoming list"

Mylist.append ([1, 2, 3, 4])

Print "value within the function:", mylist

Return

Mylist = [10, 10, 20, 30]

Changeme (mylist)

Print "function external value:", mylist

Output result

Value in function: [10, 20, 30, [1, 2, 3, 4]]

Function external value: [10, 20, 30, [1, 2, 3, 4]]

Parameters:

Required parameters: the required parameters must be passed into the function in the correct order. The number of calls must be the same as when declared.

To call the printme () function, you must pass in a parameter, or there will be a syntax error

Keyword parameter

#! / usr/bin/python

# writable function description

Def printme (str):

"print any incoming strings"

Print str

Return

# call printme function

Printme (str = "My string")

Default parameter

When a function is called, the value of the default parameter is considered to be the default if it is not passed in

#! / usr/bin/python

# writable function description

Def printinfo (name, age = 35):

"print any incoming strings"

Print "Name:", name

Print "Age", age

Return

# call printinfo function

Printinfo (age=50, name= "miki")

Printinfo (name= "miki")

Variable length parameter

You may need a function that can handle more parameters than when it was originally declared. These parameters are called indefinite length parameters, and unlike the above two parameters, they are not named when declared.

#! / usr/bin/python

Def printinfo (arg1, * vartuple):

"print any incoming parameters"

Print "output:"

Print arg1

For var in vartuple:

Print var

Return

Printinfo (10)

Printinfo (70,60,50)

Return statement

The return statement [expression] exits the function and optionally returns an expression to the caller. Return statements with no parameter values return None

Def sum (arg1, arg2):

Total = arg1 + arg2

Print "within function:", total

Return total

Total = sum (10,20)

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

Internet Technology

Wechat

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

12
Report