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

以下哪个函数用于返回数字的绝对值?

  • min()
  • abs() (correct)
  • max()
  • round()

Aiter()函数用于返回异步可迭代对象的同步迭代器。

False (B)

使用哪个内置函数可以获取一个对象的类型?

type()

返回字符串长度的内置函数是 ______。

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

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

<p>abs() = 返回数字的绝对值 len() = 返回对象的长度 str() = 将对象转换为字符串 float() = 将字符串或数字转换为浮点数</p> Signup and view all the answers

以下哪个函数用于过滤序列?

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

Locals() 函数用于返回当前作用域内的所有局部变量和返回值。

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

哪个内置函数可以将对象转换为浮点数?

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

用于迭代可迭代对象的函数是 ______。

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

以下哪个函数不是内置的 Python 函数?

<p>concat() (D)</p> Signup and view all the answers

以下哪个函数用于检查可迭代对象中的所有元素是否为真值?

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

Any() 函数返回 False 如果可迭代对象为空。

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

解释 bin() 函数的作用。

<p>将整数转换为带前缀 '0b' 的二进制数字符串。</p> Signup and view all the answers

Bool() 类返回布尔值,即 True 或 _____ 中的一个。

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

将以下函数与其描述匹配:

<p>all() = 检查所有元素是否为真值 any() = 检查任意元素是否为真值 ascii() = 返回对象的可打印表示形式 bin() = 将整数转换为二进制字符串</p> Signup and view all the answers

Anext() 函数的主要功能是什么?

<p>等待下一个异步迭代器的值 (D)</p> Signup and view all the answers

Breakpoint() 函数用于输出调试信息。

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

在 Python 中,bool() 类是哪个数据类型的子类?

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

要控制是否显示前缀 '0b',可以使用 format() 函数的格式 _____。

<p>'#b'</p> Signup and view all the answers

使用 any() 函数时,如果输入为 [False, False, False],结果将是?

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

在Python中,如何创建一个复数?

<p>通过直接声明为 '1+2j'。 (B), 通过使用 complex() 函数。 (D)</p> Signup and view all the answers

使用 complex('1 + 2j') 会导致 ValueError。

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

如果参数为数字,complex() 函数会进行什么样的转换?

<p>与 int 和 float 类似的数值转换。</p> Signup and view all the answers

如果提供两个参数,complex() 函数返回一个实部为 ___,虚部为 ___ 的复数。

<p>real, imag</p> Signup and view all the answers

哪个函数会尝试返回对象的有效属性列表?

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

Delattr() 函数用于删除指定的属性。

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

如果省略所有参数,complex() 函数返回什么?

<p>0j</p> Signup and view all the answers

对于一个普通的 Python 对象 x , complex(x) 会委托给 x.complex(),如果未定义则回退到 ___。

<p><strong>float</strong>()</p> Signup and view all the answers

将下列函数与其描述进行匹配:

<p>complex() = 创建复数 delattr() = 删除对象属性 dict() = 创建字典 dir() = 获取有效属性列表</p> Signup and view all the answers

当对象是模块对象时,dir() 返回什么内容?

<p>模块的属性名称 (D)</p> Signup and view all the answers

如果未定义 dir(),dir() 将会返回对象的 dict 属性。

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

在 Python 3.8 版本中,如果 complex() 和 float() 均未定义,返回将回退到哪个方法?

<p><strong>index</strong>()</p> Signup and view all the answers

该函数会尝试返回通过 ___ 方法实现的有效属性列表。

<p><strong>dir</strong>()</p> Signup and view all the answers

在创建新的字典时,使用哪个类?

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

Dir() 函数的返回列表是不按字母表排序的。

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

在 Python 中,以下哪种模式用于打开一个文件用于只读?

<p>'r' (D)</p> Signup and view all the answers

使用 'w' 模式打开文件时,文件内容会被保留不变。

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

在 Python 中,哪个模式用于打开文件进行附加写入?

<p>'a'</p> Signup and view all the answers

文本模式的默认打开模式是 _______.

<p>'r'</p> Signup and view all the answers

将以下文件模式与其含义匹配:

<p>'r' = 读取 'w' = 写入并截断 'x' = 排它性创建 'a' = 附加写入</p> Signup and view all the answers

在 Python 文件处理函数中,以下哪个参数用于指定如何处理编码错误?

<p>errors (D)</p> Signup and view all the answers

以二进制模式打开的文件,其返回内容为 str 对象。

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

如果不指定编码,Python 默认使用何种编码格式?

<p>平台相关的编码格式</p> Signup and view all the answers

在文本模式下,返回的文件内容首先使用指定的 _______ 进行解码。

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

在什么情况下,可以将 buffering 参数设置为 0?

<p>只允许在二进制模式下 (C)</p> Signup and view all the answers

对于文本模式下的文件处理,使用 'replace' 错误处理程序时,会生成 ValueError 异常。

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

在输入流的换行符解析中,若 newline 为 None,则会启用什么模式?

<p>通用换行模式</p> Signup and view all the answers

使用 os.open() 函数创建的文件是 _______.

<p>不可继承的</p> Signup and view all the answers

在 Python 中,哪个参数决定如何处理文件的换行符?

<p>newline (D)</p> Signup and view all the answers

将以下错误处理机制与其含义匹配:

<p>'strict' = 引发异常 'ignore' = 忽略错误 'replace' = 用替换标记代替错误 'backslashreplace' = 用反向转义序列替换</p> Signup and view all the answers

以下哪个函数用于确保可迭代对象的长度相同?

<p>itertools.zip_longest() (C)</p> Signup and view all the answers

如果未指定 strict=True 参数,zip() 会抑制所有因可迭代对象长度不同而导致的错误。

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

使用 zip 函数将两个列表合并成元组的格式是什么?

<p>[(1, 4), (2, 5), (3, 6)]</p> Signup and view all the answers

使用 zip() 函数时,如果只有一个可迭代对象,返回的结果是一个 ______ 的迭代器。

<p>一元组</p> Signup and view all the answers

请将以下 Python 导入功能与其描述相匹配:

<p><strong>import</strong>() = 高阶函数用于动态导入模块 importlib.import_module() = 标准的导入模块函数 fromlist = 导入对象或子模块的名称列表 level = 指定是使用绝对还是相对导入</p> Signup and view all the answers

使用 zip(*[iter(s)]*n, strict=True) 的作用是什么?

<p>将列表按长度 n 进行分组 (A)</p> Signup and view all the answers

在 Python 中,level 参数支持负数值以实现相对导入。

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

在模块导入时,locals 参数通常被用来决定什么?

<p>决定如何在包的上下文中解释模块名称。</p> Signup and view all the answers

要将分组后的对象提取并赋值给其对应的名称,应该使用 ______。

<p><strong>import</strong>()</p> Signup and view all the answers

将以下 zip() 方法的特性与其功能匹配:

<p>zip() = 合并多个可迭代对象 zip_longest() = 填充较短的可迭代对象 zip(*args) = 拆解一个列表 strict=True = 确保可迭代对象长度一致</p> Signup and view all the answers

在使用 zip() 函数时,如果没有参数会返回什么?

<p>一个空的迭代器 (A)</p> Signup and view all the answers

Python 中的 zip() 函数可以同时处理任意数量的可迭代对象。

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

使用 zip(*zip(x, y)) 可以达到什么效果?

<p>从元组中解压出原始的列表 x 和 y。</p> Signup and view all the answers

在 Python 3.10 版本中,zip() 函数增加了 ______ 参数。

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

Flashcards are hidden until you start studying

Study Notes

内置函数概述

  • Python 解释器内置多个函数和类型,可以随时使用。
  • 内置函数按字母顺序排列,分为多个类别。

内置函数列表

  • abs():返回数字的绝对值,支持整数、浮点数及实现了 __abs__() 的对象;对复数返回模。
  • aiter(async_iterable):返回异步可迭代对象的异步迭代器,等价于调用 x.__aiter__()
  • all(iterable):若可迭代对象中所有元素为真值则返回 True;否则返回 False。
  • anext(async_iterator):异步版的 next(),用于获取异步迭代器的下一个项目。
  • any(iterable):若可迭代对象有任一元素为真值则返回 True;若为空则返回 False。
  • ascii(object):返回对象的可打印表示,使用转义字符串处理非 ASCII 字符。
  • bin(x):将整数 x 转换为二进制字符串,前面带有 "0b" 前缀。
  • bool(object):返回布尔值 True 或 False,依据标准真值测试。
  • breakpoint():在调用位置进入调试器。
  • complex(real, imag):创建复数,支持实部和虚部参数。
  • delattr(object, name):删除对象指定属性,需保证对象允许此操作。
  • dict():创建新字典对象,支持多种初始化方式,包括映射和可迭代对象。
  • dir(object):返回当前作用域或指定对象的有效属性列表。
  • filter(function, iterable):从可迭代对象中过滤掉不符合条件的元素。
  • float(x):将字符串或数字 x 转换为浮点数。
  • format(value, format_spec):格式化值为特定字符串形式。
  • help(object):提供关于对象的文档信息。
  • id(object):返回对象的唯一标识符。
  • input(prompt):接收用户输入。
  • len(s):返回对象的长度或元素数量。
  • list(iterable):将可迭代对象转换为列表。
  • map(function, iterable):对可迭代对象的每个元素应用函数并返回结果。
  • max(iterable):返回可迭代对象中的最大值。
  • min(iterable):返回可迭代对象中的最小值。
  • next(iterator, default):返回迭代器的下一个项目。
  • print(*objects, sep=' ', end='\n'):输出对象到标准输出。
  • range(start, stop, step):生成一个整数序列。
  • reversed(seq):返回可迭代对象的反向迭代器。
  • sorted(iterable, key=None, reverse=False):返回一个排序后的列表。
  • str(object):返回对象的字符串表示。
  • sum(iterable, start=0):返回可迭代对象中的所有元素之和。
  • type(object):返回对象的类型。
  • zip(*iterables):将多个可迭代对象打包成元组的迭代器。

注意事项

  • 许多函数的行为依赖于对象的类型和实现。
  • 输入和输出操作有特定的模式,需留意编码和换行符设置。### zip() 函数及其用途
  • zip() 函数用于将多个可迭代对象按对应位置进行配对,返回一个元组的迭代器。
  • 如果可迭代对象长度不同,未指定 strict=True 时会抑制错误,可能导致难以发现的潜在问题。
  • 可利用 itertools.zip_longest() 来填充较短的可迭代对象,以确保它们具有相同长度。

使用 zip() 的技巧

  • 仅一个可迭代对象参数时,zip() 返回一个一元组的迭代器;未给参数则返回空的迭代器。
  • 可使用 zip(*[iter(s)]*n, strict=True) 将列表按长度 n 进行分组,返回长度为 n 的块。
  • 利用 zip() 结合 * 运算符,可以拆解一个列表,示例如下:
    • x = [1, 2, 3]
      y = [4, 5, 6]
      list(zip(x, y))  # 输出: [(1, 4), (2, 5), (3, 6)]
      x2, y2 = zip(*zip(x, y))
      x == list(x2) and y == list(y2)  # 输出: True
      

import() 函数详解

  • __import__() 是一高级函数,主要由 import 语句调用,不推荐直接使用。
  • 此函数根据 globalslocals 参数,在包上下文中导入模块 name
  • fromlist 参数指定应从模块中导入的对象或子模块名称。
  • level 参数控制绝对或相对导入,0 表示仅执行绝对导入,正数表示父目录层数。
  • 使用语句 import spam 而不是直接调用 __import__() 可避免一些潜在问题。

注意事项与版本变更

  • 3.10 版本新增 strict 参数以处理可迭代对象长度不一致的情况。
  • 3.3 版本改变了 level 参数的默认值和负数支持情况。
  • 读取文件时需注意换行符,解析器只接受 Unix 格式的行结束符。

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