Learning Python(Third Day)
**Abstract: List Correction, List Insertion,List Modify and Some Tips **
- 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]))
- 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)
- List Modify
- ‘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)
- .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)
- .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)
- 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