Python function of the log

Source: Internet
Author: User

Python functions of the log

First, the Cognition function

1. What is a function

Functions, function, colloquially, functions, functions are used to encapsulate a particular function, for example, in Python, Len () is a function, and Len () is a function that can return the length of a string, so Len () His particular function is to return the length, for example, we can define a function, then write the function, and then call this function when it is used. So the function is divided into two types, one is the system comes with without our function, such as Len () This function, another is we define, we need to write its function, this function of high degree of freedom, called the custom function, need to use the time to call the function directly.


2. Function functions

Different functions for different functions, convenient and quick

#实例:

#系统自带的函数

#1. The ability to take string lengths

A= "Hellomyteacher" Print Len (a)

#输出结果:

14


#2. Implementing a cut of a string

A= "Student" B=a.split ("U") Print B

#输出结果:

[' St ', ' Dent ']

#注意, the split result does not include the


#3. A custom function

Def a (): print "Hello";p rint 777 print "a" a ()

#输出结果:

hello777a


Definition of function in 3.Python

To use a custom function in Python, you have to define a function that defines the meaning of a function that consists of two parts, the first of which is to affirm that the part of the formulation is a function, not another object, and the second meaning is to define the functions that the function contains, that is, the function of the functions to be written.

#函数的定义

#格式

‘‘‘

def function name (): #括号不可缺, and colon:, indicates the end of the

function content;

‘‘‘

#实例

Def function1 (): A=8 print a


Second, formal parameters and arguments

1. The concept of parameters in functions

function is to implement one or more functions, we know that the function Len () is the length of the string, but spit out only Len (), he is meaningless, after all, we do not take the string length of the target, if we want to make him meaningful, we must put a string into this function, For example, we want to take the length of the string "admin", we will put "admin" into Len () This function, it becomes len ("admin"), so that our Len () function has practical significance, at this time, we put the "admin" in parentheses called the parameters of the function, in fact, A parameter is the data that the function needs to use to perform its function.

#参数的概念

Print Len ()

#输出结果:

Traceback

Typeerror:len () takes exactly one argument (0 given)

A= "Admin" Print Len (a)

#输出结果:

5


2. What is a formal parameter

Formal parameters generally occur in the process of function definition, formal parameters usually refers to the name of the parameter, but not the value of the parameter, he is only the formal parameter, only a function, which has the name of which parameter is the parameter only.

#什么是形参

def function1 (b): If A>b:print a else:print b


3. What is an argument

Arguments are exactly complementary to formal parameters, and arguments are generally present in function calls, and arguments generally refer to parameter specific values rather than formal

#什么是实参

Def function1 (A, B):

If a>b:

Print a

Else

Before print b# is a formal parameter


Function1 (1,3) #现在是实参


Difference:

The argument is a specific value, and the parameter is just the name of the argument, indicating where

When an argument is not the same as the formal parameter, the argument appears in the call, and the parameter appears in the definition


4. Transfer of parameters

In Python, the arguments are passed in order during the invocation of the function.

#参数的传递

#第一种, the simplest pass, the argument is passed to the formal parameter

def function (A, B): If A>b:print "before this number is greater than the number" Else:print "after this number is larger" function (7,8) #7->a,8-> ; b

#输出结果:

This number is bigger than the back.

#第二种, Assignment delivery

def function (a,b=8): Print a print bfunction (1) function


5. Key parameters

When multiple parameters are present in a function in Python, we can assign values directly to our parameters by their names, so these parameters are called key parameters.

#关键参数

def function (a=1,b=6,c=7): #初始化 print a print b print cfunction (5)

#输出结果: passed to first parameter a by default

567

function (b=7,a=8)

#输出结果:

877

function (5,c=2,b=3)

#输出结果

523

function (b=4,c=2,a=1)

#输出结果:

142

"But be aware that arguments cannot conflict '"

function (b=2,c=3,2)

#输出结果:

Hint program has Eroor


Third, global variables and local variables

1. What is a scope

A variable in Python is its function within a certain range, which we call a scope in the scope of its function.

#作用域

def func ():

I=8

Print I

#输出结果:

Hint I is not defined, I scope is within the function, so there is no point in print execution function, that I no longer the scope of print

Print J

J=9

#输出结果:

Hint J is undefined, the scope of J is after the assignment, and the previous print is not included in the scope

J=9print J

#输出结果:

9


2. Local Variables

In Python, where scopes are scoped rather than global, we are called local variables, and in a function, if we do not declare a global variable, then the local variable is the default.

#局部变量

Def Func2 (a): I=7 print Ii=9func2 (i)

#输出结果: I is a local variable

7

Print I

#输出结果: 9 is an argument to a local variable call

9


3. Global variables

In Python, the scope of these variables is global, that is, in the program all the place, then we need to make a global declaration of this variable, after the declaration of the variable becomes a global variable

#全局变量

Def func3 (): Global I #i是全局变量, globally declared i=7 print ii=9func3 ()

#输出结果:

7

Because the i=7 inside is the value that the function has always given, the external i=9 is not assigned.


I=9

Print I

#输出结果: There is no calling function func3

9


Iv. use of functions and return values

1. Invocation of functions

In the run program is, to execute a function, you need to call the function, you want to call a function, after the function definition, directly input once the function name, if you want to pass the argument into the functions inside the execution, directly to call the parentheses inside the input arguments. such as a function Def func3 (): So defined, then we call it directly input func3 (parameters) can be. Where parameters can be ignored

Def a ():

I=1

A ()


2. return value of function

There are functions in Python that have return values, some function that have no return value, and a function that returns a value, we can return a value, or let the function return multiple values.

#函数返回值

#函数返回值是通过return语句实现的

#返回一个值的情况

def test (): i=7 return iprint test ()

Return I to the function test ()

#输出结果:

7


#多个返回值的情况

def test2 (i,j): K=i*j return (i,j,k) x=test2 (4,5) print X

#输出结果:

(4,5,20)
def test2 (i,j): K=i*j return (i,j,k) y,z,m=test2 (4,5) print Y

#输出结果:

4


V. Document string

1. What is a document string

As a language, Python can define a lot of functions, and when the number of functions increases, it becomes confusing for developers to encounter too many functions without deep understanding of the program, so we need to address this problem because of the increasing number of sizes.

The first way: Write a document description for each function at the time of development

The second way: Add a piece of descriptive text at the beginning of each function, so that we become a document string, so that the program becomes clear



2. Document string usage

#文档字符串

def d (i,j): ' This function implements a multiplication operation. The function returns the result of a multiplication operation. "' #文档字符串, followed by a colon, with" ' three quotation marks, the first letter must be capitalized, the sentence must have a sentence #\ k=i*j return kprint d.__doc__# print the document string content Help (d) #输出d的字符信息, including text File strings and Function definitions


This article is from the "8626774" blog, please be sure to keep this source http://8636774.blog.51cto.com/8626774/1676847

Python function of the log

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.