Skip to content

Commit

Permalink
Python new tricks
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnatas Rabelo Santos committed Feb 15, 2021
0 parents commit 207a36a
Show file tree
Hide file tree
Showing 30 changed files with 280 additions and 0 deletions.
5 changes: 5 additions & 0 deletions anon_functions.py
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]
10 changes: 10 additions & 0 deletions check_memory.py
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
6 changes: 6 additions & 0 deletions check_version_python.py
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
10 changes: 10 additions & 0 deletions comparision_operators.py
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
10 changes: 10 additions & 0 deletions counter.py
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})
4 changes: 4 additions & 0 deletions create_string_by_list.py
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'
15 changes: 15 additions & 0 deletions dataclasses.py
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')
12 changes: 12 additions & 0 deletions decorators.py
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))
6 changes: 6 additions & 0 deletions dict_set_compreensions.py
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}
2 changes: 2 additions & 0 deletions elipsis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def my_awesome_func():
...
20 changes: 20 additions & 0 deletions list_comprehensions.py
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]
12 changes: 12 additions & 0 deletions merging_dictionaires.py
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}
15 changes: 15 additions & 0 deletions named_strings_format.py
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)
10 changes: 10 additions & 0 deletions nested_list_comprehension.py
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]
6 changes: 6 additions & 0 deletions python_dateutil.py
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
17 changes: 17 additions & 0 deletions query_json_jmespath.py
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']
8 changes: 8 additions & 0 deletions readme.MD
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
6 changes: 6 additions & 0 deletions required_kwargs.py
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
8 changes: 8 additions & 0 deletions return_multiple_values.py
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
8 changes: 8 additions & 0 deletions reverse_list_string.py
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]
11 changes: 11 additions & 0 deletions slice.py
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'
4 changes: 4 additions & 0 deletions split_string_in_list.py
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']
3 changes: 3 additions & 0 deletions string_title.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cool_text = "python tricks"
print(cool_text.title())
# 'Python Tricks'
7 changes: 7 additions & 0 deletions swapping.py
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
2 changes: 2 additions & 0 deletions ternary_operator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

x = "Success!" if (y == 2) else "Failed!"
8 changes: 8 additions & 0 deletions underscore_repeat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

"""
>>> 3 * 3
9
>>> _ + 3
12
"""
6 changes: 6 additions & 0 deletions unique_of_list_strings.py
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'}
14 changes: 14 additions & 0 deletions unpacking.py
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
11 changes: 11 additions & 0 deletions using_map.py
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]
24 changes: 24 additions & 0 deletions zen_of_python.py
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! '''

0 comments on commit 207a36a

Please sign in to comment.