Introductory Notes to Python

Finally we can also make use of boolean operations, but this only could be done between booleans (like the name say it).

So we can have something like thisresult_and = example_bool_one and example_bool_two # Falseresult_or = example_bool_one or example_bool_two # Trueresult_not = not example_bool_one # FalsePlease note that we use the word and, or, not to make use of the operations, inside the Python language, those are know as keywords or reserved words.

So we have the rule that we can not used them as identifiers of our variables.

Knowing these basics we can move further to a little more complex knowledge, but fear not, you will get it.

Data StructuresAccording to Wikipedia:A data structure is a data organization, management and storage format that enables efficient access and modification.

More precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data.

In other words, a data structure is just a group of data values with some relation between them.

In Python we can find the following:Strings: Those are a simple group of characters, we have already used them before.

So it should be anything new for you.

We can initialize them using ' or "and close it in the same way.

(Like we did when declaring the table’s material!).

As example consider thisname = 'My name'blog = "Acquisition of Learning" Lists: This is a group of different values, that could be (but it is not recommended) of different types.

To declare a list we just need to make use of these symbols [] and separate every item with a comma.

Also we can access one specific value by using its index inside the list.

Remember, we start at the position zero inside a list.

We can change the value inside a list by using the index as well.

Consider the following as examplelist_one = [1, 2, 3, 4, 5]list_two = ['a', 'b', 'c', 'd']print(list_one[0]) # This will print the first item, 1print(list_two[0]) # This will print the first item, alist_one[0] = 0print(list_one[0]) # This will print the first item, 0Tuples: The tuples are like list, but we can not change any value, and the initialization of tuple is using commas.

We can access one element from a tuple in the same way the did with the lists.

tuple_one = 1, 3, 5, 7print(tuple_one) # This will print "(1, 3, 5, 7)"print(tuple_one[0]) # This will print the first item, 1Dictionaries: A dictionary is a data structure that works using pairs, key and value.

We can access the value by calling the key.

Keep in mind that key are unique so if you call twice the same key you will overwrite the value.

They can initialize using {}.

And we can access to some specific index or add a new pair using the symbols [], specifying the key and value.

dictionary_one = {'a': 1, 'b': 2, 'c': 3, 'd': 4}print(dictionary_one['a']) # This will print 1dictionary_one['e'] = 5 # This will add a new pairData Frames: These kind of structure is the most command and useful when we are working with data.

Data Frames are kind of table so making queries and some transformation to them is really easy.

To make use of them, we need to install Pandas and Numpy.

Later in these post I will give a brief introduction to how to install libraries, but for now just have in mind that you need to install them.

Consider the following as an example to the declaration of Data Frames inside Python.

import pandas as pdcolumns = ['C1', 'C2', 'C3', 'C4']data = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]df = pd.

DataFrame(data, columns = columns)Sometimes we need to look inside every item inside a data structure, and to do this we will need to learn about loops.

Conditions and LoopsFirst we need to have a clear idea of what a conditions is.

A conditions is just a statement that could be true or false, and depending of it we can proceed to different parts and do something different with our variables.

For example, we can say if the table less than 2 years old we will consider it as new, otherwise it will not.

if age <= 2: is_new = Trueelse: is_new = FalseNote that we are using boolean operators, and that we are using the keywords if, else there is also another keyword elif that will be evaluated if the first if is false.

This last keyword can have a boolean statement, while else part do not have a statement, because it will be done only if the others conditions are false.

Now let’s consider the term loop.

When we want to check or do something with the variables inside a data structure, if it is iterable, we can make use of a loop to check every single value.

For a loop we can use for loops or while loops.

The difference between those is simple, a for loop will iterate every value inside a iterable util it reach a end.

On the other hand, the while loop will continue until a given condition results as false, so we have to keep in mind that while loops will make use of a boolean statement explicit declared.

for i in range(10): print(i) # This will print numbers from 0 to 9a = 0while(a < 10): print(a) # This will print numbers from 0 to 9 a = a + 1while(True): print("Hello World!") # Will print the phrase until you stop itCheck that in the first for we used range() function, this is just a simple function that will generate a list of numbers from 0 (default value) to 9, the number specified is not included.

When we use for loop the increase of the variable is not necessary, on the other hand while need it, because if not it will generate a infinite loop, like the one showed in the last part of the example.

So if we want to iterate over a list we can do it like thisfor element in list_one: print(element) # This will print the number 1 to 5 inside listfor index in range(len(list_one)): print (list_one[index]) # This will print the numbers as wellNotice that in the first part of the last example we did not use range function, because list_one is a list, and list are iterable.

Differently, in the last part we make use of range() function but we need another function called len() function, that will give us the size of the list, so you can notice that the result of len() will be a number and in this way is similar to the previous example.

LibrariesAs I mentioned early, Python has a huge community, and this is one of the main reason that gives Python a lot of libraries that are ready to use.

But why should we use libraries?.Because we do not intended to invent the wheel, if there is something that already exists and works really well it is better to make use of it instead of spend time and energy redoing it.

Also the community notify when a error has been found and it is usually fixed in next versions of the library, so we can make sure that the library will be better by cause of a multiple “eyes” are looking at it.

Also Python has a various libraries pre-installed.

One of the main example is the use of the math library, this allows us to make mathematical operations like square root and related.

To call a library is really simple, we just have to use the command or keyword import and if we want we can give a nickname using as .

When we import the whole library we will need to call function making use of the name given and a .

(dot) to call the function like math.

sqrt() Sometimes the libraries are huge and can make our program to run slower than expected, so we could want to import just a specific function, and to do so we can use the keyword from so, consider this exampleimport math # This will import the whole library mathimport math as mathematical # Import whole library with a nicknamefrom math import sqrt # Import only the square root functionIn the same way we can make import or call external libraries (the one made by community).

But how we can install them?.We have to use pip or conda, those are package managers.

It should be already installed when you installed Python on your computer.

One of the most popular machine learning libraries in Python is Scikit Learn so in the following example lets say we want to install this library.

pip3 install -U scikit-learnconda install scikit-learnEasy, right?.Now lets move to the last part of our introduction.

The use of Jupyter notebooks.

Using JupyterWhen you install Python as by itself, you will have a white interface to code, something like a notepad.

This is usually a little bit plain, and running the code is usually slow, more over when you are trying to re run just only a little part.

Because of that I recommend to use Jupyter Notebooks, you can find how to installed it here.

You can find a really good tuturial from DataQuest of how to start with Jupyter Notebooks here.

My thoughts about Jupyter notebooks are that this “notebooks” are really good when developing in Python, moreover when you are making explorations, the fact that you can embed texts, links, images and mode in the same place for me, it awesome.

You can give different formats to different parts of the notebook, maybe you want to use bold for a specific word or you want to make a text as title, or want to add images to explain better your results, all these and more can be done using Jupyter.

One thing to keep in mind is you must be organized and keep a correct order when coding with notebooks.

I have seen how my students used to get lost with all the cells, and used to think “this is not working anymore” adding “I just added this here”; but what really happens is that they run over and over the same cell and overwrite the values of the variable they were using, so it is necessary to re run every cell to get the program to run well.

In the next post I will try to make use of Python for my examples, and address topics directly related to data science and machine learning.

Hope you had found this brief introduction useful and I really hope this can give you more interest in knowing more about Python.

If you want to know more about Python, I recommend to check out these linksPython for Data Science – Tutorial for Beginners #1 – Python BasicsIf you are learning Data Science, pretty soon you will meet Python.

Why is that?.Because it's one of the most commonly…data36.

comA Complete Tutorial to Learn Data Science with Python from ScratchIntroduction It happened few years back.

After working on SAS for more than 5 years, I decided to move out of my…www.

analyticsvidhya.

comWhat are Jupyter Notebooks?.Why would I want to use them? : Blended Learning in the Liberal ArtsRight after the Blended Learning Conference, Bryn Mawr is hosting JupyterDayPhilly on May 19, 2017.

The theme is…blendedlearning.

blogs.

brynmawr.

eduWhat exactly is 'Jupyter Notebook'?Answer (1 of 7): Jupyter notebook is a very popular and flexible tool which lets us put our code, output of the code…www.

quora.

comHave a good day! :D.. More details

Leave a Reply