from collections import Counter
text = """
Never gonna give you up
Never gonna let you down
Never gonna run around and desert you
Never gonna make you cry
"""
words = text.lower().split()
for word, count in Counter(words).most_common():
bar = "🐍" * count
print(f"{word:8} {bar}")
def fahrenheit_to_celsius(fahrenheit: float) -> float:
return (fahrenheit - 32) * 5 / 9
and you understand it, don’t you?
avg = sum(numbers) / len(numbers)
You get the idea, right?
def cointoss(n,t):
return (lambda a:"\n".join(str(i)+":\t"+"*"*a.count(i) for i in range(min(a),max(a)+1)))\
([sum(randint(0,1) for _ in range(n)) for __ in range(t)])
After the course, I’d like you to be able to
Source: IBM training manual, 1979
Python is not simple, however. Simple ≠ easy
It’s not easy to master software engineering though
Checking whether a collection is empty
if items:
print("There are items")
if len(items) > 0:
print("There are items")
Iterating over a collection
for name in names:
print(name)
for i in range(len(names)):
print(names[i])
Building a list of squares
squares = []
for x in numbers:
squares.append(x * x)
squares = [x * x for x in numbers]
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!
import this