Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added folder = CarlosViniMSouza -> 10 programs .py #419

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CarlosViniMSouza/0001/day01.py
Original file line number Diff line number Diff line change
@@ -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)]
21 changes: 21 additions & 0 deletions CarlosViniMSouza/0002/day02.py
Original file line number Diff line number Diff line change
@@ -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!"
18 changes: 18 additions & 0 deletions CarlosViniMSouza/0003/day03.py
Original file line number Diff line number Diff line change
@@ -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)
31 changes: 31 additions & 0 deletions CarlosViniMSouza/0004/day04.py
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions CarlosViniMSouza/0005/day05.py
Original file line number Diff line number Diff line change
@@ -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))
15 changes: 15 additions & 0 deletions CarlosViniMSouza/0006/day06.py
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions CarlosViniMSouza/0007/day07.py
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions CarlosViniMSouza/0008/day08.py
Original file line number Diff line number Diff line change
@@ -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
"""
21 changes: 21 additions & 0 deletions CarlosViniMSouza/0009/day09.py
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions CarlosViniMSouza/0010/day10.py
Original file line number Diff line number Diff line change
@@ -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: <function sub at 0x000001F907895900>

print(func[0](5, 7))
# Output: -2