Learning Python(Third Day)

Learning Python(Third Day)

**Abstract: List Correction, List Insertion,List Modify and Some Tips **

  1. List Correction
party_list = [ "Tom", "Jerry", "Black"]
print("before:",party_list)

party_list[2] = "Smith"
print("after:",party_list)

print("before:",party_list)
party_list[1] = party_list[1] + " Green"   # equals to  string's plus

print("after:",party_list)

single_digits = ["zero","one","two","three","four","five","six","seven","eight","nine"]

print("single_digits:",single_digits)
print("The type of single_digits[5] is ",type(single_digits[5]))

single_digits[5] = 5
print("The type of single_digits[5] is ",type(single_digits[5]))
  1. List Insertion
print(party_list)

party_list.insert(-1,"You")  # The first item whitch represent the sequence of a list can be out of the range of the list . And if it is out of the
                               # range, it will append a item at the top or the bottom. Remember, it is difference from the slicing list when the value
                              # is out of the range(two directions)
                              #When you insert a item in a list, the item will get to the specific location and the rest items behind the specific locat-
                              #ion will move right.
print(party_list)
  1. List Modify
  1. ‘del’ function
sample_list = [1,7,8,95,4,1,6,3,9,71,5]
print("sample_list before:",sample_list)

del sample_list[10] # the item in del function can not expand the range of the list and there is not an output in del
print("sample_list after:",sample_list)
  1. .pop()
party_list = ["Tom","Green","Redic"]

print(party_list)
print(party_list.pop())  # .pop() has two meanings. Both returning and remove the last item(default) in a list
print(party_list.pop())
print(party_list.pop())
sample_list = [4,8,7,41,2,5,45,64,8,498,894,84,]

print(sample_list)

sample_list.pop(5) #When give a speicific location ,it is not the last item.
print(sample_list)
  1. .remove()
dog = ["pud","puddle","lab"]
print(dog)

dog.remove("puddle")    #neither .remove() or del has not an output but .pop() has an output
print(dog)
dog = ["pud","puddle","puddle","lab","puddle"]
print(dog)

while "puddle" in dog:
    dog.remove("puddle") #.remove() will remove a goal item in the first one time. if there is not any speficic item, it is an error to use this function
    print(dog)

  1. Some Tips

1)The empty list is seen as false by while loop

dog_types = ["hoo","lala","dada"]
print(dog_types)

while dog_types:    #empty list equals to false
    print(dog_types.pop(),dog_types)
print(dog_types)

2)Some forms about sets

if a in A: # discern whether a belongs to A or not and A can be a list or a string
    #here the codes

while a in A:
    #here the codes

for a in A:
    #here the codes

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值