I am a novice to learn Python.

1. Write the code, the following list, each function according to requirements

And outputs the calculated length of the list

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
print(li[0:4])

Results: [ 'alex', 'WuSir', 'ritian', 'barry']

List of lists of additional elements "seven", and add the output

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
li.append("seven")
print(li)

结果:['alex', 'WuSir', 'ritian', 'barry', 'wenzhou', 'seven']


In the first of a list of locations to insert elements "Tony", and outputs the list to add

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
li.insert(0, "Tony")
print(li)

结果:['Tony', 'alex', 'WuSir', 'ritian', 'barry', 'wenzhou']

Please modify the list of elements of the 2nd position as "Kelly", and outputs the modified list

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
li.insert(1, "Kelly")
print(li)

结果:['alex', 'Kelly', 'WuSir', 'ritian', 'barry', 'wenzhou']

Please list l2 = [1, 3,4, "heart" "a",] each li element added to the list, the line of code to achieve, does not allow the cycle is added.

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
l2=[1,"a",3,4,"heart"]
print(li+l2)

结果:['alex', 'WuSir', 'ritian', 'barry', 'wenzhou', 1, 'a', 3, 4, 'heart']

Enclose the string s = "qwert" each li element added to the list, the line of code to achieve, does not allow the cycle is added.

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
li.extend("qwert")
print(li)

结果:['alex', 'WuSir', 'ritian', 'barry', 'wenzhou', 'q', 'w', 'e', 'r', 't']

Please delete the list of elements in the list "ritian", and add the output

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
li.pop(2)
print(li)

Results: [ 'alex', 'WuSir', 'barry', 'wenzhou']

Please delete a list after list of two elements, and output elements and remove elements deleted

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
print(li.pop(1))
print(li)

Results: WuSir
[ 'Alex', 'ritian', 'Barry', 'Wenzhou']

Please delete a list after list of 2-4 elements, remove elements and output

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
del li[1:5]
print(li)

Results: [ 'alex']

2. Write code that has the following list, use slices to achieve each function

L1 formed by slicing a new list of li list, l1 = [1,3,2]

li = [1, 3, 2, "a", 4, "b", 5,"c"]
print(li[0:3])

Li is formed by slicing a list of new list l2, l2 = [ "a", 4, "b"]

li = [1, 3, 2, "a", 4, "b", 5,"c"]
print(li[3:6])

L3 is formed by slicing a new list of li list, l3 = [ "1,2,4,5]

li = [1, 3, 2, "a", 4, "b", 5,"c"]
print(li[0::2])

L4 is formed by slicing a new list of li list, l4 = [3, "a", "b"]

li = [1, 3, 2, "a", 4, "b", 5,"c"]
print(li[1::2])

L5 is formed by slicing a new list of li list, l5 = [ "c"]

li = [1, 3, 2, "a", 4, "b", 5,"c"]
print(li[7])

The new list is formed by slicing li list l6, l6 = [ "b", "a", 3]

li = [1, 3, 2, "a", 4, "b", 5,"c"]
print(li[-1::-2])

3. Write code that has the following list, as required to achieve each function.

Lis in the list of "tt" capitalized (two ways).

lis = [2, 3, "k", ["qwe", 20, ["k1", ["tt", 3, "1"]], 89], "ab", "adv"]
lis[3][2][1][0] =lis[3][2][1][0].upper()
print(lis)

lis = [2, 3, "k", ["qwe", 20, ["k1", ["tt", 3, "1"]], 89], "ab", "adv"]
lis[3][2][1][0]='TT'
print(lis)

3, the list of numbers into character string "100" (in two ways).

lis = [2, 3, "k", ["qwe", 20, ["k1", ["tt", 3, "1"]], 89], "ab", "adv"]
lis[3][2][1][1]='100'
lis[1]='100'
print(lis)

lis = [2, 3, "k", ["qwe", 20, ["k1", ["tt", 3, "1"]], 89], "ab", "adv"]
lis[3][2][1][1:2] = ["100"]
print(lis)

List string "1" to numeral 101 (in two ways).

lis = [2, 3, "k", ["qwe", 20, ["k1", ["tt", 3, "1"]], 89], "ab", "adv"]
lis[3][2][1][2]='101'
print(lis)

lis = [2, 3, "k", ["qwe", 20, ["k1", ["tt", 3, "1"]], 89], "ab", "adv"]
lis[3][2][1][-1:] = [101]
print(lis)

4. implemented in code:

Underline the use of each element of the list spliced ​​into the string "alex_wusir_taibai"

li = ["alex", "wusir", "taibai"]
lis = li[0] + "_" + li[1] + "_" + li[2]
print(lis)

The use for the index print range loop and the following list.

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
for li in range(0,5):
print(li)

6. The use for loop and within the range 100 to find all of these and even-even inserted into a new list.

li = []
for i in range(2,101,2):
li.append(i)
print(i)

7. Use for loop and to identify within the range 503 can be divisible, and insert these numbers into a new list.

li = []
for i in range(3,51):
if i % 3 == 0:
li.append(i)
print(i)

8. The use for loop and range from 100 to 1, reverse printing.

li = []
for i in range(100,0,-1):
if i < 100:
li.append(i)
print(i)

9. The use for loop and range from 100 to 10, all even reverse added to a new list, then the list of filter elements, the number divisible by four stay.

li = list(range(100,9,-2))
for i in li:
if i % 4 != 0:
li.remove(i)
print(li)

10. The use for loop and Range, will add a digital 1-30 into a list, the list and the cycle will be changed to a number divisible by 3 *.

li = list(range(30,0,-1))
for i in li:
if i % 3 == 0:
li[li.index(i)] = "*"
print(li)

Guess you like

Origin www.cnblogs.com/xiaomo668/p/11502351.html