{}
See how a CS professor is using our compiler for class assignment.
Try Programiz PRO for Educators!
Learn DSA with step-by-step code visualization.
Try Programiz PRO for Educators!
run-icon
main.py
# Online Python compiler (interpreter) to run Python online. # Write Python 3 code in this online editor and run it. from functools import reduce # Data countries = ['Estonia', 'Finland', 'Sweden', 'Denmark', 'Norway', 'Iceland'] names = ['Asabeneh', 'Lidiya', 'Ermias', 'Abraham'] numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # For loops print("Countries:") for country in countries: print(country) print("\nNames:") for name in names: print(name) print("\nNumbers:") for number in numbers: print(number) # map print("\nMap - Countries to UPPERCASE:") print(list(map(str.upper, countries))) print("\nMap - Square of Numbers:") print(list(map(lambda x: x**2, numbers))) print("\nMap - Names to UPPERCASE:") print(list(map(str.upper, names))) # filter print("\nFilter - Countries containing 'land':") print(list(filter(lambda country: 'land' in country, countries))) print("\nFilter - Countries with exactly 6 characters:") print(list(filter(lambda country: len(country) == 6, countries))) print("\nFilter - Countries with 6+ characters:") print(list(filter(lambda country: len(country) >= 6, countries))) print("\nFilter - Countries starting with 'E':") print(list(filter(lambda country: country.startswith('E'), countries))) # reduce print("\nReduce - Sum of numbers:") print(reduce(lambda x, y: x + y, numbers)) # Higher-order function def higher_order_function(f, data): return [f(item) for item in data] print("\nHigher-order function - lowercase countries:") print(higher_order_function(str.lower, countries)) # Closure def make_multiplier(n): def multiplier(x): return x * n return multiplier times3 = make_multiplier(3) print("\nClosure - Multiply 10 by 3:") print(times3(10)) # Decorator def uppercase_decorator(func): def wrapper(): result = func() return result.upper() return wrapper @uppercase_decorator def greet(): return 'hello' print("\nDecorator - Greet:") print(greet())
Output