Last active
February 5, 2022 10:39
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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