转载请注明出处: 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!"
- Header: which includes the
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, andsqrt()
is one of those functions. - When it’s simply done with the
import module
andmodule.function()
to access math module in the way below, it’s called a generic import.
- There is a Python module named
- 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
.
- Pulling in just a single function from a module is called a function import, and it’s done with the
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)
- Use the power of
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()
andmin()
.
type()
:
- returns the type of the data it receives as an argument.