Python Notes (3)

来源:互联网 发布:模块化数据机房 编辑:程序博客网 时间:2024/06/08 14:33

转载请注明出处: 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 functiondef 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 importimport mathprint math.sqrt(25)# Function importfrom math import sqrtprint sqrt(25)# Universal importfrom 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 moduleeverything = dir(math) # Sets everything to a list of things from mathprint 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

  • Functions in Python
  • Functions Can Return Something
0 0
原创粉丝点击