Introduction to Python 1.1

This notebook provides introduction to Python programming language by summarizing the data types, operators, functions and several important points covered during the 1st class.

Note: as of date, Python is one of the most popular and demanded programming languages with its applications especially in Data Science and Aritificial Intelligence.

Running the Jupyter Notebook

While Python is the programming language we will use, Jupyter notebook is the software (the environment) where we will code. In other words, Jupyter Notebook is the program that understands the language of Python. To run one has to open the Command Prompt (called Terminal in Mac) and type the following command: jupyter notebook. Afterwards the Notebook screen must automatically appear (after some time) in the default browser tab.

Note: if for some reasons your command prompt tells you that the command jupyter notebook was not recognized, then just search for Anaconda prompt instead, open it and type and run the same jupyter notebook command there.

Data types covered

Data/variables created/developed in Python environment can be provided in different types. There key data types that we have already learnt about are the following:

  • String (str) - the type used for text variables. Text in Python is always provided inside double or single quotes ("" or '').

  • Integer (int) - the type used for variables with integer values

  • Float (float) - the type used for variables with both integer and noninteger values (i.e. float is a more general type than int)

While the above-provided types can be used separately, one can also combine them inside one single data type. List is a consolidated data type, which is itself a list of elements, where elements can be strings, integers, floats or even lists. In Python lists are created using square brackets ([]).

Operators covered

The multiplication is implemented by the * operator, while the division, summation and substraction are implemented by /, + and - operators respectively. Key things to note are:

  • "+" - the summation operator, can be used for integers/floats to implement the same summation function as in mathematics generally (e.g. 5 + 2 = 7). It can also be used to sum up (concatenate) strings (e.g."Hrant" + "Davtyan" = "HrantDavtyan"). Summation operator can be used to append one list to another too (e.g. [1,2,3] + [4,5,6] = [1,2,3,4,5,6])
  • "/" - the division operator, can be used to divide one integer/float to another. Important thing to remember here is that by default Python is doing integer division, i.e. 5/2 = 2 (as 2 is the integer component of 2.5). To tell Python explicitly that you are interested in "usual" division with decimals you have to convert your integers to decimals (e.g. for our case it will be 5.0/2 = 2.5 as 5.0 is already float, yet has the same value as the integer 5).

Functions covered

  • print() - prints the value of an argument, e.g. print(5+7) will print 7.

  • len() - provides the length of the argument, e.g. len("Hrant") will output 5, because the string "Hrant" has 5 characters.

  • range() - creates a list of numbers starting from 0 (included) and ending with the number given as an argument (not included), e.g. range(5) will create the following list: [0,1,2,3,4].

  • float(), str(), int() - convert the argument into float, string or an integer respectively.

  • type() - provide the type of the argument, e.g. type("Hrant") will output str as the argument is string.

for loop

for loop is one of the 3 building blocks of any programming language (the other 2 are are if/else statement and while loop). It has a key importance, because it can automate stuff for you. To understand it let's see an example.

for i in range(3):
    print(i)

Let's explain the for loop above starting from the very end first. The last thing we have is the print() function, which means the task we want to implement is printing. for loop provides the opportunity to automate this task, i.e. print several times. range(3), as you already know, creates a list of 3 numbers as follows: [0,1,2]: i is just a temporarely variable which gets the value 0 first (as it is the first value in our range() function), then becomes 1 and then becomes 2. This means that for i in range(3) (which is to say for i=0, for i=1 and for i=2) we want to print(i) (respectively it will print 0, 1 and 2). What is important to notice is that we have the colon guy in the first row (where for loop is starting) and the 2nd row stars with 4-space-indentation.

Important notes

Enumeration in Python starts from 0. This means that if you have a list of names say ["Jack","James","Jimmy"] than Jack will be the 0th element, James the 1st and Jimmy the 2nd. It is important to understand that altough the enumeration starts from 0, the length of this list is still 3.