Skip to content

Instantly share code, notes, and snippets.

@Chizaram-Igolo
Created December 15, 2020 14:22
Show Gist options
  • Save Chizaram-Igolo/0e40e6c75b57f18b39ed3808a3239205 to your computer and use it in GitHub Desktop.
Save Chizaram-Igolo/0e40e6c75b57f18b39ed3808a3239205 to your computer and use it in GitHub Desktop.
# After going through this Gist file you would have learnt:
#
# 1) The different types of arguments; Positional and Keyword
# and the differences between them.
# 2) How to use Positional and Keyword arguments singly and together.
# 3) How to specify default arguments.
# Functions are not limited to simple expressions. We can have conditional
# statements, comparison expressions and other kinds of control flow statements
# that we have in programs without functions as you'll see.
#--------Positional Arguments------------#
# Positional arguments are arguments that are passed in to the function
# based on the position of their defined parameters.
# The order in which they are passed is important.
def simple_invoice(item, price, quantity):
return f"{quantity} pcs of {item} was purchased for a total cost of \
{price * quantity}"
# If the line gets too long, you can break it with the line continuation
# character `\`.
# You can put the line continuation character in between a string statement,
# before or after an operand like: `+`, `-`, `and`, `or`, etc. and keywords
# like `if`, `while` but not inside the string formatting brackets `{}` above.
# It is a standard rule that to improve readability, line lengths in code
# should not exceed 80 characters.
print(simple_invoice("Bread", 400, 2))
# It is important in our example, that the item comes before the price and the
# price before the quantity or we get unintended results.
print(simple_invoice(400, "Bread", 2))
# output: 2 pcs of 400 was purchased for a total cost of BreadBread
# print(simple_invoice(2, "Bread", 400))
# output: 400 pcs of 2 was purchased for a total cost of BreadBreadBread...
# Remember that Python will print a string the number of times
# you multiply it by (e.g Bread * 400).
#--------Keyword Arguments and Default Arguments------------#
# Keyword arguments are arguments that are passed in to keywords.
# To have keyword arguments you must define the keyword in the
# parameter statements in the function definition.
#
# With keyword parameters, you can pass default values like this:
#
# par_name=default_value.
#
# This is known as a default argument and if a user does not provide a value
# for that parameter, the function will use this default value instead.
#
# This is useful in cases where we want to always have a value to work with
# whether or not a value gets passed to the function.
# In the following example, the first 2 parameters are given a default value
# of `None` and the last one is given a default value of 2.
def simple_invoice(item=None, price=None, quantity=2):
return f"{quantity} pcs of {item} was purchased for a total cost of \
{price * quantity}"
# When calling the function, order is not important if we specify the keyword.
# output: 2 pcs of Bread was purchased for a total cost of 800
print(simple_invoice(item="Bread", price=400, quantity=2))
# output: 4 pcs of Bread was purchased for a total cost of 1600
print(simple_invoice(price=400, item="Bread", quantity=4))
# 2 pcs of Bread was purchased for a total cost of 800
print(simple_invoice(item="Bread", price=400))
# Notice that regardless of the order in which we pass the arguments, we always
# get the correct result.
# Also, notice that the last function call didn't pass an argument to
# `quantity`, in this case the function simply used the
# default value of 2 for quantity.
# With keyword arguments, you are not mandated to put the keywords when calling
# the function, although putting the keywords is a good idea as it can help you
# understand the code better.
print(simple_invoice("Bread", 400, 2))
# The behaviour will default to positional argument in this case.
# Just remember to pass the arguments in the right order.
# You can use positional and default arguments together, the only caveats to
# watch out for are:
# i) Positional arguments come first.
# ii) Positional arguments must follow the order of their parameters,
# keyword arguments can still be passed in any other as far as their
# keywords are specified.
def simple_invoice(item_id, tax, item=None, price=None, quantity=2):
return f"{quantity} pcs of {item} was purchased for a total cost of \
{price * quantity} with tax of {tax}%"
print(simple_invoice(6742, 7, "Sugar", 500, quantity=4))
# Arguments for `item_id` and `tax` must come first always in the order their
# parameters were defined.
# "Sugar" and 500 have no keywords even though their parameters are keyword
# parameters. Because they are passed this way, they have to be
# passed in order like positional arguments and come before `quantity`.
print(simple_invoice(6845, 7, price=5, quantity=10, item="Paper"))
# Now because keywords are specified, the keyword arguments can be passed
# in any other.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment