Skip to content

Commit 11023b3

Browse files
authored
Merge pull request Asabeneh#560 from Taki-Ta/chinese-version
feature: add 06_tuples chinese translation
2 parents a09e192 + 3883f3e commit 11023b3

File tree

3 files changed

+263
-4
lines changed

3 files changed

+263
-4
lines changed

Chinese/05_lists.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
</div>
1616

17-
[<< 第四天](./04_strings.md) | [第六天 >>](../06_Day_Tuples/06_tuples.md)
17+
[<< 第四天](./04_strings.md) | [第六天 >>](./06_tuples.md)
1818

1919
![30DaysOfPython](../images/30DaysOfPython_banner3@2x.png)
2020

@@ -593,4 +593,4 @@ ages = [19, 22, 19, 24, 20, 25, 26, 24, 25, 24]
593593

594594
🎉 恭喜 ! 🎉
595595

596-
[<< 第四天](./04_strings.md) | [第六天 >>](../06_Day_Tuples/06_tuples.md)
596+
[<< 第四天](./04_strings.md) | [第六天 >>](./06_tuples.md)

Chinese/06_tuples.md

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
<div align="center">
2+
<h1> 30 天 Python:第六天 - Tuples</h1>
3+
<a class="header-badge" target="_blank" href="https://www.linkedin.com/in/asabeneh/">
4+
<img src="https://img.shields.io/badge/style--5eba00.svg?label=LinkedIn&logo=linkedin&style=social">
5+
</a>
6+
<a class="header-badge" target="_blank" href="https://twitter.com/Asabeneh">
7+
<img alt="Twitter Follow" src="https://img.shields.io/twitter/follow/asabeneh?style=social">
8+
</a>
9+
10+
<sub>Author:
11+
<a href="https://www.linkedin.com/in/asabeneh/" target="_blank">Asabeneh Yetayeh</a><br>
12+
<small> 第二版:2021 年 7 月</small>
13+
</sub>
14+
15+
</div>
16+
17+
[<< 第五天](./05_lists.md) | [第七天 >>](./07_sets.md)
18+
19+
![30DaysOfPython](../images/30DaysOfPython_banner3@2x.png)
20+
21+
- [第六天:](#第六天)
22+
- [元组](#元组)
23+
- [如何创建元组](#如何创建元组)
24+
- [元组长度](#元组长度)
25+
- [获取元组项](#获取元组项)
26+
- [元组切片](#元组切片)
27+
- [将元组更改为列表](#将元组更改为列表)
28+
- [检索元组中的项](#检索元组中的项)
29+
- [连接元组](#连接元组)
30+
- [删除元组](#删除元组)
31+
- [💻 练习 - 第六天](#-练习---第六天)
32+
- [练习: 1级](#练习-1级)
33+
- [练习: 2级](#练习-2级)
34+
35+
# 第六天:
36+
37+
## 元组
38+
39+
元组是有序且不可变的不同数据类型的集合。一旦创建了元组,我们就无法更改其值。我们不能在元组中使用 add、insert、remove 方法,因为它是不可修改的(不可变的)。与列表不同,元组的方法很少。与元组相关的方法有:
40+
41+
- tuple():创建一个空元组
42+
- count():计算元组中指定项的数量
43+
- index():查找元组中指定项的索引
44+
- + 运算符:连接两个或多个元组并创建一个新元组
45+
46+
### 如何创建元组
47+
48+
- 创建一个空元组
49+
50+
```py
51+
# 语法
52+
empty_tuple = ()
53+
# 或使用元组构造函数
54+
empty_tuple = tuple()
55+
```
56+
57+
- 创建一个具有初始值的元组
58+
59+
```py
60+
# 语法
61+
tpl = ('item1', 'item2','item3')
62+
```
63+
64+
```py
65+
fruits = ('banana', 'orange', 'mango', 'lemon')
66+
```
67+
68+
69+
### 元组长度
70+
71+
我们使用 _len()_ 方法来获取元组的长度。
72+
73+
```py
74+
# 语法
75+
tpl = ('item1', 'item2', 'item3')
76+
len(tpl)
77+
```
78+
79+
### 获取元组项
80+
81+
82+
- 正索引
83+
与列表数据类型类似,我们使用正索引或负索引来访问元组项。
84+
![Accessing tuple items](../images/tuples_index.png)
85+
86+
```py
87+
# 语法
88+
tpl = ('item1', 'item2', 'item3')
89+
first_item = tpl[0]
90+
second_item = tpl[1]
91+
```
92+
93+
```py
94+
fruits = ('banana', 'orange', 'mango', 'lemon')
95+
first_fruit = fruits[0]
96+
second_fruit = fruits[1]
97+
last_index =len(fruits) - 1
98+
last_fruit = fruits[las_index]
99+
```
100+
101+
- 负索引
102+
负索引是从末尾开始的,-1 表示最后一项,-2 表示倒数第二项,列表/元组长度的负数表示第一项。
103+
![Tuple Negative indexing](../images/tuple_negative_indexing.png)
104+
105+
```py
106+
# 语法
107+
tpl = ('item1', 'item2', 'item3','item4')
108+
first_item = tpl[-4]
109+
second_item = tpl[-3]
110+
```
111+
112+
```py
113+
fruits = ('banana', 'orange', 'mango', 'lemon')
114+
first_fruit = fruits[-4]
115+
second_fruit = fruits[-3]
116+
last_fruit = fruits[-1]
117+
```
118+
119+
### 元组切片
120+
121+
我们可以通过指定开始和结束的索引范围来切出子元组,返回值是一个包含指定项的新元组。
122+
123+
- 正索引范围
124+
125+
```py
126+
# 语法
127+
tpl = ('item1', 'item2', 'item3','item4')
128+
all_items = tpl[0:4] # 所有项
129+
all_items = tpl[0:] # 所有项
130+
middle_two_items = tpl[1:3] # 不包括索引 3 的项
131+
```
132+
133+
```py
134+
fruits = ('banana', 'orange', 'mango', 'lemon')
135+
all_fruits = fruits[0:4] # 所有项
136+
all_fruits= fruits[0:] # 所有项
137+
orange_mango = fruits[1:3] # 不包括索引 3 的项
138+
orange_to_the_rest = fruits[1:]
139+
```
140+
141+
- 负索引范围
142+
143+
```py
144+
# 语法
145+
tpl = ('item1', 'item2', 'item3','item4')
146+
all_items = tpl[-4:] # 所有项
147+
middle_two_items = tpl[-3:-1] # 不包括索引 3 的项
148+
```
149+
150+
```py
151+
152+
fruits = ('banana', 'orange', 'mango', 'lemon')
153+
all_fruits = fruits[-4:] # 所有项
154+
orange_mango = fruits[-3:-1] # 不包括索引 3 的项
155+
orange_to_the_rest = fruits[-3:]
156+
```
157+
158+
### 将元组更改为列表
159+
160+
我们可以将元组更改为列表,将列表更改为元组。如果我们想修改元组,我们应该将其更改为列表。
161+
162+
```py
163+
# 语法
164+
tpl = ('item1', 'item2', 'item3','item4')
165+
lst = list(tpl)
166+
```
167+
168+
```py
169+
fruits = ('banana', 'orange', 'mango', 'lemon')
170+
fruits = list(fruits)
171+
fruits[0] = 'apple'
172+
print(fruits) # ['apple', 'orange', 'mango', 'lemon']
173+
fruits = tuple(fruits)
174+
print(fruits) # ('apple', 'orange', 'mango', 'lemon')
175+
```
176+
177+
### 检索元组中的项
178+
179+
我们可以使用 _in_ 检查元组中是否存在某个项,它返回一个布尔值。
180+
181+
```py
182+
# 语法
183+
tpl = ('item1', 'item2', 'item3','item4')
184+
'item2' in tpl # True
185+
```
186+
187+
```py
188+
fruits = ('banana', 'orange', 'mango', 'lemon')
189+
print('orange' in fruits) # True
190+
print('apple' in fruits) # False
191+
fruits[0] = 'apple' # TypeError: 'tuple' object does not support item assignment
192+
```
193+
194+
195+
196+
### 连接元组
197+
198+
我们可以使用 + 运算符连接两个或多个元组
199+
200+
```py
201+
# 语法
202+
tpl1 = ('item1', 'item2', 'item3')
203+
tpl2 = ('item4', 'item5','item6')
204+
tpl3 = tpl1 + tpl2
205+
```
206+
207+
```py
208+
fruits = ('banana', 'orange', 'mango', 'lemon')
209+
vegetables = ('Tomato', 'Potato', 'Cabbage','Onion', 'Carrot')
210+
fruits_and_vegetables = fruits + vegetables
211+
```
212+
213+
### 删除元组
214+
215+
不能删除元组中的单个项,但可以使用 _del_ 删除元组本身。
216+
217+
```py
218+
# 语法
219+
tpl1 = ('item1', 'item2', 'item3')
220+
del tpl1
221+
222+
```
223+
224+
```py
225+
fruits = ('banana', 'orange', 'mango', 'lemon')
226+
del fruits
227+
```
228+
229+
230+
🌕 你太勇敢了,你做到了。你刚刚完成了第 6 天的挑战,你已向着伟大的目标迈出了 6 步。现在做一些练习来锻练你的大脑和肌肉。
231+
232+
## 💻 练习 - 第六天
233+
234+
### 练习: 1级
235+
236+
1. 创建一个空元组
237+
1. 创建一个包含你姐妹和兄弟名字的元组(虚构的兄弟姐妹也可以)
238+
1. 连接兄弟姐妹元组并将其分配给 siblings
239+
1. 你有多少兄弟姐妹?
240+
1. 修改兄弟姐妹元组并添加你父母的名字,然后将其分配给 family_members
241+
242+
### 练习: 2级
243+
244+
1. 从 family_members 中获取兄弟姐妹和父母
245+
1. 创建 fruits、vegetables 和 animal products 元组。连接三个元组并将其分配给名为 food_stuff_tp 的变量。
246+
1. 将 food_stuff_tp 元组更改为 food_stuff_lt 列表
247+
1. 从 food_stuff_tp 元组或 food_stuff_lt 列表中切出中间项或项。
248+
1. 从 food_staff_lt 列表中切出前三项和最后三项
249+
1. 完全删除 food_staff_tp 元组
250+
1. 检查元组中是否存在项:
251+
- 检查 'Estonia' 是否在 nordic_country 元组中
252+
- 检查 'Iceland' 是否在 nordic_country 元组中
253+
254+
```py
255+
nordic_countries = ('Denmark', 'Finland','Iceland', 'Norway', 'Sweden')
256+
```
257+
258+
259+
[<< 第五天](./05_lists.md) | [第七天 >>](./07_sets.md)

Chinese/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
| 03 | [运算符](./03_operators.md) |
88
| 04 | [字符串](./04_strings.md) |
99
| 05 | [列表](./05_lists.md) |
10-
| 06 | [元组](./06_Day_Tuples/06_tuples.md) |
11-
| 07 | [集合](./07_Day_Sets/07_sets.md) |
10+
| 06 | [元组](./06_tuples.md) |
11+
| 07 | [集合](./07_sets.md) |
1212
| 08 | [字典](./08_Day_Dictionaries/08_dictionaries.md) |
1313
| 09 | [条件](./09_Day_Conditionals/09_conditionals.md) |
1414
| 10 | [循环](./10_Day_Loops/10_loops.md) |

0 commit comments

Comments
 (0)