del|append()|insert()|pop()|remove()|sort()|sorted|reverse()|len()|range()|min()|max()|sum()|[:]|区分两种列表复制|

时间:2022-08-17 14:41:53
 fruit = ['apple','banana','peach']
print fruit[0],fruit[-1] fruit_1 =[]
fruit_1.append('orange')
print fruit_1 fruit.insert(1,'orange')
del fruit[2]
print fruit '''
apple peach
['orange']
['apple', 'orange', 'peach']
''' pop_1 = fruit.pop(1)
print pop_1,"\n",fruit '''
orange
['apple', 'peach']
''' apple = 'apple'
fruit.remove(apple)
print fruit,"\n",fruit_1 '''
['peach']
['orange']
''' fruit_2 = ['a','b','c','d','e','f']
fruit = fruit_1 + fruit_2
print fruit '''
['orange', 'a', 'b', 'c', 'd', 'e', 'f']
'''
new_fruit = sorted(fruit,reverse=True)
print fruit,"\n",new_fruit
new_fruit.sort(reverse=True)
print new_fruit
new_fruit.reverse()
print new_fruit '''
['orange', 'a', 'b', 'c', 'd', 'e', 'f']
['orange', 'f', 'e', 'd', 'c', 'b', 'a']
['orange', 'f', 'e', 'd', 'c', 'b', 'a']
['a', 'b', 'c', 'd', 'e', 'f', 'orange'] ''' length = len(new_fruit)
print length # #empty = []
#iprint empty[-1] '''
Traceback (most recent call last):
File "t.py", line 61, in <module>
print empty[-1]
IndexError: list index out of range
''' for i in new_fruit:
print i
print i '''
a
b
c
d
e
f
orange
orange
''' new_list = list(range(1,15,3))
print new_list #[1, 4, 7, 10, 13] max_in_list = max(new_list)
min_in_list = min(new_list)
sum_in_list = sum(new_list)
print max_in_list,"\n",min_in_list,"\n",sum_in_list '''
13
1
35
'''
double=[i**2 for i in range(1,20)]
new_double = double[:]
print new_double
print double[4:] #[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361]
#[25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361] for single in double[5:-1]:
print single '''
36
49
64
81
100
121
144
169
196
225
256
289
324
'''
new_double_2 = double
double.pop(0)
print double
print new_double_2
print new_double '''
[4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361]
[4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361]
''' new_double[1] =2500000000000
print new_double #[1, 2500000000000, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361] scale = [1,8]
for number in scale:
print number scale = (1,8)
for number in scale:
print number '''
1
8
1
8
'''

问题:列表可以用[ ]or( )???

 scale = [1,8]
for number in scale:
print number scale = (1,8)
for number in scale:
print number '''
1
8
1
8
'''