L1 = [1,3,2,6,8,4,3,9,7]
L1.sort()
print(L1)
[1, 2, 3, 3, 4, 6, 7, 8, 9]
L1[3] = 5
print(L1)
[1, 2, 3, 5, 4, 6, 7, 8, 9]
t = (1,2,3,4,5)
t[2] = 8
print(t)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-d3eb5c696ef3> in <module>
1 # 列表可以修改元素值,但元组不行
2 t = (1,2,3,4,5)
----> 3 t[2] = 8
4 print(t)
TypeError: 'tuple' object does not support item assignment
L2 = [1,3,2,6,8,4,3,9,7]
L3 = sorted(L2)
print(L3)
[1, 2, 3, 3, 4, 6, 7, 8, 9]
print(L2)
[1, 3, 2, 6, 8, 4, 3, 9, 7]
L4 = sorted(L2,reverse = True)
print(L4)
[9, 8, 7, 6, 4, 3, 3, 2, 1]
t1 = (1,2,3,4,6,5)
t2 = sorted(t1)
print(t2)
[1, 2, 3, 4, 5, 6]
t3 = sorted(t1,reverse = True)
print(t3)
[6, 5, 4, 3, 2, 1]
t4 = tuple()
t4 = ()
print(t4)
()
t5 = (1,2,3,2,2,2,5,7,2)
print('t5元组中数字2出现的次数为:',t5.count(2))
t5元组中数字2出现的次数为: 5
print('5的索引为:',t5.index(5))
5的索引为: 6
print('2的索引为:',t5.index(2))
2的索引为: 1
t6 = ('ke','he','xl')
T = t5 + t6
print(T)
(1, 2, 3, 2, 2, 2, 5, 7, 2, 'ke', 'he', 'xl')
S = str()
print(S)
st = 'hello word!'
z1 = st.find('he',0,len(st))
print(z1)
0
z2 = st.find('hell',0,len(st))
print(z2)
0
z3 = st.find('heo',0,len(st))
print(z3)
-1
z4 = st.find('he',1,len(st))
print(z4)
-1
z5 = st.find('lo',1,len(st))
print(z5)
3
z6 = st.find('lo',0,len(st))
print(z6)
3
st1 = 'helle work!'
stt =st1.replace('le','lo')
print(stt)
hello work!
print(st1)
helle work!
stt1 = stt.replace('rk','rd')
print(stt1)
hello word!
stt2 = st1.replace('e','o')
print(stt2)
hollo work!
st2 = 'Green'
stp = st + st2
print(stp)
hello word!Green
stp1 = st + ' '+ st2
print(stp1)
hello word! Green
a1 = 'hello'
a2 = 'helle'
a3 = "hello"
print(a1 == a2)
False
print(a1 == a3)
True
print(a1 != a2)
True
|