Skip to main content

CS / IP - LIST OPERATIONS - PROJECT - 16TH AUG 24

 

LIST OPERATIONS- PROJECT FOR BEGINNERS

# -*- coding: utf-8 -*-

"""

Created on Fri Aug 16 10:18:23 2024

@author: Kirti Hora

"""


myList = [] #myList having 5 elements

choice = 0

print("\nL I S T O P E R A T I O N S")

ans='y'

while ans=='y':

    print(" 1. Append an element")

    print(" 2. Insert an element at the desired position")

    print(" 3. Append a list to the given list")

    print(" 4. Modify an existing element")

    print(" 5. Delete an existing element by its position")

    print(" 6. Delete an existing element by its value")       

    print(" 7. Sort the list in ascending order")

    print(" 8. Sort the list in descending order")

    print(" 9. Display the list")

    choice = int(input("ENTER YOUR CHOICE (1-9): "))

    #append element

    if choice == 1:

        #WAP to append an element in a list

        n =int (input("Enter no of elements"))

        i=0

        while i < n:

            num= int(input("Enter element " + str(i) + ": "))

            myList+=[num]  

            i+=1

        print("The elements have been appended\n")

    #insert an element at desired position

    elif choice == 2:

        #WAP to insert element at a particular position

        element = eval(input("Enter the element to be inserted: "))

        pos = int(input("Enter the position:"))

        myList.insert(pos,element)

        print("The element has been inserted\n")

    elif choice == 3:

       # WAP to enter list in the existing list

        newList = eval(input("Enter the list to be appended: "))

        myList.extend(newList)

        print("The list has been appended\n")

    #modify an existing element

    elif choice == 4:

        #WAP to to modify the element in the list

        i = int(input("Enter the position of the element to be modified: "))

        if i < len(myList):

            newElement = eval(input("Enter the new element: "))

            oldElement = myList[i]

            myList[i] = newElement

            print("The element",oldElement,"has been modified\n")

        else:

            print("Position of the element is more then the length of list")

    #delete an existing element by position

    elif choice == 5:

        #WAP to select the element on particular position

        i = int(input("Enter the position of the element to be deleted: "))

        if i < len(myList):

            element = myList.pop(i)

            print("The element",element,"has been deleted\n")

        else:

            print("\nPosition of the element is more then the length of list")

    #delete an existing element by value

    elif choice == 6:

       # WAP to remove particular element from  the list

        element = int(input("\nEnter the element to be deleted: "))

        if element in myList:

            myList.remove(element)

            print("\nThe element",element,"has been deleted\n")

        else:

            print("\nElement",element,"is not present in the list")

    #list in sorted order

    elif choice == 7:

       # WAP to sort the given list

        myList.sort()

        print("\nThe list has been sorted")

    #list in reverse sorted order

    elif choice == 8:

       # WAP a to print list in reverse order

        myList.sort(reverse = True)

        print("\nThe list has been sorted in reverse order")

    #display the list

    elif choice == 9:

        print("\nThe list is:", myList)

    else:

        print("Choice is not valid")

        ans=input("Do you want to continue..")

else:

    print("Terminating")

Comments

Popular posts from this blog

PYTHON - MYSQL CONNECTIVITY CODE

  #INSERTION OF DATA import mysql.connector mydb = mysql.connector.connect( host="localhost", user="root", passwd="root", database="school" ) print("Successfully Connected") #print(mydb) mycursor=mydb.cursor()   v1=int(input("enter ID:")) v2=input("enter name:") v3=input("enter Gender:") v4=int(input("enter age:")) sql='insert into TEACH values("%d","%s","%s","%s")'%(v1,v2,v3,v4) print(sql) mycursor.execute(sql) mydb.commit() print("record added") #MYSQL Connection code – Deletion on database SOURCE CODE: s=int(input("enter id of TEACHER to be deleted:")) r=(s,) v="delete from TEACH where id=%s" mycursor.execute(v,r) mydb.commit() print("record deleted") MYSQL Connection code – Updation on database SOURCE CODE: import mysql.connector mydb = mysql.connector.c...

GRADE XII - CSV FILE

CSV FILE (COMMA SEPERATED VALUE) CSV NOTES LINK :  CSV NOTES - CLICK HERE Writing data to a CSV file involves the following steps: Import the csv module Open the CSV file in write mode ( "w" ) using open() Create the writer object Write the data into the file using the writer object Close the file using close() writerow() Method This method is used to write a single row to a CSV file. It takes a sequence (list or tuple) as its parameter, and writes the items as a comma-separated line in the file. You do not need to add a newline character (\n) — it automatically places the end-of-line marker.     EXAMPLE 1: import csv  # Importing the csv module # CSV file opened using relative path csv_fobj = open("emp.csv", "w")   # Writer object created wtr_obj = csv.writer(csv_fobj) # Record with field heading is written wtr_obj.writerow(["Empno", "Name", "Salary"]) # Records with data are...