当前位置:网站首页>组合数据类型之元组小练习
组合数据类型之元组小练习
2022-06-29 02:12:00 【长街395】
描述
这是一个关于"组合数据类型之元组"的小练习,覆盖元组类型的基本、操作符、函数和方法等的使用,包含两部分内容:跟随练习 和 小测验。
在"跟随练习"环节,请使用IDLE编辑器的交互模式,逐一输入以下语句,观察并比较输出结果。
在"小测验"环节,请在本题目对应的"提交代码"页面按要求输入代码,"保存并提交评判"后查看结果。
跟随练习
请使用IDLE编辑器的交互模式(含有>>>提示符),按顺序逐一输入以下语句(共20个):
>>>t = 1, 2, 3, 2, 1
>>>max(t)
3
>>>t.index(1)
0
>>>t.count(1)
2
>>>s = (1, "a", 2, "b")
>>>min(s)
Traceback (most recent call last):
File "<pyshell#49>", line 1, in <module>
min(s)
TypeError: unorderable types: str() < int()
>>>t + s
(1, 2, 3, 2, 1, 1, 'a', 2, 'b')
>>>print(t, s)
(1, 2, 3, 2, 1) (1, 'a', 2, 'b')
>>>t2 = tuple(set(t))
>>>print(t, t2)
(1, 2, 3, 2, 1) (1, 2, 3)
>>>len(t)
5
>>>t > t2
True
>>>t3 = t2*3
>>>print(t2, t3)
(1, 2, 3) (1, 2, 3, 1, 2, 3, 1, 2, 3)
>>>t3[::3]
(1, 1, 1)
>>>t3[::-1]
(3, 2, 1, 3, 2, 1, 3, 2, 1)
>>>t2 in t3
False
>>>2, 3 in t3
(2, True)
>>>p = 1, 2, [1,2]
(1, 2, [1, 2])
>>> p[-1]
[1, 2]小测验
请将如下代码粘贴至"提交代码"页面,"保存并提交评判"后查看结果。注意,此时代码不是交互模式,而是文件模式。
t = 1, 2, 3, [1, 2, 3]
try:
t[-1].extend([4, 5, 6])
print(t)
except:
print("error")在定义元组时,列表可以作为其元素,实际上是列表的指针是其元素。所以,元组要求其元素不能修改,只要列表指针不修改即可。因此,这里见到了一种"奇怪"的现象,元组元素的"值"可以被修改,请大家合理理解这个现象。
边栏推荐
- Google Borg paper
- [redis] data introduction & General Command & string type
- [redis] set type
- Blog publishing test 1
- A full screen gesture adaptation scheme
- Fundamentals of scala (3): operators and process control
- How to become a senior digital IC Design Engineer (6-7) digital IC Verification: debug skills
- Why install an SSL certificate on a web site?
- 如何成为一名高级数字 IC 设计工程师(4-2)脚本篇:Verilog HDL 代码实现的文件读写操作
- Ambiguity between 0 and 1
猜你喜欢

SystemVerilog-结构体(一)

chrome浏览器关闭更新弹窗

What is the Valentine's Day gift given by the operator to the product?

如何成为一名高级数字 IC 设计工程师(4-2)脚本篇:Verilog HDL 代码实现的文件读写操作

【Redis】数据介绍 & 通用命令 & String类型

Finally got the byte offer. The 25-year-old inexperienced experience in software testing is written to you who are still confused
![[redis] list type](/img/c7/adec2aaea50d69a0aed95bfe683b9e.png)
[redis] list type

Koa 快速入门

Analysis of advantages and disadvantages of environment encryption and transparent encryption

How to become a senior digital IC Design Engineer (4-3)
随机推荐
KOA Quick Start
Junior final exam
7-29 delete substring in string (non KMP)
How to encrypt the device
How to become a senior digital IC Design Engineer (1-1) Verilog coding Grammar: Introduction
2022.02.15
项目研发,有哪些好用的免费脑图工具软件
同花顺究竟是如何开户?在线开户是安全么?
MySQL详解 --- 聚合与分组
Kubernetes: container resource requirements and constraints (constraints)
[redis] get to know redis for the first time
Pyinstaller打包pikepdf失败的问题排查
Is the ETF fund reliable and safe
OculusRiftS与Unity.UI的交互(1)-总览
如何成为一名高级数字 IC 设计工程师(4-3)脚本篇:C 语言实现的文件读写操作
Who do you want to know when opening a stock account? Is it safe to open an account online now?
Scala 基础 (三):运算符和流程控制
B1006 output integer in another format
[redis] list type
网上联系客户经理办理炒股开户安全吗?