Using Functions to do Maths
February 9, 2018
Mathematics isn't really something I'm good at but luckily, Python makes it easy when using functions. I'm also using .format to correctly show decimal places - as well as # -*- coding: utf-8 -*- so that Python can print currency symbols..
# -*- coding: utf-8 -*-
def add(a, b, c):
print "\nADDING %d + %d + %d" % (a, b, c)
return a + b + c
price = add(12, 12, 12);print "The price is £{0:.2f}\n".format (price)
def multiply(a, b):
print "MULTIPLYING %d * %d" % (a, b)
return a * b
multiply_price = multiply(10, 10);print "The price is £{0:.2f}\n".format (multiply_price)
def subtracting(a, b):
print "SUBTRACTING %d - %d" % (a, b)
return a - b
subtract_price = subtracting(100, 50);print "The price is £{0:.2f}\n".format (subtract_price)
def division(a, b):
print "DIVIDING %d / %d" % (a, b)
return a / b
division_price = division(90, 2);print "The price is £{0:.2f}\n".format (division_price)
Recent Entries
- The World Before the Index April 1, 2026
- The Thinker and the Talker April 1, 2026
- Thirty-Three Million for a Suggestion Box April 1, 2026