Anúncio
Anúncio

Mais conteúdo relacionado

Anúncio

program 2.pptx

  1. #Program 2: Create a list and perform the following methods # 1) insert() 2) remove() 3) append() # 4) len() 5) pop() 6) clear() print("nt───────────────────────────────") print("t Create a list and perform the following methods") print("t────────────────────────────────") print("t 1) insert() 2) remove() 3) append()") print("t 4) len() 5) pop() 6) clear()") print("t────────────────────────────────")
  2. Usr_Lst = eval(input("Enter elements into the list ([,]): ")) print("The List is: ",Usr_Lst) # Method 1: insert() print("ntt Method 1: insert()") ele = eval(input("Enter any value to insert into list: ")) pos = int(input("Enter the position to insert: ")) Usr_Lst.insert(pos,ele) print("The Updated List is: ",Usr_Lst) # Method 2: remove() print("ntt Method 2: remove()") ele = int(input("Enter the value to delete: ")) if ele in Usr_Lst: Usr_Lst.remove(ele) print("The Updated List is: ",Usr_Lst) else: print(ele," is NOT present in the list"); print("The List is: ",Usr_Lst, " Unchanged")
  3. # Method 3: append() print("ntt Method 3: append()") ele = eval(input("Enter any value to append: ")) Usr_Lst.append(ele) print("The Updated List is: ",Usr_Lst) # Method 4: len() print("ntt Method 4: len()") length = len(Usr_Lst) print("The length of the list: ",Usr_Lst," is: ",length)
  4. # Method 5: pop() print("ntt Method 5: pop()") pos = int(input("Enter a valid position to delete from: ")) if pos <= length: ele = Usr_Lst.pop(pos) print("The Popped Element is: ",ele) print("The Updated List is: ",Usr_Lst) else: print("Invalid Position") # Method 6: clear() print("ntt Method 6: clear()") Usr_Lst.clear() print("The List ",Usr_Lst," is Cleared") print("────────────────────────────────")
Anúncio