2) a. Numbers

A quick overview on the data types in Python

Integers

Integer is one of the most widely used data types in Python. An integer variable holds a value of a whole number without a decimal part and it can be positive or negative.

1
2
3
Output:
Sum =  5
Multiplication =  -500



You can find the power of an integer by using the ** operator. A single asterisk is multiplication. A double asterisk is power.

While dividing numbers, you can get the remainder with % and the divisor with //.

1
2
3
4
Output:
If I distribute 10 doughnuts among 4 people, each of them gets 2 doughnuts.
At the end I should have 2 doughnuts still remaining.
A $1000 loan taken at 3.75% compound interest for 10 years will cost you $ 1445.04394263



Floats

Float variables hold a value of a number that has a decimal part and it can be positive or negative. Floats have limited precision which depends on the hardware capability of the computer.

Any mathematical equation that involves a float value will always result in float.

1
2
3
4
Output:
0.8
1.0
10.0


It is worth noting that if you divide an integer by another integer the result will always be a float.

1
2
3
4
Output:
2.0
0.5
2.0



How do you determine what type of value is stored in a variable? You can do this using the type built-in.

1
2
3
Output:
unknown_var1 =  <type 'int'>
unknown_var2 =  <type 'float'>