Using Functions to do Maths
February 09, 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
- Wonder 2 Finally Shows Some Restraint January 25, 2026
- Forty-Five Bugs Hiding in Plain Sight January 23, 2026
- Cleaning the Metadata January 21, 2026