-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Johnatas Rabelo Santos
committed
Feb 15, 2021
0 parents
commit 207a36a
Showing
30 changed files
with
280 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} | ||
# { <expression> for item in list if <conditional> } | ||
# {s for s in range(1,5) if s % 2} | ||
# {1, 3} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def my_awesome_func(): | ||
... |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
A huge collections with a lot about Python World, complete as soon as possible. | ||
|
||
|
||
|
||
|
||
Thanks to : | ||
|
||
- Erik van Baaren |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
def some_function(*, a: int, b: int): | ||
print(a, b) | ||
|
||
some_function(a=25, b=23) | ||
# 25 23 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
def get_user(): | ||
return 'Johnatas', '20/20/20' | ||
|
||
name, birthdate = get_user() | ||
|
||
print(name, birthdate) | ||
# Johnatas , 20/20/20 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
cool_text = "python tricks" | ||
print(cool_text.title()) | ||
# 'Python Tricks' |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
a = 'João' | ||
b = 'Jacob' | ||
a, b = b, a | ||
print (a) | ||
# Jacob | ||
print (b) | ||
# João |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
x = "Success!" if (y == 2) else "Failed!" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
""" | ||
>>> 3 * 3 | ||
9 | ||
>>> _ + 3 | ||
12 | ||
""" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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! ''' |