4.7 KiB
1.3 Numbers
This section covers some basics of performing mathematical calculations in Python.
Reading
Types of Numbers
Python has 4 types of numbers:
- Booleans
- Integers
- Floating point
- Complex (imaginary numbers)
Booleans
Booleans have two values: True, False.
a = True
b = False
Numerically, they're evaluated as integers with value 1, 0.
c = 4 + True # 5
d = False
if d == 0:
print('d is False')
Don't do that, it would be odd.
Integers
Signed values of arbitrary size and base:
a = 37
b = -299392993727716627377128481812241231
c = 0x7fa8 # Hexadecimal
d = 0o253 # Octal
e = 0b10001111 # Binary
Common operations:
x + y Add
x - y Subtract
x * y Multiply
x / y Divide
x // y Floor Divide
x % y Modulo
x ** y Power
x << n Bit shift left
x >> n Bit shift right
x & y Bit-wise AND
x | y Bit-wise OR
x ^ y Bit-wise XOR
~x Bit-wise NOT
abs(x) Absolute value
Comparisons
The following comparison / relational operators work with numbers:
<, >, <= >=, ==, !=
You can form more complex boolean expressions using
and, or, not
Here are a few examples:
if b >= a and b <= c:
print('b is between a and c')
if not (b < a or b > c):
print('b is still between a and c')
Floating point (float)
Use a decimal or exponential notation to specify a floating point value:
a = 37.45
b = 4e5 # 4 x 10**5 or 400,000
c = -1.345e-10
Floats are represented as double precision using the native CPU representation IEEE 754.
17 digits or precision Exponent from -308 to 308
This is the same as the double type in the programming language C.
Be aware that floating point numbers are inexact when representing decimals.
>>> a = 2.1 + 4.2
>>> a === 6.3
False
>>> a
6.300000000000001
>>>
This is not a Python issue, but the underlying floating point hardware on the CPU.
Common Operations:
x + y Add
x - y Subtract
x * y Multiply
x / y Divide
x // y Floor Divide
x % y Modulo
x ** y Power
abs(x) Absolute Value
Theses are the same operators as Integers, except for the bit-wise operators.
Additional math functions are found in the math module.
import math
a = math.sqrt(x)
b = math.sin(x)
c = math.cos(x)
d = math.tan(x)
e = math.log(x)
Converting Numbers
The type name can be used to convert values:
a = int(x) # Convert x to integer
b = float(x) # Convert x to float
Try it out.
>>> a = 3.14159
>>> int(a)
3
>>> b = '3.14159' # It also works with strings containing numbers
>>> float(b)
3.15159
>>>
Exercise 1.3
(a) Dave's mortgage
Dave has decided to take out a 30-year fixed rate mortgage of $500,000 with Guido’s Mortgage, Stock Investment, and Bitcoin trading corporation. The interest rate is 5% and the monthly payment is $2684.11.
Here is a program that calculates the total amount that Dave will have to pay over the life of the mortgage:
# mortgage.py
principal = 500000.0
rate = 0.05
payment = 2684.11
total_paid = 0.0
while principal > 0:
principal = principal * (1+rate/12) - payment
total_paid = total_paid + payment
print('Total paid', total_paid)
Enter this program and run it. You should get an answer of 966,279.6.
(b) Extra payments
Suppose Dave pays an extra $1000/month for the first 12 months of the mortgage?
Modify the program to incorporate this extra payment and have it print the total amount paid along with the number of months required.
When you run the new program, it should report a total payment of 929,965.62 over 342 months.
(c) Making an Extra Payment Calculator
Modify the program so that extra payment information can be more generally handled.
Make it so that the user can set these variables:
extra_payment_start_month = 60
extra_payment_end_month = 108
extra_payment = 1000
Make the program look at these variables and calculate the total paid appropriately.
How much will Dave pay if he pays an extra $1000/month for 4 years starting in year 5 of the mortgage?
(d) Making a table
Modify the program to print out a table showing the month, total paid so far, and the remaining principal.
The output should look something like this:
1 2684.11 499399.22
2 5368.22 498795.94
3 8052.33 498190.15
4 10736.44 497581.83
5 13420.55 496970.98
...
308 875705.88 674.44
309 878389.99 -2006.86
Total paid 878389.99
Months 309
(e) Bonus
While you’re at it, fix the program to correct the for overpayment that occurs in the last month.