Created
April 18, 2020 15:00
-
-
Save HAKSOAT/296c297ef6ae92bccdf9419418d22395 to your computer and use it in GitHub Desktop.
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 decorate_text(text): | |
| decoration = "\n\n**********{}**********\n\n" | |
| decorated_text = decoration.format(text) | |
| return decorated_text | |
| # Unpacking of values | |
| def generate_multiplications_1(multiplicand, start, stop): | |
| multiplications = [] | |
| for multiplier in range(start, stop + 1): | |
| answer = multiplicand * multiplier | |
| multiplications.append(answer) | |
| return multiplications | |
| values = [2, 1, 10] | |
| print(generate_multiplications_1(*values)) | |
| kw_values = {"multiplicand": 8, "start": 1, "stop": 10} | |
| print(generate_multiplications_1(**kw_values)) | |
| print(decorate_text('The generate_multiplications_1 function')) | |
| # Packing of values into args | |
| def generate_multiplications_2(*values): | |
| multiplicand = values[0] | |
| start = values[1] | |
| stop = values[2] | |
| multiplications = [] | |
| for multiplier in range(start, stop + 1): | |
| answer = multiplicand * multiplier | |
| multiplications.append(answer) | |
| return multiplications | |
| print(generate_multiplications_2(3, 1, 12)) | |
| print(generate_multiplications_2(5, 5, 20)) | |
| print(decorate_text('The generate_multiplications_2 function')) | |
| # Packing of values into kwargs | |
| def generate_multiplications_3(**kwargs): | |
| print("It packs it into a dictionary: ", kwargs, end='\n\n') | |
| multiplicand = kwargs["multiplicand"] | |
| start = kwargs["start"] | |
| stop = kwargs["stop"] | |
| multiplications = [] | |
| for multiplier in range(start, stop + 1): | |
| answer = multiplicand * multiplier | |
| multiplications.append(answer) | |
| return multiplications | |
| print(generate_multiplications_3(multiplicand=8, start=1, stop=10)) | |
| print(generate_multiplications_3(multiplicand=2, start=1, stop=10)) | |
| print(decorate_text('The generate_multiplications_3 function')) | |
| # Creating Decorators | |
| # Applying a decorator to an existing function | |
| def show_even_numbers_only(func): | |
| def wrapper(*args, **kwargs): | |
| numbers = func(*args, **kwargs) | |
| even_numbers = [] | |
| for number in numbers: | |
| if number % 2 == 0: | |
| even_numbers.append(number) | |
| return even_numbers | |
| return wrapper | |
| # Using decorators | |
| wrapper = show_even_numbers_only(generate_multiplications_1) | |
| print(wrapper.__name__) | |
| print(wrapper(3, start=1, stop=30)) | |
| print(wrapper(7, start=1, stop=30)) | |
| print(decorate_text('The show_even_numbers_only decorator function')) | |
| # Decorating using an existing decorator function | |
| @show_even_numbers_only | |
| def generate_multiplications_4(multiplicand, start, stop): | |
| multiplications = [] | |
| for multiplier in range(start, stop + 1): | |
| answer = multiplicand * multiplier | |
| multiplications.append(answer) | |
| return multiplications | |
| print(generate_multiplications_4(10, start=1, stop=10)) | |
| print(generate_multiplications_4(6, start=10, stop=20)) | |
| print(decorate_text( | |
| 'The generate_multiplications_4 function with the show_even_numbers_only decorator' | |
| )) | |
| lyrics = [ | |
| "Una mattina mi son alzato","O bella ciao, bella ciao, bella ciao, ciao, ciao", | |
| "Una mattina mi son alzato","E ho trovato l'invasor", | |
| "O partigiano, portami via","O bella ciao, bella ciao, bella ciao, ciao, ciao", | |
| "O partigiano, portami via","Ché mi sento di morir", | |
| "E se io muoio da partigiano","O bella ciao, bella ciao, bella ciao, ciao, ciao", | |
| "E se io muoio da partigiano","Tu mi devi seppellir", "E seppellire lassù in montagna", | |
| "O bella ciao, bella ciao, bella ciao, ciao, ciao","E seppellire lassù in montagna", | |
| "Sotto l'ombra di un bel fior", "E le genti che passeranno", | |
| "O bella ciao, bella ciao, bella ciao ciao ciao","E le genti che passeranno", | |
| "Mi diranno «che bel fior.", "Questo è il fiore del partigiano", | |
| "O bella ciao, bella ciao, bella ciao ciao ciao","Questo è il fiore del partigiano", | |
| "Morto per la libertà"] | |
| def get_lyrics_line_length(lyrics): | |
| lengths = [] | |
| for lyric in lyrics: | |
| lengths.append(len(lyric)) | |
| return lengths | |
| print(get_lyrics_line_length(lyrics)) | |
| print(decorate_text( | |
| 'The get_lyrics_line_length function without the show_even_numbers_only decorator' | |
| )) | |
| @show_even_numbers_only | |
| def get_lyrics_line_length(lyrics): | |
| lengths = [] | |
| for lyric in lyrics: | |
| lengths.append(len(lyric)) | |
| return lengths | |
| print(get_lyrics_line_length(lyrics)) | |
| print(decorate_text( | |
| 'The get_lyrics_line_length function with the show_even_numbers_only decorator function' | |
| )) | |
| # Lambda fuctions | |
| square_it = lambda number: pow(number, 2) | |
| print(square_it(5)) | |
| print(decorate_text('The line above is for squaring numbers using a lambda function')) | |
| def sort_by_third(x): | |
| return x[2] | |
| numbers = [ | |
| [20, 35, 70], [34, 16, 82], [34, 5, 9], [12, 10, 0], | |
| [10, 82, 15], [72, 63, 30], [76, 6, 7]] | |
| print(sorted(numbers, key=sort_by_third)) | |
| print(decorate_text('Sorting using a proper function as the key')) | |
| numbers = [ | |
| [20, 35, 70], [34, 16, 82], [34, 5, 9], [12, 10, 0], | |
| [10, 82, 15], [72, 63, 30], [76, 6, 7]] | |
| print(sorted(numbers, key=lambda x: x[2])) | |
| print(decorate_text('Sorting using a lambda function as the key')) | |
| # Deep list copy | |
| a = [1, 2, 5] | |
| b = a | |
| b[1] = 10 | |
| print(a) | |
| print(b) | |
| print(decorate_text('Doing a deep copy of the list a to b')) | |
| # Shallow list copy | |
| a = [1, 2, 5] | |
| b = a.copy() | |
| b[1] = 10 | |
| print(a) | |
| print(b) | |
| print(decorate_text('Doing a shallow copy of the list a to b')) | |
| # Checking some of the methods (functions) or attributes from an object | |
| print(dir(numbers)) | |
| print(decorate_text('Some methods and attributes of a Python list')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment