1:创建表格
a=[
['张一',18,30000,'白云'],
['李二',20,40000,'天河'],
['王三',30,50000,'越秀'],
]
for m in range(3):
for n in range(4):
print(a[m][n],end='\t')
print()
元组的学习
1:元组建好以后就不能改。
2:列表里学的加元素,减元素,删除元素,元组都没有使用。
3:而列表里学的索引访问,切片操作,连接操作,成员关系操作,比较运算操作,计数(最大值,最小值,求和)都可以使用在元组里(tuple)
4:先快速的把知识体系给搭建起来,知新而温故。要事为先,不要事事求完美。
5:通过()创建元组。a=(1,),()可以省略。
6:tuple可接收列表,字符串,其他的数据类型,迭代器等生成元组。
7:list()可接收元组,字符串,其他的数据类型,迭代器等生成列表。
8:del(b),元组的删除。
9:tuple与list使用方式基本相同,只是职责不同,一个是生成元组,一个是生成列表。
10:元组不能修改
>>> a=(10,20,30,40)
>>> a[0]
10
>>> a[3]=5
Traceback (most recent call last):
File "<pyshell#34>", line 1, in <module>
a[3]=5
TypeError: 'tuple' object does not support item assignment
11:
>>> #元组的切片
>>> a=tuple('abcdefghijklmnopqrst')
>>> a
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't')
>>> a(::-1)
SyntaxError: invalid syntax
>>> a[::-1]
('t', 's', 'r', 'q', 'p', 'o', 'n', 'm', 'l', 'k', 'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a')
>>> a[3:10:1]
('d', 'e', 'f', 'g', 'h', 'i', 'j')
12:
>>> #元组不能使用外置函数像list.sorted,而是使用外置函数sorted()
>>> a
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't')
>>> a=(1,2,3,4,5,6,0,8,9)
SyntaxError: invalid character '(' (U+FF08)
>>> a=(1,2,3,4,5,6,0,9,8)
>>> sorted(a)
[0, 1, 2, 3, 4, 5, 6, 8, 9]
13:>>> #加法
>>> a=(10,15,20)
>>> b=(30,40)
>>> a+b
(10, 15, 20, 30, 40)
14:其他常用的
>>> len(a)
3
>>> sum(a)
45
>>> Max(a)
Traceback (most recent call last):
File "<pyshell#63>", line 1, in <module>
Max(a)
NameError: name 'Max' is not defined. Did you mean: 'max'?
>>> max(a)
20
>>> min(a)
10
15:#zip的用法(返回一个迭代器,与list一起使用)
>>>
>>> a=[10,20,30,40]
>>> b=[50,60,70,80]
>>> c=[90,100,110,120]
>>> d=zip(a,b,c,d)
Traceback (most recent call last):
File "<pyshell#70>", line 1, in <module>
d=zip(a,b,c,d)
NameError: name 'd' is not defined. Did you mean: 'id'?
>>> c=zip(a,b,c)
>>> c
<zip object at 0x000001206A154300>
>>> d=list(c)
>>> d
[(10, 50, 90), (20, 60, 100), (30, 70, 110), (40, 80, 120)]
16:生成器的使用
s=(x*2 for 2 in range(5))
SyntaxError: cannot assign to literal
s=(x*2 for x in range(5))
>>> s
<generator object <genexpr> at 0x0000012067EFF2A0>
>>> tuple(s)
(0, 2, 4, 6, 8)
>>> s.__next__
<method-wrapper '__next__' of generator object at 0x0000012067EFF2A0>
>>>
>>> s.__next__
<method-wrapper '__next__' of generator object at 0x0000012067EFF2A0>
>>> s.__next__
<method-wrapper '__next__' of generator object at 0x0000012067EFF2A0>
>>> s=(x*3,for x in range(5))
SyntaxError: invalid syntax
>>> s=(x*3,for x in range(5))
SyntaxError: invalid syntax
>>> SyntaxError: invalid syntax
SyntaxError: incomplete input
>>> s=(x*3 for x in range(5))
>>> s
、
<method-wrapper '__next__' of generator object at 0x000001206A343780>
>>> s.__next__()
0
>>> s.__next__()
3
>>> s.__next__()
6
>>> s.__next__()
9
>>> s.__next__()
12
>>> s.__next__()
Traceback (most recent call last):
File "<pyshell#107>", line 1, in <module>
s.__next__()
StopIteration