Python Arithmetic 2 – Solve Real-World Problems
Computers are used to solve complex problems in science, engineering, medicine, astronomy, etc. On Python Arithmetic 2 page, we will write programs that involve floating points.
We will use the arithmetic operators we learned in the previous Python Arithmetic chapter and apply them to solve problems that relate to our real-world experience.
For our first example, we will use the division (/) operator to calculate the time for the sunlight to reach earth at sunrise.
The second example introduces two temperature measurement units used in the world. We will write programs to convert temperature from Fahrenheit temperature units to Celsius temperature units and vice versa.
Python Arithmetic 2 (More Details)
Following up on our discussion in the Python Arithmetic chapter, this section discusses arithmetic in a little more detail. As a recap, the following examples show the result of using different arithmetic operators.
>>> print(17/3) #Division always results in floating point result.
5.666666666666667
>>> print(2**4) # 2 raised to the power 4 = 16.
16
>>> print(2**-2) # 2 raised to the power (-2) = 0.25
0.25
>>> print(17//3)
5
>>> # // operator gives the quotient and ignores the remainder.
>>> print(17%3) # % operator ignores the quotient and gives the remainder.
2
>>>
Integer Arithmetic
Let us use arithmetic operators to solve a daily observance phenomenon. When you get up every day to go to school in the morning, you see the sunlight.
Have you wondered about the time it takes for the sun’s rays to reach the earth at sunrise?
Let us write a Python program that calculates the time for the sun’s rays to reach the earth.
You already know that the speed of light is the fastest; nothing else can travel faster than light.
The speed of light is 186,000 miles per second.
In contrast, the fastest man-made object (a rocket that goes into space) travels at the speed of about 18,000 miles per hour. This speed is only about 5 miles per second!
So how do we calculate the time for the sun’s rays to reach the earth?
The time for the sun’s rays to reach the earth is calculated by dividing the sun-to-earth distance by the speed of light.
The sun-to-earth distance is 93 million miles. The Enrichment: Astronomy Related section at the end of the chapter on Data Types has the data about the planet’s distance from the earth.
Now we will write a Python program to calculate the time for the sun’s rays to reach the earth at sunrise.
We will define two variables:
- speed_of_light
- sun_earth_distance
and assign them values as below.
light_speed_miles_per_second = 186000 miles per second
sun_earth_distance-miles = 93000000 miles
We will find the time it takes for sun light to reach earth by dividing distance by speed.
time = sun_earth_distance-miles / light_speed_miles_per_second
PROGRAM EXAMPLE: INTEGER ARITHMETIC
>>> # Program to find out how long it takes for sun rays to reach earth.
>>> light_speed_miles_per_second = 186000
>>> sun_earth_distance_miles = 93000000
>>> time = sun_earth_distance_miles / light_speed_miles_per_second
>>> print('Time_for_sun_light_to_reach_earth = ', time , ' seconds')
Time_for_sun_light_to_reach_earth = 500.0 seconds
>>> # Divide by 60 to get time in minutes
>>> time_in_minutes = time_in_seconds/60
>>> print('Time in minutes for sun light to reach earth = ', time_in_minutes, 'minutes')
Time in minutes for sun light to reach earth = 8.333333333333334 minutes
>>> # Round the answer to two digits.
>>> round(time_in_minutes, 2) #1)
8.33
>>>
This program calculates that it takes 8.33 seconds for the sun’s rays to reach the earth.
Notice that Python gave the result with 15 digits after the decimal point. Let’s limit the result to a few digits. Statement 1) in the above program uses the round()
function that rounds the calculated result to two digits.
We will discuss the round()
function for rounding numbers in Python Functions 2 chapter.
Floating Point Arithmetic
In the previous section, we divided two integers to calculate the time for sunlight to reach the earth. In this section, we will write Python programs to solve arithmetic problems with floating point numbers (Data Type ‘float
’).
These programs use temperature measurement scales. Let us first review the temperature units that are used in the world to measure temperature.
Temperature Scales
Every morning, the meteorologist on TV reports the forecast of the temperature in your city during the morning weather forecast.
The temperature unit used in the United States is Fahrenheit. The Fahrenheit scale is a temperature scale based on one proposed in 1724 by Dutch–German–Polish physicist Daniel Gabriel Fahrenheit (1686–1736).
https://en.wikipedia.org/wiki/Fahrenheit
The temperature in Fahrenheit scale is written with the symbol °F. In this scale, the water freezing temperature is defined as 32°F, and the water boiling temperature is defined as 212°F. Therefore, there is a difference of 212 – 32 = 180 degrees between the water freezing point and the water boiling point.
Most other countries in the world officially use the Celsius temperature scale. This scale is named after Swedish astronomer Anders Celsius.
The Celsius scale is the most commonly used temperature scale by the scientific community worldwide. The temperatures in the Celsius scale are written with the symbol °C. In this scale, the boiling point of water is 100°C. The freezing point of water is 0°C. Therefore, there is a difference of 100 – 0 = 100 degrees between the water freezing point and the water boiling point.
Since two temperature units exist, there is a need to convert the temperature from one scale to the other.
Conversion of Temperature from Fahrenheit to Celsius Scale
The equation to convert temperature from Fahrenheit units to Celsius is:
TCelsius = (TFahrenheit -32) * (5/9)
PROGRAM EXAMPLE: FLOATING POINT PROGRAM – FAHRENHEIT TO CELSIUS
>>> # Convert temperature from Celsius to Fahrenheit temperature scale.
>>> # Define a variable tF for Fahrenheit temperature.
>>> # Define another variable tC for Celsius temperature.
>>> tF = 145 # Assign a value of 145 degrees to tF.
>>> # Convert Fahrenheit temperature to Celsius temperature.
>>> tC = (tF - 32)* (5/9)
>>> print(tF, 'degrees Fahrenheit temperature is =', tC, ' degrees Celsius')
145 degrees Fahrenheit temperature is = 62.77777777777778 degrees Celsius
>>> round(tC, 2)
62.78
>>>
Conversion of temperature from Celsius to Fahrenheit Scale
The program below converts the temperature from the Celsius scale to the Fahrenheit scale. The formula to convert from Celsius units to Fahrenheit units is:
TFahrenheit = Tcelsius* (9/5) +32
PROGRAM EXAMPLE: FLOATING POINT PROGRAM – CELSIUS TO FAHRENHEIT
>>> # Convert temperature from Celsius to Fahrenheit.
>>>
>>> # Define a variable tF for Fahrenheit temperature.
>>> # Define another variable tC for Celsius temperature
>>> tC = 89 # Assign a value of 89 degrees to Tc
>>> # Now convert Celsius temperature to Fahrenheit temperature
>>> tF = tC * (9/5) +32
>>> print(tC, 'degrees Celsius temperature is = ', tF, ' degrees Fahrenheit')
89 degrees Celsius temperature is = 192.20000000000002 degrees Fahrenheit
>>>
Enrichment: Temperature Scales
The German instrument maker Gabriel Daniel Fahrenheit (1686-1736) made the first reliable thermometers. The temperature scale he proposed is named after him. Born in Danzig on May 14, 1686, Gabriel Fahrenheit was the son of a well-to-do merchant. Daniel Gabriel Fahrenheit was a physicist, inventor, and scientific instrument maker. He turned to physics and became an instrument maker and glassblower. He lived in Amsterdam most of his life. He spent considerable time in England, where he became a member of the Royal Society.
Fahrenheit completed his first two thermometers by 1714. They contained alcohol. He soon decided to replace the alcohol with mercury. This temperature scale bears Fahrenheit’s name.
Anders Celsius was born on November 27, 1701 in Uppsala, Sweden. Anders Celsius was a Swedish astronomer, physicist and mathematician. He was professor of astronomy at Uppsala University from 1730 to 1744. He built the Uppsala Observatory. And he was the inventor of temperature scale named after him (Celsius temperature scale).
The following are the formulae for conversion of temperature from Fahrenheit to Celsius and vice versa.
US | WORLD | |
---|---|---|
Fahrenheit (°F) | Celsius (°C) | |
Water Boiling Point | 212°F | 100°C |
Water Freezing point | 32°F | 0°C |
Fahrenheit (°F) and Celsius (°C) Conversion
Fahrenheit to Celsius | °C = (5/9) * (°F – 32) |
Celsius to Fahrenheit | °F = (9/5) * (°C) + 32 |
In the next chapter, we will see how Python handles program flow control.
Copyright © 2019 – 2021 softwareprogramming4kids.com
All Rights Reserved