Functions Basics------Learning Python

1. Function(or called procedure or subroutines) is a device that groups a set of statements so they can be run more than once in a program-a packaged procedure invoked by name.


2. Functions are the alternative to programming by cutting and pasting-rather than having mulitple redundant copies of an operation's code,we can factor it into a single function. 


3. Functions are also the most basic program structure Python provides for maximizing code reuse, and lead us to the larger notions of program design. As we’ll see, functions let us split complex systems into manageable parts. By implementing each part as a function, we make it both reusable and easier to code.


4. Two ways to make functions (def and lambda)

   Two ways to manage scope visibility (global and nonlocal)

  Two ways to send results back to callers (return and yield)


5. Coding Function:


1) def is executable code.


2) def creates an object and assigns it to a name

(When Python reaches and runs a def statement, it generates a new function object and assigns it to the function’s name. the function object can be assigned to other names, stored in a list, and so on.
Function objects may also have arbitrary user-defined attributes attached to them to record data)


othername = func # Assign function object

othername() # Call func again


3) lambda creates an object but returns it as a result(it allows in-line function definitions in places where a def statement won' work syntactically. cause def is a statement ,lambda is an expression. e.g. def cannot be used in a list comprehension)


4) yield sends a result object back to the caller, but remembers where it left off.


5) global declares module-level variables that are to be assigned


6) nonlocal declares enclosing function variables that are to be assigned(This allows enclosing functions to serve as a place to retain state—information remembered between function calls—without using shared global names)


7) arguments are passed by assignment( object reference)


8) arguments are passed by position, unless you say otherwise.


9) Arguments, return values, and variables are not declared


6. return : return statement can show up anywhere in a function body. when reached, it ends the function call and sends a result back to the caller. if return is not present, it returns None automatically. 


7. def is much like = statement: it simply assigns a name at runtime. 

defs are not evaluated until they are reached and run, and the code inside defs is not evaluated until the functions later called. 


8. there are two sides to the function picture: a definition(the def that creates a function) and a call(an expression that tells Python to run the function's body)


9. what the function means and does depends on what we pass into it (Polymorphism).

Ex 1:

def times(x, y): # Create and assign function

... return x * y

this can work for both number and string or mix them.

def intersect(seq1, seq2):

res = [] # Start empty

for x in seq1: # Scan seq1

if x in seq2: # Common item?

res.append(x) # Add to end

return res

this can work on mulitple data types,like list, tuples ,dictionary,...



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值