tup =(" nihao ",“ wohao ",“ dajiahao ") 1.计算元组长度并输出 2.获取元组第2个元素并输出 3.获取元素第2-3个元素并输出 4.使用 while循环遍历输出元组
时间: 2024-05-06 15:17:37 浏览: 181
Python3 元组tuple入门基础
1. 计算元组长度并输出
```python
tup = ("nihao", "wohao", "dajiahao")
print(len(tup)) # 输出3
```
2. 获取元组第2个元素并输出
```python
tup = ("nihao", "wohao", "dajiahao")
print(tup[1]) # 输出wohao
```
3. 获取元素第2-3个元素并输出
```python
tup = ("nihao", "wohao", "dajiahao")
print(tup[1:3]) # 输出('wohao', 'dajiahao')
```
4. 使用 while 循环遍历输出元组
```python
tup = ("nihao", "wohao", "dajiahao")
i = 0
while i < len(tup):
print(tup[i])
i += 1
```
输出:
```
nihao
wohao
dajiahao
```
阅读全文