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
View 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 }}
ljxpython
test_engineer
Repository navigation
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
test_engineer
/
pythoncase
/
103.1.tuple.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
67 lines (60 loc) · 1.92 KB
main
Breadcrumbs
test_engineer
/
pythoncase
/
103.1.tuple.py
Copy path
Top
File metadata and controls
Code
Blame
67 lines (60 loc) · 1.92 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
# Python 元组常见操作
'''
可以把元组简单理解为不可变的列表
使用的场景:
1. 当我们需要存储一些不可变的数据时,就可以使用元组
2. 当我们需要存储一些可变的数据时,就可以使用列表
3. 当我们需要存储一些不可变的数据,又需要存储一些可变的数据时,就可以使用元组和列表的结合
'''
# 元组的创建
tuple1 = ()
tuple2 = tuple()
# 1. 创建元组
# 1.1. 直接使用小括号
tuple1 = (1, 2, 3, 4, 5)
print(f"tuple1的内容为:{tuple1}")
# 1.2. 使用 tuple() 函数
tuple2 = tuple([1, 2, 3, 4, 5])
print(f"tuple2的内容为:{tuple2}")
# 1.3. 使用逗号分隔
tuple3 = 1, 2, 3, 4, 5
print(f"tuple3的内容为:{tuple3}")
# 1.4. 使用 tuple() 函数和 range() 函数
tuple4 = tuple(range(1, 6))
print(f"tuple4的内容为:{tuple4}")
# 1.5. 使用 tuple() 函数和字符串
tuple5 = tuple('hello')
print(f"tuple5的内容为:{tuple5}")
# 2. 元组的访问
# 2.1. 使用索引访问
print(f"tuple1[0]的内容为:{tuple1[0]}")
# 2.2. 使用切片访问
print(f"tuple1[0:3]的内容为:{tuple1[0:3]}")
# 2.3. 使用 for 循环访问
for i in tuple1:
print(i)
# 2.4. 使用 while 循环访问
i = 0
while i < len(tuple1):
print(tuple1[i])
i += 1
# 2.5. 使用 enumerate() 函数访问
for i, v in enumerate(tuple1):
print(i, v)
# 3. 元组的修改
# 3.1. 元组是不可变的,不能修改元组的元素
# 4. 元组的删除
# 4.1. 元组是不可变的,不能删除元组的元素
# 4.2. 可以删除整个元组
# 5. 元组的其他操作
# 5.1. 元组的长度
print(f"tuple1的长度为:{len(tuple1)}")
# 5.2. 元组的拼接
tuple6 = tuple1 + tuple2
print(f"tuple6的内容为:{tuple6}")
# 5.3. 元组的重复
tuple7 = tuple1 * 2
print(f"tuple7的内容为:{tuple7}")
# 5.4. 元组的比较
print(f"tuple1 == tuple2的结果为:{tuple1 == tuple2}")
print(f"tuple1 > tuple2的结果为:{tuple1 > tuple2}")
You can’t perform that action at this time.