Skip to content

Instantly share code, notes, and snippets.

@paulmwatson
Created April 9, 2021 08:32
Show Gist options
  • Save paulmwatson/b76f7171e05b891f5ea1b48e7c5b3fd1 to your computer and use it in GitHub Desktop.
Save paulmwatson/b76f7171e05b891f5ea1b48e7c5b3fd1 to your computer and use it in GitHub Desktop.
Python assignment expressions (if/conditional assignments)
d = {'a': True, 'b': False}
# Without assignment expressions
if d['a']:
a = d['a']
print(a)
# => True
if d['b']:
b = d['b']
print(b)
# =>
# Using assignment expressions
if a := d['a']:
print(a)
# => True
if b := d['b']:
print(b)
# =>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment