Pyhton——List,Sorting,Tuples类型

以下是学习goole PYTHON教程过程中的一些笔记。
'# ' 后面的是输出,希望你能从中获取你想要的。

Lists,Sorting,and Tuples
[1, 2, 3]
#[1, 2, 3]

a = [1, 2, 3]
a
#[1, 2, 3]
a = [1, 2, 'aaaa']
a
#[1, 2, 'aaaa']

len(a)
#3

[1, 2, 'aaaa'] + [3, 4]
#[1, 2, 'aaaa', 3, 4]
a = [1, 2, 3]
b = a
#[1, 2, 3]
b
#[1, 2, 3]
a[0]
#1
a[0] = 13
a
#[13, 2, 3]
b
#[13, 2, 3]

b = a[:]
b
#[13, 2, 3]
a == b
#True
 
a[1:3]
#[2, 3]
a[:-1]
#[13, 2]

for num in a: print num
#13
#2
#3
a.apend(4)
a
#[13, 2, 3 , 4]

None
#
a.pop(0)
#13
a
#[2, 3, 4]

del a
a
#error
a = [1, 2, 3]
a[1]
#2
del a[1]
[1, 3]

b = 12
a = b
del a
#12

a = [4, 2, 1, 6]
sorted(a)
#[1, 2, 4, 6]

help(sorted)
#it will show the detail how to user the sorted method

sorted(a, reverse=True)
#[6, 4, 2, 1]

a = ['ccc', 'bb', 'd', 'bb']
sorted(a)
#['aaaa', 'bb', 'ccc', 'd']
sorted(a, key=len)
#['d', 'bb', 'ccc', 'aaaa']
a
#['ccc', 'bb', 'd', 'bb']
a[1] = 'aaaz'
a
#['ccc', 'aaaz', 'd', 'bb']

def Last(s):return s[-1]
#<function Last at 0x011F0E30>
a
#['ccc', 'aaaz', 'd', 'bb']
sorted(a, key=Lost)
#['bb', 'ccc', 'd', 'aaaz']

':'.join(a)
#'cc:aaaz:d:bb'

'\n'.join(a)
#'ccc\naaaz\nd\nbb'

b = ':'.join(a)
b
#'cc:aaaz:d:bb'

':'.split(b)
#[':']
b.split(':')
#['ccc', 'bb', 'd', 'bb']

a
#['ccc', 'bb', 'd', 'bb']

result = []
for s in a: result.append(s)

result
#['ccc', 'bb', 'd', 'bb']

range(20)
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
(1, 2, 3)
#(1, 2, 3)

a = (1, 2, 3)
len(a)
#3
a[0]
#1
a[0] = 13
#Traceback (most recent call last):
# File "<pyshell#34>", line 1, in <module>
#  a[0] = 13
#TypeError: 'tuple' object does not support item assignment

a = [(1, "b"), (2, "a"), (1, "a")]
a
#[(1, "b"), (2, "a"), (1, "a")]
sorted(a)
#[(1, 'a'), (1, 'b'), (2, 'a')]

(x, y) = (1, 2)
#1
y
#2

你可能感兴趣的:(python,object,list,File,sorting,tuples)