Python Lists


list1 = [1,2,3,4,5,6,7]
list2 = [22, 33, 44, 55 ]
print(list1)
list1.append(2222)
print(list1)
list3 = list1+list2
print(list3)



list1 = [1,2,3,4,5,3,6,7]
list2 = [22, 33, 44, 55 , 3]
print("list1 = ",list1)
list1.append(2222)
print("list1 = ",list1)
list3 = list1+list2
print("list3 = ",list3)
print("length of list1 = ",len(list1))
list3.insert(3,8898)
print("list3 = ",list3)
list3.remove(3)
print("list3 = ",list3)
list3.extend(list1)
print("list3 = ",list3)
list3.insert(0,99999)
print("list3 = ",list3)
print("list3.count(3) = ",list3.count(3))
list3.sort()
print("list3 = ",list3)
list3.reverse()
print("list3 = ",list3)





Results on screen:
>>> 
list1 =  [1, 2, 3, 4, 5, 3, 6, 7]
list1 =  [1, 2, 3, 4, 5, 3, 6, 7, 2222]
list3 =  [1, 2, 3, 4, 5, 3, 6, 7, 2222, 22, 33, 44, 55, 3]
length of list1 =  9
list3 =  [1, 2, 3, 8898, 4, 5, 3, 6, 7, 2222, 22, 33, 44, 55, 3]
list3 =  [1, 2, 8898, 4, 5, 3, 6, 7, 2222, 22, 33, 44, 55, 3]
list3 =  [1, 2, 8898, 4, 5, 3, 6, 7, 2222, 22, 33, 44, 55, 3, 1, 2, 3, 4, 5, 3, 6, 7, 2222]
list3 =  [99999, 1, 2, 8898, 4, 5, 3, 6, 7, 2222, 22, 33, 44, 55, 3, 1, 2, 3, 4, 5, 3, 6, 7, 2222]
list3.count(3) =  4
list3 =  [1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 22, 33, 44, 55, 2222, 2222, 8898, 99999]
list3 =  [99999, 8898, 2222, 2222, 55, 44, 33, 22, 7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 3, 3, 2, 2, 1, 1]
>>> 

Traversing a list in Python

aList=['You','Are','My','Friend']
for i in range(len(aList)):
    print("i = ",i," - aList[",i,"]= ",aList[i])

 



List Membership
myFriends=["Tom","John","Peter","Ian","Paul"]
who=input("Find a friend of mine: ")
if who in myFriends:
    print("Congrats! ",who," is a friend of mine.")
else:
    print("I am sorry! ",who," is not a friend of mine.")



    
List Methods
aList=[1,2,3,4,5,6,1,2,2,3,4]
aList.append(111)
aList=list(aList)
print("aList = ",aList)
x=input("Give me the number to append: ")
aList.append(int(x))
print("aList = ",aList)
x=input("Give the number to search for: ")
print("Number ",x," there is ",aList.count(int(x)), " times in the list.")
x=input("Give me the list to extend: ")
aList.extend(list(x))
print("aList = ",aList)
x=input("Give me a number to find index: ")
print("At index ",aList.index(int(x))," is item ",aList[aList.index(int(x))])
item=input("Give an item to insert: ")
position=input("Give the position to put it: ")
aList.insert(int(position),item)
print("aList = ",aList)
print("Last item popped: ",aList.pop())
print("aList = ",aList)
item=input("Give me item to remove: ")
aList.remove(int(item))
print("aList = ",aList)
aList.reverse()
print("aList reversed is ",aList)
for i in range(len(aList)):
    aList[i]=int(aList[i])
aList.sort()
print("aList sorted is ",aList)



Results on screen
>>> 
aList =  [1, 2, 3, 4, 5, 6, 1, 2, 2, 3, 4, 111]
Give me the number to append: 2
aList =  [1, 2, 3, 4, 5, 6, 1, 2, 2, 3, 4, 111, 2]
Give the number to search for: 2
Number  2  there is  4  times in the list.
Give me the list to extend: 2
aList =  [1, 2, 3, 4, 5, 6, 1, 2, 2, 3, 4, 111, 2, '2']
Give me a number to find index: 2
At index  1  is item  2
Give an item to insert: 2
Give the position to put it: 2
aList =  [1, 2, '2', 3, 4, 5, 6, 1, 2, 2, 3, 4, 111, 2, '2']
Last item popped:  2
aList =  [1, 2, '2', 3, 4, 5, 6, 1, 2, 2, 3, 4, 111, 2]
Give me item to remove: 2
aList =  [1, '2', 3, 4, 5, 6, 1, 2, 2, 3, 4, 111, 2]
aList reversed is  [2, 111, 4, 3, 2, 2, 1, 6, 5, 4, 3, '2', 1]
aList sorted is  [1, 1, 2, 2, 2, 2, 3, 3, 4, 4, 5, 6, 111]
>>> 




Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου