# Doing Math with Python!

Welcome to my first post! In order to get comfortable with Jekyll, Markdown and writing for a tech blog which is totally new to me, I shared two programs below. The first bit of code ia a simple unit converter and the later a menu driven fraction operator which does just that, fraction operations. I’ve been reading ‘Doing Math with Python’ by Amit Saha and his unique approach to teaching coding combined with micro projects are really insightful. Here is my code for the fraction operations program detailed in chapter one of Amit Saha’s book, Doing Math with Python:

```
'''
Fraction operations
'''
from fractions import Fraction
def main_menu():
print(' ')
print('1. Addition')
print('2. Subtraction')
print('3. Division')
print('4. Multiplication')
print(' ')
def add(a, b):
print('Result of Addition: {0}'.format(a+b))
def sub(a, b):
print('Result of Subtraction: {0}'.format(a-b))
def divi(a, b):
print('Result of Division: {0}'.format(a/b))
def multi(a, b):
print('Result of Multiplication: {0}'.format(a*b))
if __name__ == '__main__':
a = Fraction(input('Enter first fraction: '))
b = Fraction(input('Enter second fraction: '))
main_menu()
choice = input('Chose your operator: ')
if choice == '1':
add(a, b)
if choice == '2':
sub(a, b)
if choice == '3':
divi(a, b)
if choice == '4':
multi(a, b)
```

Here is the code for the unit converter that was really illuminating, also from chapter one:

```
'''
multi_unit_converter.py
Unit converter menu:
Inches and Meters
Miles and Kilometers
Kilograms and Pounds
Celsius and Fahrenheit
'''
def main_menu():
print(' ')
print('1. Inches to Meters')
print('2. Meters to Inches')
print('3. Kilometers to Miles')
print('4. Miles to Kilometers')
print('5. Kilograms to Pounds')
print('6. Pounds to Kilograms')
print('7. Celsius to Fahrenheit')
print('8. Fahrenheit to Celsius')
print(' ')
def km_miles():
km = float(input('Enter Kilometers to be converted to Miles: '))
miles = km / 1.609
print('{} Kilometers = {} Miles.'.format(miles,km))
def miles_km():
miles = float(input('Enter Miles to be convetred to Kilometers: '))
km = miles * 1.609
print('{} Miles = {} Kilometers.'.format(km,miles))
def kg_pounds():
kg = float(input('Enter Kilograms to be converted to Pounds: '))
pounds = kg * 2.20462
print('{} Lbs = {} Kg.'.format(pounds,kg))
def pounds_kg():
pounds = float(input('Enter Pounds to be converted to Kilograms: '))
kg = pounds / 2.20462
print('{} Kg = {} Lbs.'.format(kg,pounds))
def cel_far():
cel = float(input('Enter Celcius to be converted to Farenheit: '))
far = (cel * 9/5) + 32
print('{} C = {} F.'.format(cel,far))
def far_cel():
far = float(input('Enter Farenheit to be converted to Celcius: '))
cel = (far - 32) * 5/9
print('{} F = {} C.'.format(far,cel))
def in_meters():
inches = float(input('Enter inches to be converted to Meters: '))
meters = inches / 2.54
print('{} inches = {} meters.'.format(inches,meters))
def meters_in():
meters = float(input('Enter meters to be converted to Inches: '))
inches = meters * 2.54
print('{} meters = {} inches.'.format(meters,inches))
if __name__ == '__main__':
main_menu()
choice = input('Which conversion do you prefer?": ')
if choice == '1':
in_meters()
if choice == '2':
meters_in()
if choice == '3':
km_miles()
if choice == '4':
miles_km()
if choice == '5':
kg_pounds()
if choice == '6':
kg_pounds()
if choice == '7':
cel_far()
if choice == '8':
far_cel()
```

I chose these two python examples from the book because they stood out to me as good representations of what you might expect when working through this book. The sequence of and obvious use of ‘if’ statements, functions and a print menu option, just made sense and instantly gave me ideas for other projects. If you see any mistakes or maybe you have suggestions, pleasee leave a comment below.

Sources: Saha, Amit (August 2015) Doing Math with Python. San Francisco, CA: No Starch Press.