Think Python:Chapter 3: Functions 的笔记

这篇博客是《Think Python: How to Think Like a Computer Scientist》第三章的笔记,涵盖了函数定义、类型转换、数学函数、新建函数、参数、作用域等内容。通过例子介绍了函数的使用,包括导入模块、函数的有无返回值及调试技巧。
摘要由CSDN通过智能技术生成

目录

这是麻省理工大学(MIT)官方编程教程中Python Tutorial的内容,教材为《Think Python: How to Think Like a Computer Scientist》。这是我的学习笔记,因为水品有限,请大家多多包涵。如果有一起学习的同学可以一起交流。如笔记中错误,请一定要告诉我啊,我肯定及时改正。所有笔记的目录详见:MIT:Python Tutorial目录

这是MIT官方编程教程中Python TutorialFunctions 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值