目录
这是麻省理工大学(MIT)官方编程教程中Python Tutorial的内容,教材为《Think Python: How to Think Like a Computer Scientist》。这是我的学习笔记,因为水品有限,请大家多多包涵。如果有一起学习的同学可以一起交流。如笔记中错误,请一定要告诉我啊,我肯定及时改正。所有笔记的目录详见:MIT:Python Tutorial目录
这是MIT官方编程教程中Python Tutorial中Functions and Scope的内容。本篇博客为《 Think Python: How to Think Like a Computer Scientist》的第3章 Functions 的笔记内容。(Think Python:Chapter 3: Functions)
Python Tutorial:Functions and Scope
READING LIST:
- Think Python, Chapter 3: Functions
- Think Python, Chapter 6: Fruitful Functions, sections 1-4
- 6.01 Python Notes, Section 2: Procedures (PDF)
Think Python, Chapter 3: Functions
3.1 Function calls
function: A named sequence of statements that performs some useful operation. Functions may or may not take arguments and may or may not produce a result.
function definition: A statement that creates a new function, specifying its name, parameters, and the statements it executes.
function call: A statement that executes a function. It consists of the function name followed by an argument list.
return value: The result of a function. If a function call is used as an expression, the return value is the value of the expression.
3.2 Type conversion functions(类型转换函数)
Python provides built-in functions that convert values from one type to another.(for example: int function)
>>> int('32')
32
>>> int('hello')
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
int('hello')
ValueError: invalid literal for int() with base 10: 'hello'
>>> int(2.3)
2
>>> int(-2.3)
-2
3.3 Math functions(数学函数)
Python has a math module that provides most of the familiar mathematical functions. A module is a file that contains a collection of related functions.
Before we can use the module, we have to import it (调用模块,用 import ):
>>> import math
This statement creates a module object named math. If you print the module object, you get some information about it (查看模块或函数信息,用 print ):
>> print(math)
<module 'math' (built-in)>
dot notation: To access one of the functions, you have to specify the name of the module and the name of the function, separated by a