Python Learning
Python Learning
print(7+10)
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.
#Division
print(5 / 8)
#Addition
print(7 + 10)
0.625
17
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
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:
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
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]]
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]
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.
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.