Skip to content

Instantly share code, notes, and snippets.

@MrValdez
Last active May 8, 2020 11:05
Show Gist options
  • Save MrValdez/4ec4838f1cff46b2374da91773e6e85b to your computer and use it in GitHub Desktop.
Save MrValdez/4ec4838f1cff46b2374da91773e6e85b to your computer and use it in GitHub Desktop.
I gave a talk at our @pythonph meetup. Here is a summary.

Six great things about Python 3

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+)
  • Keyword only arguments (3.0+) and Positional only arguments (3.8+)

What are f-strings? A Python 3.6 superpower

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)

More helpful chained exceptions

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.


The Windows py launcher

$ py

$ py --2.7

$ py --3.7


Keyword only arguments (3.0+) and Positional only arguments (3.8+)

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)

Bonus: Print as a function

>>> 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")

Bonus: Unicode in Python

print("♠♥♦♣")

なに = "WHAT??"
print(なに)

Reminder: with great power, comes great responsibility. Unicode variables is a great power.

@sanfx
Copy link

sanfx commented Apr 17, 2020

not a good idea to have unicode variables.

@MrValdez
Copy link
Author

not a good idea to have unicode variables.

You're right on that. Although, I only included unicode variable as something that can be done if really needed.

When I gave this talk, I presented the unicode variables in a tongue-in-cheek way. I was hoping the italics convey that on text. I guess it failed. I'll think of a different way to phase these kind of jokes in text better. Sorry for the confusion.

@sanfx
Copy link

sanfx commented May 8, 2020

I am referring to python3 for adding unicode variable support. Already, I see lot of undocumented code and now we might have to deal with people of non-englishing trying to write code in their own language making another level of confusion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment