Last active
November 18, 2020 12:26
-
-
Save shinysu/5889db70186e9403e93029c89849e840 to your computer and use it in GitHub Desktop.
Examples & exercises using list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| check if an element is present in the list | |
| you can use 'in' operator to check if the element is present and 'not in' operator to check if the element is not present | |
| ''' | |
| cart = ['ice cream', 'chocolates', 'bread', 'jam', 'butter'] | |
| item = input("Enter the element you want to search: ") | |
| if item in cart: | |
| print("yes, the element is present") | |
| else: | |
| print("No, the element is not present") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| iterate through the list and display elements in the list | |
| ''' | |
| cart = ['ice cream', 'chocolates', 'bread', 'jam', 'butter'] | |
| for x in cart: | |
| print(x) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def add_item(): | |
| item = input("Enter the task: ") | |
| todolist.append(item) | |
| print(todolist) | |
| def delete_item(): | |
| item = input("Enter the element to be removed: ") | |
| todolist.remove(item) | |
| print(todolist) | |
| todolist =[] | |
| while True: | |
| choice = input("Enter your choice(add / delete / exit): ").lower() | |
| if choice == 'add': | |
| add_item() | |
| elif choice == 'delete': | |
| delete_item() | |
| elif choice == 'exit': | |
| break | |
| else: | |
| print("Invalid choice") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| In the todolist program, include two more options: | |
| 1. edit - To edit an item in the todolist | |
| - Get the old item and the new item from the user. | |
| - Remove the old item from the list and add the new item | |
| 2. display - Display all the elements in the list in sorted order | |
| - Sort the list | |
| - display the elements in the list using for loop | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment