Skip to content

Instantly share code, notes, and snippets.

@arsho
Created May 22, 2019 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arsho/942de7920ac338cd3272f1b84a585a86 to your computer and use it in GitHub Desktop.
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
### 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