Created
September 10, 2020 16:32
-
-
Save shinysu/41d129d96895433de552a0d157710604 to your computer and use it in GitHub Desktop.
day7 examples
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 over a list and print all the 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
| ''' | |
| find the minimum value in the given list | |
| ''' | |
| def find_minimum(numbers): | |
| minimum = numbers[0] | |
| for x in numbers: | |
| if x < minimum: | |
| minimum = x | |
| return minimum | |
| numbers = [] | |
| n = int(input("Enter the number of elements:")) | |
| for i in range(n): | |
| num = int(input()) | |
| numbers.append(num) | |
| minimum = find_minimum(numbers) | |
| print(minimum) |
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
| ''' | |
| remove duplicate elements from a given list | |
| sample input : [1, 2, 1, 3, 2] | |
| sample output: [1, 2, 3] | |
| ''' | |
| def remove_duplicates(original): | |
| unique = [] | |
| for x in original: | |
| if x not in unique: | |
| unique.append(x) | |
| return unique | |
| numbers = [] | |
| n = int(input("Enter the number of elements:")) | |
| for i in range(n): | |
| num = int(input()) | |
| numbers.append(num) | |
| unique = remove_duplicates(numbers) | |
| print(unique) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment