Let’s create a simple problem to solve with Python.
Our car’s gas tank can hold 12.0 gallons of gas. It gets 22.0 miles per gallon (mpg) when driving at 55.0 miles per hour (mph). If we start with the tank full and carefully drive at 55.0 mph, how many miles can we drive (total miles driven) using the whole tank of gas?
BONUS:
How long will it take us to drive all those miles?
What do we need figure out? We need a variable for our result: totalMilesDriven. So we start our program this way…
totalMilesDriven = 0 # print result of computation print(totalMilesDriven)
It’s often good to start with a very simple template. If we run that, we will see 0 (zero) as the result, right?
Next step, let’s add the variables we know.
totalMilesDriven = 0 totalHoursTaken = 0 totalGasGallons = 12.0 milesPerGallon = 22.0 milesPerHour = 55.0 # print result of computation print(totalMilesDriven)
Okay, good. We’ve added all the stuff we know about the computation. Well, except the part of the actual computation.
You probably know that if you multiply the milesPerGallon by the totalGasGallons, that will give you totalMilesDriven. And if you divide the totalMilesDriven by the milesPerHour, you will get the totalHoursTaken.
So let’s add those as Python statements.
totalMilesDriven = 0 totalHoursTaken = 0 totalGasGallons = 12.0 milesPerGallon = 22.0 milesPerHour = 55.0 totalMilesDriven = milesPerGallon * totalGasGallons totalHoursTaken = totalMilesDriven / milesPerHour # print result of computation print(totalMilesDriven, totalHoursTaken)
We get as a result 264 miles driven in 4.8 hours. And that’s how a simple Python program can get written.
Let’s do another.
A friend of ours is offering you a "free cat". You’re not allergic to cats but before you say yes, you want to know how much it’ll cost to feed the cat for a year (and then, approximately how much much each month).
We find out that cat food costs $2 for 3 cans. Each can will feed the cat for 1 day. (Half the can in the morning, the rest in the evening.) We know there are 365 days in a year. We also know that there are 12 months in the year. So how much will it cost to feed the cat for a year?
Looking at it, this may be quite simple. If we know each can feeds the cat for a day, we then know that we need 365 cans of food. So we can describe that as
totalCost = 0
cansNeeded = 365
costPerCan = 2.0 / 3.0
totalCost = cansNeeded * costPerCan # right?
monthsPerYear = 12
costPerMonth = totalCost / monthsPerYear
# print result of computation
print(totalCost, costPerMonth)What’s going to be the answer? [1] Run it in your Repl window to work it all out.
And let’s do one more.
A cell phone company charges a monthly rate of $12.95 and $1.00 a gigabyte of data. The bill for this month is $21.20. How many gigabytes of data did we use? Again, let’s use a simple template to get started.
dataUsed = 0.0
# print result of computation
print("total data used (GB)", dataUsed)Let’s add what we know: that the monthly base charge (for calls, and so on) is $12.95 and that data costs 1 dollar per gigabyte. We also know the monthly bill is $21.20. Let’s get all that written down.
dataUsed = 0.0
costPerGB = 1.0
monthlyRate = 12.95
thisBill = 21.20
# print result of computation
print("total data used (GB)", dataUsed)Now we’re ready to do the computation. If we subtract the monthlyRate from thisBill, we get the total cost of data. Then, if we divide the total cost of data by the cost per gigabyte, we will get the dataUsed.
dataUsed = 0.0
costPerGB = 1.0
monthlyRate = 12.95
thisBill = 21.20
totalDataCost = thisBill - monthlyRate
dataUsed = totalDataCost / costPerGB
# print result of computation
print("total data used (GB)", dataUsed)How many GBs of data did we use? Turns out to be 8.25 gigabytes.
Now if the bill was $24.00? How many GBs then? (go ahead, I’ll wait…) [2]