Python
Python as a calculator
Python is perfectly suited to do basic calculations. Apart from addition, subtraction, multiplication and division, there is also support for more advanced operations such as:
Exponentiation: **. This operator raises the number to its left to the power of the number to its right. For example 4**2 will give 16.
Modulo: %. This operator returns the remainder of the division of the number to the left by the number on its right. For example 18 % 7 equals 4.
The code in the script on the right gives some examples.
Instructions
Suppose you have $100, which you can invest with a 10% return each year. After one year, it’s 100×1.1=110100×1.1=110 dollars, and after two years it’s 100×1.1×1.1=121100×1.1×1.1=121. Add code on the right to calculate how much money you end up with after 7 years.
SOLUTION
# Addition and subtraction
print(5 + 5)
print(5 - 5)
# Multiplication and division
print(3 * 5)
print(10 / 2)
# Exponentiation
print(4 ** 2)
# Modulo print
(18 % 7)
# How much is your $100 worth after 7 years?
print(100 * 1.1 ** 7)
Python is perfectly suited to do basic calculations. Apart from addition, subtraction, multiplication and division, there is also support for more advanced operations such as:
Exponentiation: **. This operator raises the number to its left to the power of the number to its right. For example 4**2 will give 16.
Modulo: %. This operator returns the remainder of the division of the number to the left by the number on its right. For example 18 % 7 equals 4.
The code in the script on the right gives some examples.
Instructions
Suppose you have $100, which you can invest with a 10% return each year. After one year, it’s 100×1.1=110100×1.1=110 dollars, and after two years it’s 100×1.1×1.1=121100×1.1×1.1=121. Add code on the right to calculate how much money you end up with after 7 years.
SOLUTION
# Addition and subtraction
print(5 + 5)
print(5 - 5)
# Multiplication and division
print(3 * 5)
print(10 / 2)
# Exponentiation
print(4 ** 2)
# Modulo print
(18 % 7)
# How much is your $100 worth after 7 years?
print(100 * 1.1 ** 7)
Post Comment
No comments