This notebook covers the materials taught during the 2nd lecture (all of it).
input() - used for asking the user to provide an input. Tries to automaticallt understand whether the input is an integer, string, variable or anything else.
raw_input() - works in the same way, yet does not try to understand the input type. Instead, it automatically converts any input to a string.
The modulo (%) operator - outputs the residual. For example, 5%2 will output 1, as this is the residual after 5/2. Similarly, 6%2 is 0 (no residual), 6%3 is again 0, 6%4 is 2, 6%5 is 1 nd 6%6 is also 0.
Python is called object-orineted programming language, as everything you see there is an object (a data type). Being an object means it has its own unique functions and functionalities that can be implemented only on itself. During the 2nd class, we covered 3 objects with their own functions:
list - a collection of (any)elements (described in previous lecture notes)
string - a text object, which is always inside quptes ("" or '').
tuple - a list which is immutable, i.e. a list which's elements cannot be changed. Tuple is declared using () instead of []. For example, my_tuple = ('a','b','c'). *Note: tuples are introduced for yout own information, but will not be used troughout the course.
dataframe - a populat data presentation type which is not coming preinstalled in Python. We have met this type once we downloaded stock data using pandas_datareader.
head() - head is a function implemented on dataframes only which shows the top rows of the dataframe. For example. my_data.head(7) will show the top 7 rows of it, while my_data.head(3) will show top 3 rows only. By default, the head function shows top 5 rows, e.g. my_data.head() will show top 5 rows.
tail() - tail is very similar to head in everything with one crucial difference. Instead of showing top rows as head, tail is showing the bottom rows. E.g. my_data.tail(7) will show the very last 7 rows of a dataframe called my_data.
describe() - describe is another nice function available for dataframes, that can calculate and present its descriptive statistics.
this - a built-in Python library, which does not have any function inside. It is just developed to be imported to provide the philosophy of Python.
There are 3 building blocks that Python (as other programming languages) is based on. We already covered the for loop last time. The other two are if/else statement (similar to Excel if function) and while loop (quite similar to the for loop).
if/else - if/else statement is very similar to the if formula in Excel: it check a consition and provides some output if the condition is satisfied and provides some other output otherwise. However, compared to Excel if formula, this one is quite powerful, as you can add and check as many different conditions as you want (in Excel you would have to insert several IF formulas inside each other for the same purpose). For example, assume you are playing a game where a random number is generated between 1 and 100 (both included). If you get anything less than 50 you loose, else if you get exactly 50 you draw, and you win otherwise (anything more than 50). The Pythonic statement will look like this (assume that random number is saved in a variable called num):
if num < 50:
print("I lost :( ")
elif num==50:
print("Draw")
else:
print("I won !!! ")
There are several important things to note from the example above. As you can see on the first row we are checking whether num iss less than 50 or not. If it is, we print "I lost :(", but if this condition is wrong we just jump to the other one: As you may have noticed, similar to for loop, we have the colon (:) sign at the end of the "if row", which is mandatory to have. The 2nd condition is presented by elif which stand for else if. As you noticed we have double equal signs here. The thing is that one equal sign(=) is used to declare a variable with some value (e.g. a=5). As it is already use dfor this purpose, we have to use double equal sign to check whether the left hand side is equal to the right hand side or not (e.g. a==5 checks whether a is really equal to 5 or not). Thus, this is what we need to use in if or elif conditions. Last but not least, the if statement is finished (in this example) with else statement. The latter covers all other posibilities not covered by if and elif (in this case num > 50).
while - while loop is very similar to the for loop covered before. It is again autmoating the repetitive work, yet unlike for loop, while loop needs to have a limit to reach. In case of for, we repeate the same function/action for several times (thus, we exactly know the number of times to repeat), yet in case of while loop, we repeat the same action while a limit is reached. This means that the while loop can be an infinite loop if we do not take care of it reaching the limit. For example:
i=0
while i<5:
print(i)
i=i+1
The loop above will first print 0, then 1,2,3,4 and will stop as 5 is not less than 5. Importantthings to note here is that again, we need to have the colon sign (:) on the "while row". Unlike for, "i" variable here is not a temporary one, which means we have to first create it (i=0). Then we have to mention the limit (i<5) and in the end we need to incrementally increase i (i=i+1) so that it reaches the limit at some time. Note: if you do not increase it, for example in this case, then i will always stay 0 which is always less than 5. Thus, you will end up with an infinite loop.