Skip to content

Instantly share code, notes, and snippets.

@KianYang-Lee
Last active February 5, 2022 10:39
Show Gist options
  • Save KianYang-Lee/4aadc50310de76137da18fd15e1358d9 to your computer and use it in GitHub Desktop.
Save KianYang-Lee/4aadc50310de76137da18fd15e1358d9 to your computer and use it in GitHub Desktop.
Demonstrating the difference between using walrus operator and not using walrus operator with simple example
#!/usr/bin/python3
# Prior to Python 3.8
# Assigning a value to variable and then evaluating the expression in different lines
python_version = 3.7
print(f'Python version with walrus operator? : {python_version >= 3.8}')
print(f'Applicable for Python version: {python_version}')
# Reset variable
python_version = None
# Python 3.8
# Both evaluating the expression and then assign the value to the variable in the same line
print('Python version with walrus operator? :', (python_version := 3.8) >= 3.8)
print(f'Applicable for Python version: {python_version}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment