Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
JavaDevTeam
notes
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
notes
/
Python
/
python-数据类型-Tuple.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
65 lines (47 loc) · 1.72 KB
master
Breadcrumbs
notes
/
Python
/
python-数据类型-Tuple.py
Copy path
Top
File metadata and controls
Code
Blame
65 lines (47 loc) · 1.72 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
tuple
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
# 元组,跟List及其相似
# 一旦初始化就不能修改,没有 append(),insert() 之类的方法
*
但是里面的元素
,
例如
:
数组
,
却是可变的
,
可以对其进行添加
/
删除元素
# 因为它的特性,所以初始化的时候,就要设置值
# 定义
users
=
(
"k"
,
"e"
,
"v"
);
*
直接定义
nums
=
tuple
([
3.61
,
8
,
9
])
*
把List转换为元组
# 可以通过下标来正常的访问
*
跟List的访问规则一样
# 定义空元组
t
=
();
t
=
tuple
()
# 定义只有一个元素的元组
t
=
(
1
);
*
'错误的定义方式'
*
定义的不是tuple
,
是1这个数
!
这是因为括号
()
既可以表示tuple
,
又可以表示数学公式中的小括号
,
这就产生了歧义
*
因此
,
Python规定
,
这种情况下
,
按小括号进行计算
,
计算结果自然是1
。
t
=
(
1
,);
*
只有1个元素的tuple定义时必须加一个逗号
,,
来消除歧义
*
Python在显示只有1个元素的tuple时
,
也会加一个逗号
,,
以免你误解成数学计算意义上的括号
# 使用 + 号组合元组
tup1
=
(
12
,
34.56
);
tup2
=
(
'abc'
,
'xyz'
)
tup3
=
tup1
+
tup2
;
print
(
tup3
)
//
(
12
,
34.56
,
'abc'
,
'xyz'
)
# 使用 * 号来快速复制
users
=
(
"k"
,
"e"
,
"v"
);
print
(
users
*
4
)
//
(
'k'
,
'e'
,
'v'
,
'k'
,
'e'
,
'v'
,
'k'
,
'e'
,
'v'
,
'k'
,
'e'
,
'v'
)
# 删除元组信息
del
users
# 支持另一种访问/截取方法
users
[
0
:]
*
访问从0到最后的元素
users
[
0
:
1
]
*
访问第一个元素
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
tuple
-
api
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
count
(
s
)
# 返回s在当前元组中出现的的次数
index
(
s
)
# 返回s在当前元组中第一次出现的次数,如果不包含s则抛出异常
You can’t perform that action at this time.