PYTHON LEARNING
When using Python as a calculator doing an input as:
#pint the sum of 7 and 10
print(7+10)
You will get a run code of:
17
Python can be used for quick calculations, new businesses you want to develop a database-driven
website and for when you need to clean and analyze results of surveys.
When entering in Python Script:
#Division
print(5 / 8)
#Addition
print(7 + 10)
You will get a run code of:
0.625
17
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 is 4 to the power of 2 and 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 because 7 fits into 18 twice,
leaving you with a remainder of 4.
Python can also be used to save variables for later use. For example if you input height as 1.79
meters and weight as 68.7 kg you can assign these values to their respective meaning using an
equal sign.
Height = 1.79
Weight = 68.7
Then to get the BMI calculation BMI = weight/height^2 you can do the following:
PYTHON LEARNING
68.7 / 1.79 ** 2
21.4413
Weight / height ** 2
21.4413
BMI = weight / height ** 2
BMI
21.4413
Here Python will now store the result in a new variable, BMI.
So far, we’ve only worked with numerical values, such as height and weight. In Python, these
numbers all have a specific type. You can check out the type of a value with the type function.
To see the type of our BMI value, simply write type and then BMI inside parenteses.
Type(bmi)
float
As seen above you can see that using float is python’s way of representing a real number, so a
number which can have both an integer part and a fractional part. Python also has a type for
integers: int, like this:
Day_of_week = 5
PYTHON LEARNING
Type(day_of_week)
Int
To do data science, you’ll need more than ints and floats. Python features tons of other data
types. The most common ones are strings and booleans. A string is Python's way to represent
text. You can use both double and single quotes to build a string:
X = “body mass index”
Y = ‘this works too’
If you print the type of the last variable:
X = “body mass index”
Y = ‘this works too’
Type (y)
Str
You see that it’s str, short for “string”. The Boolean is a type that can either be True or False:
Z = True
Type (z)
bool
PYTHON LEARNING
You can think of this as ‘Yes’ and ‘No’ in everyday language. Booleans will be very useful to
perform filtering operations on data.
Have a look at the following two lines of code, one sums two integers while the other sums two
strings:
2+3
‘ab’ + ‘cd’
‘abcd’
For the integers, the values were summed, while for the strings, the strings were pasted
together. The plus operator behaved differently for different data types. This is a general
principle: how the code behaves depends on the types you’re working with.
The formula to calculate how much money you have after 7 years of investing $100 each year
with 10% return would look something like this:
100 * 1.1 ** 7
This is known as compound interest.
Instead of calculating with the actual values, you can use variables instead. The savings
variable you've created in the previous exercise represents the $100 you started with
Savings = 100
Growth_multiplier = 1.1
Result = savings * growth_multiplier ** 7
Print(result)
194.87171000000012
A list can be used in Python instead of doing a new variable for each thing. For example
PYTHON LEARNING
Height1
Height2
Height3
Is considered inconvenient instead do:
[a, b, c]
[1.73, 1.68, 1.71]
Fam = [1.73, 1.68, 1.71] -> this is a way to reference the data structure, put a variable Fam in
front of an equal sign so it can be brought up later.
A list can contain different types (int, float, str, bool) for example:
Fam = [“liz”, 1.73, “emma”, 1.68, “mom”, 1.71]
Lists can also contain lists. For example:
Fam2 = [[“liz”, 1.73], [“emma”, 1.68], [“mom”, 1.71]]
Fam2 printed would look like:
[[‘liz’, 1.73], [‘emma’, 1.68], [‘mom’, 1.71]]
Both Fam and Fam2 are list types.
Subsetting lists -> each list has an index in the list which start at zero. For example:
Fam = [“liz”, 1.73, “emma”, 1.68, “mom”, 1.71]
Fam
[‘liz’, 1.73, ‘emma’, 1.68, ‘mom’, 1.71]
If you wanted to grab the height of emma for example:
Fam[3]
1.68
List Slicing -> when you want to grab two points of data in a list. For example:
Fam
[‘liz’, 1.73, ‘emma’, 1.68, ‘mom’, 1.71]
Fam[3:5]
[1.68, ‘mom’]
The reasoning is the syntex is [start : end] the index you specify before the colon is included
whereas the index you specify after the colon is not included.
When using an list slice like:
Fam[:4]
[‘liz’, 1.73, ‘emma’, 1.68]
Or:
Fam[3: ]
[1.68, ‘mom’, 1.71]
PYTHON LEARNING
Leaving a blank before the colon grabs the first index and up to the index before the given
number. Leaving a blank after the colon grabs the index from before the colon up until the last
index in the list.