8000 Add improvements as discussed with Phil by jslvtr · Pull Request #8 · tecladocode/complete-python-course · GitHub
[go: up one dir, main page]

Skip to content

Add improvements as discussed with Phil #8

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

Merged
merged 1 commit into from
Feb 18, 2020
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

# `before_and_after` is a higher-order function. That just means it's a function which has another function as a parameter.
def before_and_after(func): # func is a function passed
print("Before...")
func()
print("After...")


# greet, not greet(). That's because we're passing the function, not the result of calling the function.
before_and_after(greet)


# Another example


movies = [
{"name": "The Matrix", "director": "Wachowski"},
{"name": "A Beautiful Day in the Neighborhood", "director": "Heller"},
{"name": "The Irishman", "director": "Scorsese"},
{"name": "Klaus", "director": "Pablos"},
{"name": "1917", "director": "Mendes"},
]


def find_movie(expected, finder):
found = []
for movie in movies:
if finder(movie) == expected:
found.append(movie)
return found


find_by = input("What property are we searching by? ")
looking_for = input("What are you looking for? ")
movie = find_movie(looking_for, lambda x: x[find_by])
print(movie or 'No movies found.')

15 changes: 15 additions & 0 deletions course_contents/2_intro_to_python/lectures/14_enumerate/code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# https://blog.tecladocode.com/python-enumerate/

friends = ["Rolf", "John", "Anna"]

for counter, friend in enumerate(friends, start=1):
print(counter, friend)

# 1 Rolf
# 2 John
# 3 Anna


friends = ["Rolf", "John", "Anna"]
print(list(enumerate(friends))) # [(0, 'Rolf'), (1, 'John'), (2, 'Anna')]
print(dict(enumerate(friends))) # {0: 'Rolf', 1: 'John', 2: 'Anna'}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# In Python, functions are first class citizens.
# That means that, just like any other value, they can be passed as arguments to functions or assigned to variables.
# Here's a simple (yet not terribly useful) example to illustrate it:


def greet():
print("Hello!")


hello = greet # hello is another name for the greet function now.

hello()

# Let's move on to a more useful example.

# These don't _have_ to be lambdas. They could be normal functions too!
# I'm making them lambdas because they're really short.

avg = lambda seq: sum(seq) / len(seq)
total = lambda seq: sum(seq) # could just be `sum`
top = lambda seq: max(seq) # could just be `max`

students = [
{"name": "Rolf", "grades": (67, 90, 95, 100)},
{"name": "Bob", "grades": (56, 78, 80, 90)},
{"name": "Jen", "grades": (98, 90, 95, 99)},
{"name": "Anne", "grades": (100, 100, 95, 100)},
]

for student in students:
name = student["name"]
grades = student["grades"]

print(f"Student: {name}")
operation = input("Enter 'average', 'total', or 'top': ")

if operation == "average":
print(avg(grades))
elif operation == "total":
print(total(grades))
elif operation == "top":
print(top(grades))

# Here, you can see how we can store functions inside a dictionary—just as we could do with numbers, strings, or any other type of data.
# We're creating a dictionary of what would be user input to the function that we want to run in each case.

operations = {
"average": avg,
"total": total, # could just be `sum`
"top": top, # could just be `max`
}

# The `operations` dictionary could also be defined inline:

operations = {
"average": lambda seq: sum(seq) / len(seq),
"total": lambda seq: sum(seq), # could just be `sum`
"top": lambda seq: max(seq), # could just be `max`
}

# The rest of the code can make use of the `operations` dictionary

for student in students:
name = student["name"]
grades = student["grades"]

print(f"Student: {name}")
operation = input("Enter 'average', 'total', or 'top': ")
operation_function = operations[operation] # This means we don't need an if statement (but could get errors!)

print(operation_function(grades))

0