by github.com/MrValdez
- What are f-strings? A Python 3.6 superpower
- Python 3 installation that is helpful for complete beginners.
- pip (3.4), virtualenv, and path installation
- More helpful chained exceptions
- The Windows py launcher
- Ordered dictionary (3.6+)
- CPython implementation (3.6)
- Language implementation (3.7)
- https://stackoverflow.com/q/39980323/1599
- Keyword only arguments (3.0+) and Positional only arguments (3.8+)
hero = "Kardo Dalisay"
food = ["Pizza", "Chicken", "Rice"]
secret = 42
# < Python 3.6
#template ="{} is so cool. They like {}. Their password is {}."
#output = template.format(hero, food, secret)
# With the f-string superpower
output = f"{hero} is so cool. They like {food}. Their password is {secret}."""
print(output)
# Another example, showing function calling, indexing, and integer grouping.
#secret = 42 * 55 * 1000
#output = f"""{hero.upper()} is so cool.
#They like {food[0]}.
#Their password is {secret:,}."""
# Another example showing that join works in f-strings
#output = f"""{hero.upper()} is so cool.
#They like {", ".join(food[:-1])} and {food[-1]}.
#Their password is {secret:,}."""
#print(output)
def make_spam():
def make_more_spam():
raise TypeError
try:
make_more_spam()
except TypeError:
raise IndexError
make_spam()
Without running, what do you think is the raised exception? TypeError? or IndexError?
Answer: If you're in Python 3, the answer is all of the above.
$ py
$ py --2.7
$ py --3.7
def unlock(username, password, /, *, secret):
if secret:
password = len(password) * "*"
print(f"Your username is {username}. Your password is {password}")
#unlock("admin", "12345", True)
#unlock("admin", "12345", secret=True)
#unlock(username="admin", password="12345", secret=True)
unlock("admin", password="12345", secret=True)
>>> print("Hello")
Hello
>>> print(type(print))
<class 'builtin_function_or_method'>
>>> def save_to_disk(line):
... with open("foobar.txt", "w") as f:
... f.write(line)
>>> print = save_to_disk
>>> print("Hello world")
print("♠♥♦♣")
なに = "WHAT??"
print(なに)
Reminder: with great power, comes great responsibility. Unicode variables is a great power.
not a good idea to have unicode variables.