diff --git a/anon_functions.py b/anon_functions.py new file mode 100644 index 0000000..d732603 --- /dev/null +++ b/anon_functions.py @@ -0,0 +1,5 @@ +numbers = [1, 2, 3, 4] +times_two = map(lambda x: x * 2, numbers) +print(list(times_two)) + +# [2, 4, 6, 8] diff --git a/check_memory.py b/check_memory.py new file mode 100644 index 0000000..c2d5f94 --- /dev/null +++ b/check_memory.py @@ -0,0 +1,10 @@ +import sys + +list_by_range = range(0, 10000) +print(sys.getsizeof(list_by_range)) +# 48 + + +list_by_lc = [x for x in range(0, 10000)] +print(sys.getsizeof(list_by_lc)) +# 87632 it more huge \ No newline at end of file diff --git a/check_version_python.py b/check_version_python.py new file mode 100644 index 0000000..f1f68a6 --- /dev/null +++ b/check_version_python.py @@ -0,0 +1,6 @@ +import sys + +if not sys.version_info > (2, 7): + # raises a advertence about the oldest python version +elif not sys.version_info >= (3, 5): + # Show a message to python's version to be higher than 3.5 diff --git a/comparision_operators.py b/comparision_operators.py new file mode 100644 index 0000000..0e1d28c --- /dev/null +++ b/comparision_operators.py @@ -0,0 +1,10 @@ +x = 9 +# Old way +if x > 5 and x < 15: + print("Yes") +# yes + +# New way +if 5 < x < 15: + print("Yes") +# Yes \ No newline at end of file diff --git a/counter.py b/counter.py new file mode 100644 index 0000000..049cd3c --- /dev/null +++ b/counter.py @@ -0,0 +1,10 @@ +from collections import Counter + +cool_list: list = [1, 1, 2, 3, 4, 5, 5, 5, 6, 6] +count: Counter = Counter(cool_list) +print(count) +# Counter({1: 2, 2: 1, 3: 1, 4: 1, 5: 3, 6: 2}) + +# Strings +print(Counter("aaaaabbbbbccccc")) +# Counter({'a': 5, 'b': 5, 'c': 5}) \ No newline at end of file diff --git a/create_string_by_list.py b/create_string_by_list.py new file mode 100644 index 0000000..d39a382 --- /dev/null +++ b/create_string_by_list.py @@ -0,0 +1,4 @@ +cool_list: str = ['Programming', 'in', 'Python', 'is', 'so', 'cool'] +cool_string: list = " ".join(cool_list) +print(cool_string) +# 'Programming in Python is so cool' \ No newline at end of file diff --git a/dataclasses.py b/dataclasses.py new file mode 100644 index 0000000..43813e4 --- /dev/null +++ b/dataclasses.py @@ -0,0 +1,15 @@ +from dataclasses import dataclass + +@dataclass +class Car: + name: str + color: str + +car = Car("Gol", "Black") + +print(car.name) +# 'Gol' + +print(car) + +# Car(name='Gol', color='Black') \ No newline at end of file diff --git a/decorators.py b/decorators.py new file mode 100644 index 0000000..12e7cbd --- /dev/null +++ b/decorators.py @@ -0,0 +1,12 @@ + +def count_decorator(func: function): + def wrapper(name, year_born, year_today): + age: int = year_today - year_born + return func(name, age) + return wrapper + +@count_age +def show_person(name: str, year_born: int, year_today: int = 2021): + return f'{name} is {year_born} old' + +print(show_person('Johnatas', 25)) \ No newline at end of file diff --git a/dict_set_compreensions.py b/dict_set_compreensions.py new file mode 100644 index 0000000..e5c2bd1 --- /dev/null +++ b/dict_set_compreensions.py @@ -0,0 +1,6 @@ +cool_dict: dict = {x: x**2 for x in (2, 4, 6)} +print(cool_dict) +# {2: 4, 4: 16, 6: 36} +# { for item in list if } +# {s for s in range(1,5) if s % 2} +# {1, 3} \ No newline at end of file diff --git a/elipsis.py b/elipsis.py new file mode 100644 index 0000000..6602770 --- /dev/null +++ b/elipsis.py @@ -0,0 +1,2 @@ +def my_awesome_func(): + ... \ No newline at end of file diff --git a/list_comprehensions.py b/list_comprehensions.py new file mode 100644 index 0000000..688a793 --- /dev/null +++ b/list_comprehensions.py @@ -0,0 +1,20 @@ + + +new_list = [i for i in range(10)] +print(new_list) +# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + +squares = [x**2 for x in range(10)] +print(squares) +# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] + +def cool_function(a): + return (a + 5) / 2 + +my_formula = [cool_function(i) for i in range(10)] +print(my_formula) +# [2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0] + +by_filter = [i for i in range(20) if i % 2 == 0] +print(by_filter) +# [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] \ No newline at end of file diff --git a/merging_dictionaires.py b/merging_dictionaires.py new file mode 100644 index 0000000..c3ae4e2 --- /dev/null +++ b/merging_dictionaires.py @@ -0,0 +1,12 @@ +dict1 = { 'a': 1, 'b': 2 } +dict2 = { 'b': 3, 'c': 4 } +merged = { **dict1, **dict2 } + +print (merged) +# {'a': 1, 'b': 3, 'c': 4} + +# Python >= 3.9 only +merged = dict1 | dict2 + +print (merged) +# {'a': 1, 'b': 3, 'c': 4} \ No newline at end of file diff --git a/named_strings_format.py b/named_strings_format.py new file mode 100644 index 0000000..cc7248d --- /dev/null +++ b/named_strings_format.py @@ -0,0 +1,15 @@ + +data = { + 'name': 'Megan', + 'age': 22, +} + +text: str = "%(name)s is %(age)" % data +print(text) + +data = None + +if data is None: + number, text = 22, 'Awesome' + text = "The number is %(number) and text is %(text)" % locals() + print(text) \ No newline at end of file diff --git a/nested_list_comprehension.py b/nested_list_comprehension.py new file mode 100644 index 0000000..cb42175 --- /dev/null +++ b/nested_list_comprehension.py @@ -0,0 +1,10 @@ +nested_list = [[j for j in range(3)] for i in range(4)] +print(nested_list) +# [[0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]] + +nested_list2 = [value + for sublist in m + for value in sublist] + +print(nested_list2) +# [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2] \ No newline at end of file diff --git a/python_dateutil.py b/python_dateutil.py new file mode 100644 index 0000000..60b8cb4 --- /dev/null +++ b/python_dateutil.py @@ -0,0 +1,6 @@ +from dateutil.parser import parse + +logline = 'INFO 2020-01-01T00:00:01 Happy new year, human.' +timestamp = parse(logline, fuzzy=True) +print(timestamp) +# 2020-01-01 00:00:01 \ No newline at end of file diff --git a/query_json_jmespath.py b/query_json_jmespath.py new file mode 100644 index 0000000..0baf927 --- /dev/null +++ b/query_json_jmespath.py @@ -0,0 +1,17 @@ +import jmespath + +persons: dict = { + "persons": [ + { "name": "Johnatas", "age": 25 }, + { "name": "Gaby", "age": 23 }, + { "name": "Emilly", "age": 23 } + ] +} + +query = jmespath.search('persons[*].age', persons) +print(query) +# [25, 23, 23] + +query = jmespath.search('persons[*].name', persons) +print(query) +# ['Johnatas', 'Gaby', 'Emilly'] \ No newline at end of file diff --git a/readme.MD b/readme.MD new file mode 100644 index 0000000..55ef8df --- /dev/null +++ b/readme.MD @@ -0,0 +1,8 @@ +A huge collections with a lot about Python World, complete as soon as possible. + + + + +Thanks to : + +- Erik van Baaren diff --git a/required_kwargs.py b/required_kwargs.py new file mode 100644 index 0000000..d49050e --- /dev/null +++ b/required_kwargs.py @@ -0,0 +1,6 @@ + +def some_function(*, a: int, b: int): + print(a, b) + +some_function(a=25, b=23) +# 25 23 \ No newline at end of file diff --git a/return_multiple_values.py b/return_multiple_values.py new file mode 100644 index 0000000..79504bb --- /dev/null +++ b/return_multiple_values.py @@ -0,0 +1,8 @@ + +def get_user(): + return 'Johnatas', '20/20/20' + +name, birthdate = get_user() + +print(name, birthdate) +# Johnatas , 20/20/20 \ No newline at end of file diff --git a/reverse_list_string.py b/reverse_list_string.py new file mode 100644 index 0000000..88896f9 --- /dev/null +++ b/reverse_list_string.py @@ -0,0 +1,8 @@ + +reverse_string: str = "abcdefg"[::-1] +print(reverse_string) +# 'gfedcba' + +reverse_array: list = [1, 2, 3, 4, 5][::-1] +print(reverse_array) +# [5, 4, 3, 2, 1] \ No newline at end of file diff --git a/slice.py b/slice.py new file mode 100644 index 0000000..b0b36b5 --- /dev/null +++ b/slice.py @@ -0,0 +1,11 @@ +first_two = [1, 2, 3, 4, 5][0:2] +print(first_two) +# [1, 2] + +steps = [1, 2, 3, 4, 5][0:5:2] +print(steps) +# [1, 3, 5] + +mystring = "abcdefdn nimt"[::2] +print(mystring) +# 'aced it' \ No newline at end of file diff --git a/split_string_in_list.py b/split_string_in_list.py new file mode 100644 index 0000000..2fc76f6 --- /dev/null +++ b/split_string_in_list.py @@ -0,0 +1,4 @@ +cool_string: str = "Programming in Python is so cool" +string_list = cool_string.split(' ') +print(string_list) +# ['Programming', 'in', 'Python', 'is', 'so', 'cool'] \ No newline at end of file diff --git a/string_title.py b/string_title.py new file mode 100644 index 0000000..0dd2994 --- /dev/null +++ b/string_title.py @@ -0,0 +1,3 @@ +cool_text = "python tricks" +print(cool_text.title()) +# 'Python Tricks' \ No newline at end of file diff --git a/swapping.py b/swapping.py new file mode 100644 index 0000000..914399c --- /dev/null +++ b/swapping.py @@ -0,0 +1,7 @@ +a = 'João' +b = 'Jacob' +a, b = b, a +print (a) +# Jacob +print (b) +# João \ No newline at end of file diff --git a/ternary_operator.py b/ternary_operator.py new file mode 100644 index 0000000..0f036d4 --- /dev/null +++ b/ternary_operator.py @@ -0,0 +1,2 @@ + +x = "Success!" if (y == 2) else "Failed!" \ No newline at end of file diff --git a/underscore_repeat.py b/underscore_repeat.py new file mode 100644 index 0000000..9963b28 --- /dev/null +++ b/underscore_repeat.py @@ -0,0 +1,8 @@ + +""" +>>> 3 * 3 +9 +>>> _ + 3 +12 + +""" \ No newline at end of file diff --git a/unique_of_list_strings.py b/unique_of_list_strings.py new file mode 100644 index 0000000..2a007e9 --- /dev/null +++ b/unique_of_list_strings.py @@ -0,0 +1,6 @@ +cool_list: list = [1, 1, 2, 3, 4, 5, 5, 5, 6, 6] +print (set(cool_list)) +# {1, 2, 3, 4, 5, 6} + +cool_string: str = (set("aaabbbcccdddeeefff")) +# {'a', 'b', 'c', 'd', 'e', 'f'} \ No newline at end of file diff --git a/unpacking.py b/unpacking.py new file mode 100644 index 0000000..82e045b --- /dev/null +++ b/unpacking.py @@ -0,0 +1,14 @@ +# ** unpack by named keywords +def cool_function(name: str, age: int): + print(name, age) + +person_dict = { "name": 'Johnatas', "age": 25 } +cool_function(**person_dict) +# Johnatas 25 + +# * unpack by position +def other_function(name: str, age: int, country: str): + print(name, age, country) +person_list = ['Johnatas', 25, 'Brazil'] +other_function(*person_list) +# Johnatas 25 Brazil \ No newline at end of file diff --git a/using_map.py b/using_map.py new file mode 100644 index 0000000..1922416 --- /dev/null +++ b/using_map.py @@ -0,0 +1,11 @@ +def upper(string_for : str): + return string_for.upper() + +string_list: list = list(map(upper, ['python', 'javascript'])) +print(string_list) +# ['PYTHON', 'JAVASCRIPT'] + +# Convert string in list of numbers +ints_list: list = list(map(int, "1234567")) +print(ints_list) +# [1, 2, 3, 4, 5, 6, 7] \ No newline at end of file diff --git a/zen_of_python.py b/zen_of_python.py new file mode 100644 index 0000000..7340424 --- /dev/null +++ b/zen_of_python.py @@ -0,0 +1,24 @@ +import this + + +'''The Zen of Python, by Tim Peters + +Beautiful is better than ugly. +Explicit is better than implicit. +Simple is better than complex. +Complex is better than complicated. +Flat is better than nested. +Sparse is better than dense. +Readability counts. +Special cases aren't special enough to break the rules. +Although practicality beats purity. +Errors should never pass silently. +Unless explicitly silenced. +In the face of ambiguity, refuse the temptation to guess. +There should be one-- and preferably only one --obvious way to do it. +Although that way may not be obvious at first unless you're Dutch. +Now is better than never. +Although never is often better than *right* now. +If the implementation is hard to explain, it's a bad idea. +If the implementation is easy to explain, it may be a good idea. +Namespaces are one honking great idea -- let's do more of those! ''' \ No newline at end of file