t = (1,)
t
(1,)
type(t)

t = (1,)
t
(1,)
type(t)

# access tuple

print(“The number of seconds since midnight is”, \
(currentTime[0] * 3600 + currentTime[1] * 60 +
currentTime[2]))

[root@python ~]# python fig05_06.py
Enter hour: 3
Enter minute: 4
Enter second: 5
The value of currentTime is: (3, 4, 5)
The number of seconds since midnight is 11045

[root@python ~]# cat fig05_07.py
aString = “abc”
aList = [ 1, 2, 3 ]
aTuple = “a”, “A”, 1

# unpack sequences to variables

print(“Unpacking string…”)
first, second, third = aString
print(“String values:”, first, second, third)
print(“\nUnpacking list…”)
first, second, third = aList
print(“List values:”, first, second, third)
print(“\nUnpacking tuple…”)
first, second, third = aTuple
print(“Tuple values:”, first, second, third)

# swapping two values

x = 3
y = 4
print(“\nBefore swapping: x = %d, y = %d” % (x, y))
x, y = y, x # swap variables
print(“After swapping: x = %d, y = %d” % (x, y))

[root@python ~]# python fig05_07.py
Unpacking string…
String values: a b c

Unpacking list…
List values: 1 2 3

Unpacking tuple…
Tuple values: a A 1

Before swapping: x = 3, y = 4
After swapping: x = 4, y = 3

index(value) # 默认返回元组中第一次遇到value的索引（从左到右）
count(value) # 计算元组中value出现的次数

seq[start:end] => (start:end)

# start为0时可以省略，end为-1时可以省略

lst = list(range(0, 10))
lst
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
lst[2:5:2]
[2, 4]
lst[5:2-1]
[5, 4, 3]

# 当step为负数时，从后往前数，此时start应该小于end，否则返回空列表

lst[::-1] # 反转列表
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
lst[0::2]
[0, 2, 4, 6, 8]
lst[1::2]
[1, 3, 5, 7, 9]

from collections import namedtuple

User = namedtuple(‘User’, [‘name’, ‘age’])
me = User(‘lavenliu’, 23)
print(me)
print(me.name)
print(me.age)
print(me[0])
print(me[1])

# me.name = ‘taoqi’

lavenliu
23
lavenliu
23

#### 每天学一点python---列表、元组、字典、集合

2018-03-14 22:39:32

#### Python3入门基础语法总结

2016-01-26 12:33:03

#### python的列表，元组以及字典的一些内置方法与属性

2017-07-18 09:20:44

#### Python数据结构：序列——元组和列表

2015-08-24 16:35:40

#### Python中数据结构 列表 元组 字典 序列

2010-03-01 23:00:00

#### 史上最全的Python数据结构:列表和元组用法总结

2018-04-03 14:08:02

#### python内置数据类型：列表和元组

2011-11-01 23:16:29

#### python菜鸟教程

2018年04月27日 10.2MB 下载

#### python数据结构之元组,列表,字符串分片

2018-02-06 14:26:06

#### python基础知识——内置数据结构(元组)

2015-03-28 12:34:38