Python Notes (3) - Functions

转载请注明出处: http://blog.csdn.net/cxsydjn/article/details/71302965

The note covers funtion syntax, importing modules and build-in functions.

Python notes of open courses @Codecademy.

About Functions

If you need reuse a piece of code, just with a few different values. Instead of rewriting the whole code, it’s much cleaner to define a function, which can then be used repeatedly.

Function Syntax

  • Function Junction:

    • Functions are defined with three components:

      • Header: which includes the def keyword, the name of the function, and any parameters the function requires.
      • Comment: An optional comment that explains what the function does.
      • Body: which describes the procedures the function carries out. The body is indented, just like for conditional statements.

        
        # Example of a function
        
        def hello_world():
            """Prints 'Hello World!' to the console."""
            print "Hello World!"
  • Call and Response:

    • function(parameters)
    • return sth
  • Parameters and Arguments:
    • def square(n):
      • n is a parameter of square.
      • A parameter acts as a variable name for a passed in argument.
    • parameter = formal parameter (形参), argument = actual parameter (实参)。
    • A function can require as many parameters as you’d like, but when you call the function, you should generally pass in a matching number of arguments.
  • Functions Calling Functions
    • A function can call another function, in the way mentioned above.

Importing Modules

  • A module is a file that contains definitions — including variables and functions — that you can use once it is imported.
  • Generic Imports: import module
    • There is a Python module named math that includes a number of useful variables and functions, and sqrt() is one of those functions.
    • When it’s simply done with the import module and module.function() to access math module in the way below, it’s called a generic import.
  • Function Imports: from module import function
    • Pulling in just a single function from a module is called a function import, and it’s done with the from keyword.
  • Universal Imports: from module import *

    • Use the power of from module import * to import everything from the math module.
    • However, they fill your program with a ton of variable and function names without the safety of those names still being associated with the module(s) they came from.

      
      # Examples of the above importing ways
      
      
      
      # Generic import
      
      import math
      print math.sqrt(25)
      
      
      # Function import
      
      from math import sqrt
      print sqrt(25)
      
      
      # Universal import
      
      from math import *
      print sqrt(25)
  • Tips:

    • A generic import may be the best way.
    • You may see everything in a module by:

      import math            # Imports the math module
      everything = dir(math) # Sets everything to a list of things from math
      print everything       # Prints them all!

Built-in Functions

  • We may directly use some of the functions that are built in to Python (no modules required!).
  • max():
    • takes any number of arguments and returns the largest one.
  • min():
    • returns the smallest of a given series of arguments.
  • abs():
    • returns the absolute value of the number it takes as an argument—that is, that number’s distance from 0 on an imagined number line.
    • It only takes a single number, unlike max() and min().
  • type():
    • returns the type of the data it receives as an argument.

External Resources

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值