Created
May 22, 2019 07:02
-
-
Save arsho/942de7920ac338cd3272f1b84a585a86 to your computer and use it in GitHub Desktop.
Example of New features in Python 3.8. Walrus operator and positional only parameters. #python3.8
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
### Walrush operator | |
data = ["some", "dummy", "values", "in", "this", "list"] | |
# Show the length of data if data has more than 5 values | |
if (data_length:=len(data)) > 5: | |
print(data_length) | |
# 6 | |
### Positional only parameters | |
def get_amount(price, tax_percentage, total_price=False, /): | |
tax_amount = price * (tax_percentage / 100.0) | |
if total_price: | |
return price + tax_amount | |
return tax_amount | |
item_price = 1699 | |
tax_percentage = 14.87 | |
tax_amount = get_amount(item_price, tax_percentage) | |
price_with_tax = get_amount(item_price, tax_percentage, True) | |
print(f"Tax amount: {tax_amount}") | |
print(f"Item price with tax: {price_with_tax}") | |
# Tax amount: 252.6413 | |
# Item price with tax: 1951.6413 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment