week 1- learn python 4 function

Function Junction

    statement

def hello_world(): # There are no parameters,The header, which includes the def keyword, the name of the function, and any parameters the function requires. 【注意冒号】

    """Prints 'Hello World!' to the console."""#An optional comment that explains what the function does.[加Tab空格]

    print "Hello World!"#The body, which describes the procedures the function carries out. The body is indented, just like conditional statements.


Call and Response

Parameters and Arguments

When defining a function, placeholder variables are called parameters.

When using, or calling, a function, inputs into the function are called arguments.

Functions Calling Functions

def fun_one(n):

  return n * 5

def fun_two(m):

  return fun_one(m) + 7

Generic Imports 引入库

import math

print math.sqrt(25)

This tells Python not only to import math, but to get the sqrt() function from within math. 

Function Imports #只需引用一次,后续使用该function不需要阐明库,比如 from math import sqrt,后续便可使用sqrt

Pulling in just a single function from a module is called a function import, and it’s done with the from keyword:

from module import function

Universal Imports#通用引用,用*取代特定的function,将其他函数也引入

What if we still want all of the variables and functions in a module but don’t want to have to constantly type math.?

Universal import can handle this for you. The syntax for this is:

from module import *

 universal import的缺点

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.

namely, two different functions with the exact same name.

import math # Imports the math module

everything = dir(math) # Sets everything to a list of things from math

print everything # Prints 'em all!

调用函数看该库中所定义的所有函数

On Beyond Strings

max()#“Largest” can have odd definitions here, so it’s best to use max() on integers and floats,

min() # min() then returns the smallest of a given series of arguments.

abs()#绝对值

type()# Finally, the type() function returns the type of the data it receives as an argument.

你可能感兴趣的:(week 1- learn python 4 function)