In [1]: list1 = [1, 'a', 'tan']
In [2]: list1
Out[2]: [1, 'a', 'tan']
In [3]: type(list1)
Out[3]: list
In [4]: len(list1)
Out[4]: 3
In [6]: list1[0] #调取某一个元素
Out[6]: 1
In [7]: list1[1]
Out[7]: 'a'
In [8]: list1[2]
Out[8]: 'tan'
In [9]: list1[-1] #调取倒数第一个元素
Out[9]: 'tan'
In [10]: list1[-2]
Out[10]: 'a'
In [11]: list1[-3]
Out[11]: 1
In [14]: list1.append(2)
In [15]: list1
Out[15]: [1, 'a', 'tan', 2]
In [16]: list1.insert(2, 'b') #插入一个元素
In [17]: b = 'b'
In [18]: list1
Out[18]: [1, 'a', 'b', 'tan', 2]
In [19]: list1.insert(2, b)
In [20]: list1
Out[20]: [1, 'a', 'b', 'b', 'tan', 2]
In [2]: list1 = [1, 'a', 'tan']
In [3]: list1.pop()
Out[3]: 'tan'
In [4]: list1
Out[4]: [1, 'a']
In [5]: list1[1] = 'c'
In [6]: list1
Out[6]: [1, 'c']
In [7]: list2 = ['hello', list1, 123]
In [8]: list2
Out[8]: ['hello', [1, 'c'], 123]
In [10]: list2[1][0]
Out[10]: 1
In [11]: t1 = (1, 'a', 'b', 23)
In [12]: t1
Out[12]: (1, 'a', 'b', 23)
In [13]: type(t1)
Out[13]: tuple
In [14]: t1[0]
Out[14]: 1
In [15]: t1[-1]
Out[15]: 23
In [16]: t2 = ('a',)
In [17]: type(t2)
Out[17]: tuple
In [18]: t2 = ('a')
In [19]: type(t2)
Out[19]: str
In [20]: t2 = (1)
In [21]: type(t2)
Out[21]: int
In [22]: t2 = (1,)
In [23]: type(t2)
Out[23]: tuple
In [24]: t = (1,2,3,4,5,6,7,8,9,10)
In [25]: t[2:5]
Out[25]: (3, 4, 5)
In [26]: t[:5]
Out[26]: (1, 2, 3, 4, 5)
In [27]: t[6:]
Out[27]: (7, 8, 9, 10)
In [28]: t[-1]
Out[28]: 10
In [29]: t[-3:-1]
Out[29]: (8, 9)
In [30]: t[-1:-3]
Out[30]: ()
In [31]: t[-3:]
Out[31]: (8, 9, 10)
In [32]: t[:-5]
Out[32]: (1, 2, 3, 4, 5)
In [1]: t = (1,2,3,4,5,6,7,8,9,10)
In [2]: t[2:10:2]
Out[2]: (3, 5, 7, 9)
In [3]: t[1::2]
Out[3]: (2, 4, 6, 8, 10)
In [4]: t[::3]
Out[4]: (1, 4, 7, 10)
In [5]: t1 = 'abcdefghijllmnopqrstuvwxyz'
In [6]: t1[-10:]
Out[6]: 'qrstuvwxyz'
In [7]: t1
Out[7]: 'abcdefghijllmnopqrstuvwxyz'
In [8]: t1[::-1]
Out[8]: 'zyxwvutsrqponmlljihgfedcba'
[root@learning ~]
a = input("请输入一个数字:")
if int(a)<0:
a=a[1:]
b=-int(a[::-1])
else:
b=int(a[::-1])
print(b)
[root@learning ~]
请输入一个数字:123456
654321
[root@learning ~]
请输入一个数字:-123456
-654321
f = [['banana',5.2],['apple', 9.9], ['boluo', 11.49], ['liulian', 29.9]]
n = len(f)
print('fruit\tprice')
print('-' * 14)
for i in range(n):
print("%s\t%.2f" % (f[i][0],f[i][1]))
fruit price
--------------
banana 5.20
apple 9.90
boluo 11.49
liulian 29.90
In [11]: t = ['abc',123, 'tan', 'linux', 99812, 'python']
In [12]: t = t[::-1]
In [13]: for i in range(len(t)):
...: print(t[i])
...:
python
99812
linux
tan
123
abc