Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
learn-python/src/data_types/test_tuples.py at master · arianshi/learn-python · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
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 }}
arianshi
learn-python
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
learn-python
/
src
/
data_types
/
test_tuples.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
89 lines (71 loc) · 4 KB
master
Breadcrumbs
learn-python
/
src
/
data_types
/
test_tuples.py
Copy path
Top
File metadata and controls
Code
Blame
89 lines (71 loc) · 4 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""Tuples.
@see: https://www.w3schools.com/python/python_tuples.asp
@see: https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences
A tuple is a collection which is ordered and unchangeable. In Python tuples are written with
round brackets.
The Tuples have following properties:
- You cannot change values in a tuple.
- You cannot remove items in a tuple.
"""
import
pytest
def
test_tuples
():
"""Tuples"""
fruits_tuple
=
(
"apple"
,
"banana"
,
"cherry"
)
assert
isinstance
(
fruits_tuple
,
tuple
)
assert
fruits_tuple
[
0
]
==
"apple"
assert
fruits_tuple
[
1
]
==
"banana"
assert
fruits_tuple
[
2
]
==
"cherry"
# You cannot change values in a tuple.
# 元组中的值不可更改。
with
pytest
.
raises
(
Exception
):
# pylint: disable=unsupported-assignment-operation
fruits_tuple
[
0
]
=
"pineapple"
# It is also possible to use the tuple() constructor to make a tuple (note the double
# round-brackets).
# The len() function returns the length of the tuple.
# 也可以使用 tuple() 构造函数创建元组(注意双层圆括号)。
# len() 函数返回元组的长度。
fruits_tuple_via_constructor
=
tuple
((
"apple"
,
"banana"
,
"cherry"
))
assert
isinstance
(
fruits_tuple_via_constructor
,
tuple
)
assert
len
(
fruits_tuple_via_constructor
)
==
3
# It is also possible to omit brackets when initializing tuples.
# 初始化元组时也可以省略括号。
another_tuple
=
12345
,
54321
,
'hello!'
assert
another_tuple
==
(
12345
,
54321
,
'hello!'
)
# Tuples may be nested:
# 元组可以嵌套。
nested_tuple
=
another_tuple
, (
1
,
2
,
3
,
4
,
5
)
assert
nested_tuple
==
((
12345
,
54321
,
'hello!'
), (
1
,
2
,
3
,
4
,
5
))
# As you see, on output tuples are always enclosed in parentheses, so that nested tuples are
# interpreted correctly; they may be input with or without surrounding parentheses, although
# often parentheses are necessary anyway (if the tuple is part of a larger expression). It is
# not possible to assign to the individual items of a tuple, however it is possible to create
# tuples which contain mutable objects, such as lists.
# A special problem is the construction of tuples containing 0 or 1 items: the syntax has some
# extra quirks to accommodate these. Empty tuples are constructed by an empty pair of
# parentheses; a tuple with one item is constructed by following a value with a comma (it is
# not sufficient to enclose a single value in parentheses). Ugly, but effective. For example:
# 构造包含 0 或 1 个元素的元组有一些特殊之处:空元组用一对空括号表示;
# 包含一个元素的元组需在值后面加逗号(仅用括号包裹单个值是不够的)。虽然难看,但有效。
empty_tuple
=
()
# pylint: disable=len-as-condition
assert
len
(
empty_tuple
)
==
0
# pylint: disable=trailing-comma-tuple
singleton_tuple
=
'hello'
,
# <-- note trailing comma
assert
len
(
singleton_tuple
)
==
1
assert
singleton_tuple
==
(
'hello'
, )
# The following example is called tuple packing:
# 以下示例称为元组打包。
packed_tuple
=
12345
,
54321
,
'hello!'
# The reverse operation is also possible.
# 反向操作(解包)也是可行的。
first_tuple_number
,
second_tuple_number
,
third_tuple_string
=
packed_tuple
assert
first_tuple_number
==
12345
assert
second_tuple_number
==
54321
assert
third_tuple_string
==
'hello!'
# This is called, appropriately enough, sequence unpacking and works for any sequence on the
# right-hand side. Sequence unpacking requires that there are as many variables on the left
# side of the equals sign as there are elements in the sequence. Note that multiple assignment
# is really just a combination of tuple packing and sequence unpacking.
# 这被称为序列解包,适用于右侧的任意序列。序列解包要求等号左侧的变量数量与序列中的元素数量相同。
# 多重赋值实际上就是元组打包和序列解包的组合。
You can’t perform that action at this time.