Ints and Floats

While there are multiple formats to represent numerical values, this is an introductory course. We will primarily focus on two representations - Integers and Floats (Decimals point numbers).


In [ ]:
a = 1
print(a)
print(type(a))

In [ ]:
b = 2.0
print(b)
print(type(b))

Can we add an 'int' and a 'float'?


In [ ]:
c = a + b
print(c)
print(type(c))

In [ ]:
# How many kittens did the cat give birth to?
kittens = 3
print(type(kittens) )

In [ ]:
# What is the value of Pi?
val_pi = 3.142
print(type(val_pi))

In [ ]:
# Not an integer
hooli_founder = 30,000,000,000
bachmanity = 60,000,000,000
moolah = hooli_founder + bachmanity 
print(moolah)

In [ ]:
type(moolah)

Case Study 1: Baghead's Bag of Riches

Hooli has just offered Baghead a fantastic offer. He gets a salary, a bonus, vested equity, and free soda. Should he take it?


In [ ]:
salary = 500000
bonus = 0.75
equity = 1000000
vesting_period = 4

total_annual_value = salary*(1+bonus) + (equity/vesting_period)
print(total_annual_value)

In [ ]:
salary*(1+bonus)

In [ ]:
salary + (salary*bonus)

In [ ]:
equity/vesting_period

In [ ]:
print("Baghead's Yearly Offer is: $", total_annual_value)

Case Study 2: Peter Gregory's Profit

Peter analyzed cockroach samples at a restaurant in January, and bought 1,000,000 shares of a cooking oil company at \$200 per share. He sold his position in September at $450. Calculate his profit.


In [ ]:
close_jan = 200
close_sep = 450
return_rate = (close_sep - close_jan)/close_jan
print(return_rate)

Note: The above doesn't take into account slippage, cost of transaction, brokerage etc. It's just to illustrate mathematical operations in Python.

Recap of what you already know


In [ ]:
# ints are whole numbers
int1 = 1
int2 = 42
int3 = -27

In [ ]:
# floats are anything with a decimal point
float1 = 1.25
float2 = .496
float3 = 3.0

Converting between types


In [ ]:
type(float3)

In [ ]:
new_int = int(float3)
print(new_int)
print("The type is: ", type(new_int))

In [ ]:
# Time for revenge!
new_float1= float(int1)
print(new_float1)

Exercise

Convert the following:

  • 42 into float
  • 27.3 into an integer
  • The result of 99/36 into an integer

In [ ]:
# Enter your code below

Revisiting Mathematical Operations

  • +: plus to add numbers
  • -: minus, to subtract numbers, or assign a negative value (eg. -5, -25, -100 etc)
  • *: multiply numbers
  • /: divide number
  • %: modulo operator

In [ ]:
print("Addition")
print(1 + 1)
print(1.0 + 2.0)

In [ ]:
print("Subtraction")
print(100-50)
print(75.3-22.7)

In [ ]:
print("Multiplication")
print(2*5)
print(5.0 * 7.5)

In [ ]:
print("Division")
print(21 / 7)
print(22.0 / 7)

In [ ]:
# Another type of division
22.0//7

Also known as 'Floor Division'. Read about it here - https://docs.python.org/3.1/tutorial/introduction.html


In [ ]:
print("Modulo Operator")
print(5%2) # Prints 1, as 1 is the remainder when 5 is divided by 2
print(4%2) # Prints 0

Equality and Comparison Operators

  • ==: equality
  • <: less than
  • <=: less than or equal to
  • >: greater than
  • >=: greater than or equal to
  • !=: not equal to

In [ ]:
print("Equality")
print(2 == 2) # Will print True
print(2 == 1) # Will print False

In [ ]:
print("Comparison")
print(0 > 0) # False, since they are equal
print(1 > 0) # True, since 1 is greater than 0
print(1.0 < 1) # False, since numerically they are the same
print(2 >= 2) # True, since it checks for two conditions, greater than as well as equal to
print(2 <= 2) # True, since it checks for two conditions, less than as well as equal to
print(1 != 2) # True, since 1 is not equal to 2

Exercise

Create a tip calculator that takes into account the cost of food, sales tax, and tip.


In [ ]:
# Enter your code below:
food = xx
sales_tax = xx #Remember to enter in decimals. So 7.5% will be 0.075
tip = xx # Same as above, if you're calculating tip as percentage of food cost

tax_cost = food*(1 + sales_tax)
after_tip = tax_cost*(1 + tip)
print(after_tip)