Python learning Notes (functions)

Source: Internet
Author: User

Laziness is the Fibonacci sequence of virtues

>>> fibs=[0,1]
>>> for I in range (8):
Fibs.append (Fibs[-2]+fibs[-1])

>>> fibs
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Or

fibs=[0,1]
Num=input (' How many Fibonacci numbers does you want? ')
For I in Range (num-2):
Fibs.append (Fibs[-2]+fibs[-1])
Print fibs

Create a function

def hello (name):

Return ' hello ' +name+ '! '

>>> Print Hello (' world ')
helloworld!

def fibs (num):
result=[0,1]
For I in Range (num-2):
Result.append (Result[-2]+result[-1])
return result

-----------------------------

>>> fibs (10)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
>>>

Record function adds a document string to a function

def Square (x):
' Calculates the square of the number x. '
Return x*x

---------------

>>> Square.func_doc
' Calculates the square of the number x. '
>>>

>>> Help (Square)
Help on Function square in module __main__:

Square (x)
Calculates the square of the number X.

>>>

Parameters

>>> def try_to_change (n):
N= ' Mr.gumby '


>>> name= ' mrs.entity '
>>> Try_to_change (name)
>>> Name
' Mrs.entity '

>>> def Change (n):
n[0]= ' Mr.gumby '


>>> names=[' mrs.entity ', ' mrs.thing ']
>>> Change (names)
>>> names
[' Mr.gumby ', ' mrs.thing ']
>>>

Keyword parameters and default values

>>> def hello_1 (greeting,name):
Print '%s,%s '% (greeting,name)


>>> def hello_2 (name,greeting):
print '%s,%s '% (name,greeting)


>>> hello_1 (' Hello ', ' world ')
Hello, world
>>> hello_2 (' Hello ', ' world ')
Hello,world
>>> hello_1 (greeting= ' Hello ', name= ' world ')
Hello, world
>>> hello_2 (greeting= ' Hello ', name= ' world ')
World,hello
>>> def hello_3 (greeting= ' Hello ', name= ' world '):
print '%s,%s '% (greeting,name)


>>> Hello_3 ()
Hello,world
>>> hello_3 (' Greetings ')
Greetings,world
>>> hello_3 (' Greetings ', ' universe ')
Greetings,universe
>>> hello_3 (name= ' Gumby ')
Hello,gumby

>>> def hello_4 (name,greeting= ' Hello ', punctuation= '! '):
print '%s,%s%s '% (greeting,name,punctuation)


>>> hello_4 (' Mars ')
hello,mars!
>>> hello_4 (' Mars ', ' Howdy ')
howdy,mars!
>>> hello_4 (' Mars ', ' Howdy ', ' ... ')
Howdy,mars ...
>>> hello_4 (' Mars ', punctuation= '. ')
Hello,mars.
>>> hello_4 (' Mars ', greeting= ' Top of the Morning to Ya ')
Top of the morning to ya,mars!
>>> Hello_4 () #name也需要赋予默认值

Traceback (most recent):
File "<pyshell#7>", line 1, in <module>
Hello_4 ()
Typeerror:hello_4 () takes at least 1 argument (0 given)
>>>

Practice Using Parameters

>>> def Story (**kwds):
Return ' Once upon a time.there was a '% (job) s called% (name) s. '%kwds

>>> def Power (x,y,*others):
If others:
print ' recived redundant parameters: ', others
Return pow (x, y)

>>> def interval (start,stop=none,step=1):
' imitates range () for step>0 '
If stop is None:
Start , Stop=0,start
result=[]
I=start
while i<stop:
Result.append (i)
I+=step
Return result

>>> Print Story (job= ' King ', Name= ' Gumby ')
Once upon a time.there is a king called Gumby.
>>> Print Story (name= ' Sir Robin ', job= ' Brave Knight ')
Once upon a time.there was a brave knight called Sir Robi N.
>>> params={' job ': ' Language ', ' name ': ' Python '}
>>> print Story (**params)
Once upon a Time.there was a language called Python.
>>> del params[' job ']
>>> pirnt Story (job= ' stroke of genius ', **params)
SyntaxError: Invalid syntax
>>> print story (job= ' stroke of genius ', **params)
Once Upon a time.there is a stroke of geni US called Python.
>>> Power (2,3)
8
>>> Power (3,2)
9
>>> Power (y=3,x=2)
8
>> > params= (5,) * *
>>> Power (*params)
3125
>>> Power ( 3,3, ' Hello,world ')

Recived redundant parameters: (' Hello,world ',)
27
>>> interval (10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> interval (1,5)
[1, 2, 3, 4]
>>> interval (3,12,4)
[3, 7, 11]
>>> Power (*interval (3,7))
Recived Redundant parameters: (5, 6)
81
>>>

Scope

>>> x=1
>>> Scope=vars ()
>>> scope[' x ']
1
>>> scope[' x ']+=1
>>> x
2
>>> def foo ():
X=42


>>> x=1
>>> foo ()
>>> x
1
>>> def output (x):
Print X


>>> x=1
>>> y=2
>>> Output (y)
2
>>> def Combine (parameter):
Print Parameter+external


>>> external= ' Berry '
>>> combine (' shrub ')
Shrubberry
>>>

>>> x=1
>>> def change_global ():
Global X
X=x+1


>>> Change_global ()
>>> x
2
>>>

>>> def multiplier (factor):
def multiplybyfactor (number):
Return Number*factor
Return Multiplybyfactor

>>> Double=multiplier (2)
>>> Double (5)
10
>>> Triple=multiplier (3)
>>> Triple (3)
9
>>> multiplier (5) (4)
20
>>>

Recursive

>>> def factorial (n):
Result=n
For I in Range (1,n):
Result*=i
return result

>>> n=10
>>> factorial (n)
3628800
>>>
>>> def factorial (n):
If n==1:
Return 1
Else
return n factorial (n-1)


>>> factorial (10)
3628800
>>> def Power (x,n):
Result=1
For I in range (n):
Result *=x
Result result

Syntaxerror:invalid syntax
>>>
>>> def Power (x,n):
Result=1
For I in range (n):
Result *=x
return result

>>> Power (2,3)
8
>>> def Power (x,n):
If n==0:
Return 1
Else
Return X*power (x,n-1)

def search (Sequence,number,lower=0,upper=none):
If Upper is None:upper =len (sequence)-1
If Lower==upper:
Assert Number==sequence[upper]
Return Upper
Else
Middle= (Lower+upper)//2
If Number>sequence[middle]:
Return Search (Sequence,number,middle+1,upper)
Else
Return Search (Sequence,number,lower,middle)

-----------------------------------

>>> seq=[34,67,8,123,4,100,95]
>>> Seq.sort ()
>>> seq
[4, 8, 34, 67, 95, 100, 123]
>>> Search (seq,34)
2
>>> Search (seq,100)
5

Python learning Notes (functions)

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.