Python Base Two, pythonbasetwo

Source: Internet
Author: User

Python Base Two, pythonbasetwo

// Fourth day to study python

24. In python, how to create funcation.

We can use def to define funcation. such:

Def MyFirstFuncation ():

Print ('This is my first funcation ')

MyFirstFuncation ()

-> This is my first funcatio.

If there are no MyFirstFuncation, it will be have an error.

Of course, we can pass a parameter

Def MySecondFuncation (language ):

Print ('I love' + language)

MySecondFuncation ('python ')

-> I love python

If parameter is more, we can use comma divided parameter, such:

Def AddFuncation (num1, num2 ):

Print (num1 + num2)

-> 3

How to use return:

Def SubFuncation (num1, num2 ):

Return (num1-num2)

Print (SubFuncation (6, 3 ))

-> 3

OK, next to study

Def myFuncation (m ):

'Do you know what is funcation'

# This is secret

Print ('My + m + 'roy ')

MyFuncation. _ doc __

-> Do you know what is funcation

Next example:

Def sortFuncation (str1, str2 ):

Print (str1, str2)

SortFuncation ('second', 'first ')

-> Second first

SortFuncation (str2 = 'second', str1 = 'first ')

-> First second

Default funcation:

Def defaultFuncation (name = 'wyg', age = '12 '):

Print (my name is '+ name + 'Age is' + age)

DefaultFuncation ()

-> My name is wyg age is 12

Def textPara (* paras ):
Print ('parameters length is: ', len (paras ))
Print ('The second parameters is: ', paras [1])

TextPara (1, 2, 3, 'roy ')
->

Parameters length is: 4
The second parameters is: 2

Def textPara (* paras, exp ):
Print ('parameters length is: ', len (paras), exp)
Print ('The second parameters is: ', paras [1])

TextPara (1, 2, 3, 'roy ', exp = 6)
->

Parameters length is: 4 6
The second parameters is: 2

// Th day to study python)

25. In python, how to return one or more values, such:

Def back ():

Return [1, 3.14, 'roy ']

Print (back ())

-> [1, 3.14, 'roy ']

 

Def back ():

Return 1, 3. 14. 'roy'

Print (back ())

-> (1, 3.14, 'roy ')

 

26. In python, Local Variable and Global Variable. Look at example,

Def textFuncation (num1, num2 ):

Result = num1 * num2

Return result

Num1_input = float (input ('input num1 :'))

Num2_input = float (input ('input num2 :'))

Res = textFuncation (3, 5)

Print (result is: ', res)

-> Result is 15

Now we analyze which is local variable and which is global variable

Num1, num2, result is local variable

Num1_input, num2_input, res is global variable

But we need to know if we modify global variable in funcation, global variable will not change, it will create a local variable with the same name global name, we can access global variable in funcation, but try not to modify it. if you want to mofify it, we continue to study it.

 

27. In python, if you want to change global variable in funcation, how to achieve it.

Count = 5

Def myFun ():

Global count

Count = 10

Print (count)

MyFun ()

Print (count)

-> 10, 10

If you not to use global, the result is 10, 5

 

28. In python, how to invoke a funcation in another funcation, the follow is most simple example:

Def fun1 ():

Print ('fun1 is using ')

Def fun2 ():

Print ('fun2 is using ')

Fun2 ()

Fun1 ()

->

Fun1 is using

Fun2 is using

Fun2 not invole by other doesn t fun1

 

29. In python, support lambda, such:

Def fun (x ):

Return 2 * x + 1

Fun (5)

-> 11

G = lambda x: 2 * x + 1

G (5)

-> 11

G = lambda x, y: x + y

G (3, 4)

-> 7

 

30. In python, there are two important built-in funcation I will introduce it

First is filter (None or funcation, iterable)

Such:

Temp = filter (None, [3.14, False, True,-4])

->

[1, True, 3.14,-4] # result is true

Def odd (x ):

Return x % 2

Temp = range (10)

Show = filter (odd, temp)

List (show)

->

[1, 3, 5, 7, 9]

<->

List (filter (lambda x: x % 2, range (10 )))

-> [1, 3, 5, 7, 9]

The second is map ()

List (map (lambda x: x * 2, range (5 )))

-> [0, 2, 4, 6, 8]

 

31. In python, there are alse have recursion.

By default, the depth is 100, but you can change it.

Import sys

Sys. setrecursionlimit (1000)

Def recursion ():

Return recursion ()

Recursion ()

Now we can count a simple question:

How to achieve 1*2*3*4*5

Def funcation (n ):

Result = n

For I in range (1, n)

Result * = I

Return result

Print (funcation (5 ))

-> 120

Now we can change a way to achieve it, by recurtion:

Def recurtion (n ):

If n = 1:

Return 1

Else:

Return n * recurtion (n-1)

Print (recurtion (5 ))

-> 120

We can see another question, how to achieve: 1, 1, 2, 3, 5, 8, 13, 21

As you know, the regular is:

F (1) = 1,

F (2) = 1,

F (n) = f (n-1) + f (n-2)

Def funcation (n ):

If n = 1 or n = 2:

Return 1

Else:

Return funcation (n-1) * funcation (n-2)

Print (funcation (6 ))

-> 8

Now we see a classic question hanoi:

Def hanoi :( n, x, y, z ):

If n = 1:

Print (x, '->, z)

Else:

Hanoi (n-1, x, z, y)

Print (x, '->', z)

Hanoi (n-1, y, x, z)

 

 

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.