Python 函数:定义、调用和数据类型转换

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

如果需要计算多个圆的面积,使用函数的主要优点是什么?

  • 使计算结果更精确。
  • 简化了数学公式的编写。
  • 减少代码量并提高代码的可读性和重用性。 (correct)
  • 减少了 π 的精度误差。

Python中,函数定义必须包含return语句。

False (B)

在Python中,如何调用一个名为calculate_area的函数,并传入半径r作为参数?

calculate_area(r)

在Python中,使用 ______ 关键字来定义一个函数。

<p>def</p> Signup and view all the answers

将以下Python内置函数与其功能匹配:

<p>abs() = 返回数字的绝对值 int() = 将变量转换为整数类型 str() = 将变量转换为字符串类型 bool() = 用于判断真假</p> Signup and view all the answers

若在Python中调用函数时,提供的参数数量与函数定义不符,会发生什么?

<p>Python解释器会抛出<code>TypeError</code>错误。 (D)</p> Signup and view all the answers

Python函数可以返回多个值,这些值会被自动打包成一个列表(list)。

<p>False (B)</p> Signup and view all the answers

如果需要在Python函数中不对任何操作进行,应使用哪个语句作为占位符?

<p>pass</p> Signup and view all the answers

如果想将一个Python函数保存到文件my_module.py中并在另一个文件中使用,需要使用 ______ 语句导入。

<p>import</p> Signup and view all the answers

匹配以下函数定义中参数检查的不同方式:

<p><code>isinstance(x, int)</code> = 检查变量x是否为整数类型 <code>if not isinstance(x, (int, float))</code> = 检查变量x是否不是整数或者浮点类型 <code>raise TypeError('Invalid type')</code> = 如果类型不匹配,手动抛出TypeError异常 <code>pass</code> = 如果类型匹配,则不执行任何操作,继续执行函数</p> Signup and view all the answers

下面哪种方法可以将一个整数转换为十六进制字符串?

<p><code>hex()</code> (A)</p> Signup and view all the answers

在Python中,函数名可以赋值给一个变量,这样变量就成为了该函数的别名,可以像原函数一样调用。

<p>True (A)</p> Signup and view all the answers

在Python中,如何引用math库中的sqrt函数?

<p>math.sqrt()</p> Signup and view all the answers

使用______语句可以从一个模块中导入特定的函数。

<p>from ... import</p> Signup and view all the answers

将下列代码示例与它们的功能对应起来:

<p><code># -*- coding: utf-8 -*-</code> = 声明文件使用UTF-8编码 <code>if __name__ == '__main__':</code> = 判断模块是否作为主程序运行 <code>from module import *</code> = 导入模块中的所有对象(不推荐使用) <code>try: ... except Exception as e:</code> = 捕获并处理代码中的异常</p> Signup and view all the answers

如果一个Python函数没有显式地返回任何值,那么它默认返回什么?

<p>None (C)</p> Signup and view all the answers

在Python中,一个函数可以同时返回一个整数和一个字符串,而且不需要使用任何容器类型。

<p>True (A)</p> Signup and view all the answers

在Python中,如何定义一个空的匿名函数(lambda函数)?

<p>lambda: None</p> Signup and view all the answers

以下代码用于在函数中引发一个类型错误的异常:______ TypeError('Invalid input')

<p>raise</p> Signup and view all the answers

将下列函数调用与其结果相匹配:

<p><code>math.ceil(4.2)</code> = 5 <code>math.floor(4.8)</code> = 4 <code>round(3.14159, 2)</code> = 3.14 <code>abs(-5)</code> = 5</p> Signup and view all the answers

Flashcards

函数调用

用更有意义的函数调用代替重复的代码,如用 s = area_of_circle 代替 s = 3.14 * x * x

抽象

数学中常见的概念,将具体的计算过程隐藏起来,关注结果。 在 Python 中将重复的代码段抽象成一个函数。

调用函数

Python 内置了很多有用的函数,可以直接调用,例如 abs()

Help(abs)

查看函数 abs 的帮助信息

Signup and view all the flashcards

TypeError

Python 中的一个错误类型,表示函数接收到的参数数量或类型不正确。

Signup and view all the flashcards

int() 函数

将其他数据类型转换为整数的函数,例如 int('123')

Signup and view all the flashcards

函数别名

将函数名赋值给一个变量,相当于给函数起了一个别名。

Signup and view all the flashcards

hex() 函数

将一个整数转换成十六进制表示的字符串的 Python 内置函数,例如 hex(255)

Signup and view all the flashcards

def 语句

在 Python 中定义函数要使用的语句,后面依次是函数名、括号、括号中的参数和冒号。

Signup and view all the flashcards

return 语句

用于返回函数的值,函数执行到 return 语句就结束。

Signup and view all the flashcards

return None

表示函数没有返回值。

Signup and view all the flashcards

模块导入

如果函数定义保存在 abstest.py 文件中,可以使用 from abstest import my_abs 导入 my_abs() 函数。

Signup and view all the flashcards

pass 语句

如果想定义一个什么事也不做的空函数,可以使用 pass 语句。

Signup and view all the flashcards

isinstance() 函数

用于检查参数类型,例如 isinstance(x, (int, float))

Signup and view all the flashcards

返回多个值

如果函数需要返回多个值, python实际上返回的是一个tuple

Signup and view all the flashcards

Study Notes

函数

  • 定义:当代码中出现有规律的重复时,应使用函数避免重复,简化代码,方便修改。
  • 作用:函数是一种代码抽象方式,它可以将重复使用的代码封装成一个独立的单元,从而提高代码的重用性和可读性。
  • 抽象: 抽象是一种常见的数学概念,类似于求和符号的使用,可以把复杂的运算过程抽象成一个简单的符号,从而提高代码的可读性和可维护性。
  • 其他语言:大多数高级语言都支持函数,Python 也不例外。Python 不但能非常灵活地定义函数,而且本身內置了很多有用的函数,可以直接调用。

调用函数

  • Python 内置了很多有用的函数,可以直接调用。
  • 调用函数时,需要知道函数的名称和参数。
  • abs()是求绝对值的函数,只有一个参数。
    • 可以通过 help(abs) 查看 abs 函数的帮助信息。
  • 若传入参数数量不对,会报 TypeError 错误,并明确指出应传入的参数数量。
  • 若传入参数类型不对,也会报 TypeError 错误,并给出错误信息。
  • max()函数可以接收任意多个参数,并返回最大的那个。

数据类型转换

  • Python 内置的常用函数还包括数据类型转换函数,如 int() 函数可以将其他数据类型转换为整数。

函数名

  • 函数名是指向函数对象的变量,可以像变量一样赋值和使用。
  • 函数名可以赋值给一个变量,相当于给这个函数起了一个“别名”。

定义函数

  • 在 Python 中,使用 def 语句定义函数,依次写出函数名、括号、括号中的参数和冒号。
  • 在缩进块中编写函数体。
  • 函数的返回值用 return 语句返回。
  • 函数体内部的语句执行时,一旦执行到 return 时,函数就执行完毕,并将结果返回。
  • 如果没有 return 语句,函数执行完毕后也会返回结果,只是结果为 None
  • return None 可以简写为 return
  • 在 Python 交互环境中定义函数时,注意 Python 会出现 ... 的提示。函数定义结束后需要按两次回车重新回到 >>> 提示符下。

空函数

  • 如果想定义一个什么事也不做的空函数,可以用 pass 语句。
  • pass 语句什么都不做,可以用来作为占位符,让代码能运行起来。
  • pass 还可以用在其他语句里,比如 if 语句。
  • 缺少了 pass,代码运行就会有语法错误。

参数检查

  • 调用函数时,如果参数个数不对,Python 解释器会自动检查出来,并抛出 TypeError
  • 如果参数类型不对,Python 解释器就无法帮我们检查,需要自己检查。

返回多个值

  • Python 函数可以返回多个值。
  • 函数返回的仍然是单一值,实际上返回的是一个 tuple
  • 但多个变量可以同时接收一个 tuple,按位置赋给对应的值,写起来更方便。

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser