From 9d179e44d5f8b97a4a779e62eb56053a75ae10d9 Mon Sep 17 00:00:00 2001 From: CarlosViniMSouza Date: Sat, 5 Feb 2022 16:22:31 -0300 Subject: [PATCH] added folder = CarlosViniMSouza -> 10 programs .py --- CarlosViniMSouza/0001/day01.py | 12 ++++++++++++ CarlosViniMSouza/0002/day02.py | 21 +++++++++++++++++++++ CarlosViniMSouza/0003/day03.py | 18 ++++++++++++++++++ CarlosViniMSouza/0004/day04.py | 31 +++++++++++++++++++++++++++++++ CarlosViniMSouza/0005/day05.py | 11 +++++++++++ CarlosViniMSouza/0006/day06.py | 15 +++++++++++++++ CarlosViniMSouza/0007/day07.py | 19 +++++++++++++++++++ CarlosViniMSouza/0008/day08.py | 25 +++++++++++++++++++++++++ CarlosViniMSouza/0009/day09.py | 21 +++++++++++++++++++++ CarlosViniMSouza/0010/day10.py | 17 +++++++++++++++++ 10 files changed, 190 insertions(+) create mode 100644 CarlosViniMSouza/0001/day01.py create mode 100644 CarlosViniMSouza/0002/day02.py create mode 100644 CarlosViniMSouza/0003/day03.py create mode 100644 CarlosViniMSouza/0004/day04.py create mode 100644 CarlosViniMSouza/0005/day05.py create mode 100644 CarlosViniMSouza/0006/day06.py create mode 100644 CarlosViniMSouza/0007/day07.py create mode 100644 CarlosViniMSouza/0008/day08.py create mode 100644 CarlosViniMSouza/0009/day09.py create mode 100644 CarlosViniMSouza/0010/day10.py diff --git a/CarlosViniMSouza/0001/day01.py b/CarlosViniMSouza/0001/day01.py new file mode 100644 index 00000000..6b9e1238 --- /dev/null +++ b/CarlosViniMSouza/0001/day01.py @@ -0,0 +1,12 @@ +# How to sort a Python dict by value + +import operator + + +dict_test = {'a': 4, 'b': 3, 'c': 2, 'd': 1} + +print(sorted(dict_test.items(), key=lambda x: x[1])) +# Ouput: [('d', 1), ('c', 2), ('b', 3), ('a', 4)] + +print(sorted(dict_test.items(), key=operator.itemgetter(1))) +# Output: [('d', 1), ('c', 2), ('b', 3), ('a', 4)] diff --git a/CarlosViniMSouza/0002/day02.py b/CarlosViniMSouza/0002/day02.py new file mode 100644 index 00000000..89f21503 --- /dev/null +++ b/CarlosViniMSouza/0002/day02.py @@ -0,0 +1,21 @@ +# The get() method on dicts and its "default" argument + +names = { + 382: "Beatriz", + 590: "Carlos", + 951: "Dilbert", +} + + +def greeting(userid): + return "Hi %s!" % names.get(userid, "there") + + +print(greeting(382)) +# Output: "Hi Alice!" + +print(greeting(333)) +# Output: "Hi there!" + +print(greeting(951)) +# Output: "Hi Dilbert!" diff --git a/CarlosViniMSouza/0003/day03.py b/CarlosViniMSouza/0003/day03.py new file mode 100644 index 00000000..d4ca4953 --- /dev/null +++ b/CarlosViniMSouza/0003/day03.py @@ -0,0 +1,18 @@ +# Why Python is Great: Namedtuples + +# Using namedtuple is way shorter than defining a class manually: +from collections import namedtuple + +Car = namedtuple('Car', 'color mileage') + +# Our new "Car" class works as expected: +my_car = Car('red', 3812.4) + +print(my_car.color) +# Output: 'red' + +print(my_car.mileage) +# Output: 3812.4 + +print(my_car) +# OutputCar(color='red', mileage=3812.4) diff --git a/CarlosViniMSouza/0004/day04.py b/CarlosViniMSouza/0004/day04.py new file mode 100644 index 00000000..aaae283e --- /dev/null +++ b/CarlosViniMSouza/0004/day04.py @@ -0,0 +1,31 @@ +import this + +# Python-zin !! ✌️🤓 + +""" +Output: + +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! +""" + +# This is to play with Python Interpreter diff --git a/CarlosViniMSouza/0005/day05.py b/CarlosViniMSouza/0005/day05.py new file mode 100644 index 00000000..a6825edc --- /dev/null +++ b/CarlosViniMSouza/0005/day05.py @@ -0,0 +1,11 @@ +import json + +my_map = { + 'a': 23, + 'b': 42, + 'c': 0xc0ffee +} + +print(my_map) + +print(json.dumps(my_map, indent=4, sort_keys=True)) diff --git a/CarlosViniMSouza/0006/day06.py b/CarlosViniMSouza/0006/day06.py new file mode 100644 index 00000000..c6ab0c28 --- /dev/null +++ b/CarlosViniMSouza/0006/day06.py @@ -0,0 +1,15 @@ +# Why Python Is Great: +# Function argument unpacking + +def myfunc(x, y, z): + print(x, y, z) + + +tuple_vec = (1, 0, 1) +dict_vec = {'x': 1, 'y': 0, 'z': 1} + +myfunc(*tuple_vec) +# Output: 1, 0, 1 + +myfunc(**dict_vec) +# Output: 1, 0, 1 diff --git a/CarlosViniMSouza/0007/day07.py b/CarlosViniMSouza/0007/day07.py new file mode 100644 index 00000000..50e24a36 --- /dev/null +++ b/CarlosViniMSouza/0007/day07.py @@ -0,0 +1,19 @@ +# The "timeit" module lets you measure the execution +# time of small bits of Python code + +import timeit + +t1 = timeit.timeit('"-".join(str(n) for n in range(100))', number=100) +print(t1) + +# Output: 0.0013168999976187479 + +t2 = timeit.timeit('"-".join([str(n) for n in range(100)])', number=100) +print(t2) + +# Output: 0.0012954000012541655 + +t3 = timeit.timeit('"-".join(map(str, range(100)))', number=100) +print(t3) + +# Output: 0.0011196999985259026 diff --git a/CarlosViniMSouza/0008/day08.py b/CarlosViniMSouza/0008/day08.py new file mode 100644 index 00000000..806b97fe --- /dev/null +++ b/CarlosViniMSouza/0008/day08.py @@ -0,0 +1,25 @@ +# Why Python Is Great: +# In-place value swapping + +# Let's say we want to swap the values of a and b... +a = 23 +b = 42 + +""" +# The "classic" way to do it with a temporary variable: +tmp = a +a = b +b = tmp +""" + +# Python also lets us use this short-hand: +a, b = b, a + +print("", a, "\n", b) + +""" +Output: + +42 +23 +""" diff --git a/CarlosViniMSouza/0009/day09.py b/CarlosViniMSouza/0009/day09.py new file mode 100644 index 00000000..39602d87 --- /dev/null +++ b/CarlosViniMSouza/0009/day09.py @@ -0,0 +1,21 @@ +# "is" vs "==" + +a = [1, 2, 3] +b = a + +a is b +# Output: True + +a == b +# Output: True + +c = list(a) +a == c +# Output: True + +a is c +# Output: False + +# • "is" expressions evaluate to True if two variables point to the same object + +# • "==" evaluates to True if the objects referred to by the variables are equal diff --git a/CarlosViniMSouza/0010/day10.py b/CarlosViniMSouza/0010/day10.py new file mode 100644 index 00000000..e2b0c0bf --- /dev/null +++ b/CarlosViniMSouza/0010/day10.py @@ -0,0 +1,17 @@ +# Functions are first-class citizens in Python: + +# They can be passed as arguments to other functions, +# returned as values from other functions, and +# assigned to variables and stored in data structures. + +def sub(a, b): + return a - b + + +func = [sub] + +print(func[0]) +# Output: + +print(func[0](5, 7)) +# Output: -2