{}
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 from collections import Counter from operator import itemgetter # Sample data countries = ['Estonia', 'Finland', 'Sweden', 'Denmark', 'Norway', 'Iceland'] names = ['Asabeneh', 'Lidiya', 'Ermias', 'Abraham'] numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # ----------------------------------- # MAP, FILTER, REDUCE BASICS # ----------------------------------- # map: apply transformation countries_upper = list(map(str.upper, countries)) numbers_squared = list(map(lambda x: x**2, numbers)) names_upper = list(map(str.upper, names)) # filter: filter based on condition countries_with_land = list(filter(lambda x: 'land' in x, countries)) six_char_countries = list(filter(lambda x: len(x) == 6, countries)) six_or_more = list(filter(lambda x: len(x) >= 6, countries)) starting_with_E = list(filter(lambda x: x.startswith('E'), countries)) # reduce: combine to a single result total_sum = reduce(lambda x, y: x + y, numbers) sentence = reduce(lambda x, y: x + ', ' + y, countries[:-1]) + f", and {countries[-1]} are north European countries" # chaining chained_result = reduce(lambda x, y: x + y, filter(lambda x: x > 10, map(lambda x: x ** 2, numbers))) # ----------------------------------- # HIGHER-ORDER FUNCTION, CLOSURE, DECORATOR # ----------------------------------- # higher-order function def apply_twice(fn, value): return fn(fn(value)) # closure def multiplier(factor): def multiply(x): return x * factor return multiply double = multiplier(2) # decorator def my_decorator(fn): def wrapper(): print("Before call") fn() print("After call") return wrapper @my_decorator def say_hello(): print("Hello!") # ----------------------------------- # FOR LOOPS # ----------------------------------- for country in countries: print(country) for name in names: print(name) for number in numbers: print(number) # ----------------------------------- # FUNCTION UTILITIES # ----------------------------------- def get_string_lists(lst): return list(filter(lambda x: isinstance(x, str), lst)) def categorize_countries(pattern): return [country for country in countries if pattern in country] def country_starting_letters_dict(countries): result = {} for country in countries: first = country[0] result[first] = result.get(first, 0) + 1 return result def get_first_ten_countries(lst): return lst[:10] def get_last_ten_countries(lst): return lst[-10:] # ----------------------------------- # LEVEL 3 - countries_data mock # ----------------------------------- countries_data = [ {'name': 'Finland', 'capital': 'Helsinki', 'population': 5527573, 'languages': ['Finnish', 'Swedish']}, {'name': 'Sweden', 'capital': 'Stockholm', 'population': 10099265, 'languages': ['Swedish']}, {'name': 'Norway', 'capital': 'Oslo', 'population': 5421241, 'languages': ['Norwegian']}, {'name': 'Denmark', 'capital': 'Copenhagen', 'population': 5831404, 'languages': ['Danish']}, {'name': 'Iceland', 'capital': 'Reykjavik', 'population': 343599, 'languages': ['Icelandic']}, {'name': 'Estonia', 'capital': 'Tallinn', 'population': 1326535, 'languages': ['Estonian']}, {'name': 'Germany', 'capital': 'Berlin', 'population': 83783942, 'languages': ['German']}, {'name': 'Russia', 'capital': 'Moscow', 'population': 145912025, 'languages': ['Russian']}, {'name': 'India', 'capital': 'New Delhi', 'population': 1393409038, 'languages': ['Hindi', 'English']}, {'name': 'China', 'capital': 'Beijing', 'population': 1444216107, 'languages': ['Mandarin']}, {'name': 'USA', 'capital': 'Washington, D.C.', 'population': 331002651, 'languages': ['English']}, ] # sort by name, capital, population sorted_by_name = sorted(countries_data, key=itemgetter('name')) sorted_by_capital = sorted(countries_data, key=itemgetter('capital')) sorted_by_population = sorted(countries_data, key=itemgetter('population'), reverse=True) # top 10 spoken languages all_languages = [] for country in countries_data: all_languages.extend(country['languages']) top_languages = Counter(all_languages).most_common(10) # top 10 populated countries top_populated = sorted_by_population[:10] # ----------------------------------- # DISPLAY SAMPLE OUTPUTS # ----------------------------------- print("Countries Uppercase:", countries_upper) print("Squared Numbers:", numbers_squared) print("Names Uppercase:", names_upper) print("Countries with 'land':", countries_with_land) print("Countries with 6 letters:", six_char_countries) print("Countries >= 6 letters:", six_or_more) print("Countries starting with 'E':", starting_with_E) print("Chained Result:", chained_result) print("Total Sum:", total_sum) print("Sentence:", sentence) print("Get Strings Only:", get_string_lists([1, 'a', 3.5, 'b', True])) print("Categorized (land):", categorize_countries('land')) print("Country Start Dict:", country_starting_letters_dict(countries)) print("First 10 Countries:", get_first_ten_countries(countries)) print("Last 10 Countries:", get_last_ten_countries(countries)) print("Top Languages:", top_languages) print("Top Populated Countries:", [c['name'] for c in top_populated])
Output