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!"
- 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 importimport mathprint math.sqrt(25)# Function importfrom math import sqrtprint sqrt(25)# Universal importfrom 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 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()
andmin()
.
type()
:- returns the type of the data it receives as an argument.
External Resources
- Functions in Python
- Functions Can Return Something
- Python Notes (3)
- Python Notes #3 - 1.21.2013
- Python Notes
- Python notes
- Python Notes
- python notes
- Python notes
- Python Notes
- Python Notes
- Python Notes
- python notes
- python-notes
- Python Notes
- Python 3.3 Tutorial Notes - 3:Data Structure
- Python chapter 2&3 learning notes
- Python Cookbook Notes Chapter1
- Python Reviewing Notes
- My python/java notes
- [李景山php] 20170505深入理解PHP内核[读书笔记]--第二章:用户代码执行--生命周期和Zend引擎
- 5.分布式增删改查
- hdoj 5883 The Best Path
- Java泛型_自定义泛型接口
- linux shell基础二之命令行参数
- Python Notes (3)
- 页面置换算法
- JS iterable 学习笔记
- Spring(2)与接口配合使用和获取Bean容器的两种方式(BeanFactory 、ApplicationContext)
- Retinex算法解读
- 前端开发思想及建议--整理
- JavaScript---DOM技术、BOM模型演示、window对象
- 设计模式的七大原则
- 练习