Python : Crash Course Part 7 – Sets

A Set in python is a collection of unique elements. Curly brackets are used to define a set. If you have a bunch of integers and you need to pick only the unique numbers among them, you can make use of python sets. We can use a set function to pass a list of values and the output set will contain the unique elements.

In [1]: {1,2,3,4,3,4,5}
Out[2]: {1,2,3,4,5}
In [3]: set([1,1,1,2,2,3,3,4,5])
Out[4]: {1,2,3,4,5}

We can even add items to the sets by using the add method as shown below :

In [5]: set={1,2,3}
In [6]: set.add(4)
In [7]: set
Out[8]: {1,2,3,4}

Note that the element is added to the end of the set. Try to add 4 again to the set and let me know what happens in the comments section below.


Python : Crash Course Part 6 – Tuples

Tuples in python act similar to that of a python list. In list we pass the elements within a square bracket, but in tuples it is parenthesis ( open & close brackets ). The other major difference between tuples and lists is that elements inside a list can be updated. To see how the elements can be updated refer to my previous blog post on lists. Tuples on the other hand cannot be updated. It’s more of a read only object once it is created. Fetching the data out of a tuple and a list will still remain the same. Having said that the indexing would act similar in a tuple and a list.

In [1]:tuple = (1,2,3)
In [2]:tuple[0]

Try assigning / updating an element in your tuple and let me know what error you get in the comments section below.

If you are not aware of what a for or while loop is, don’t worry about it much. Those are covered in the upcoming courses. What I wanted to show you is tuple unpacking using the for loop.

Lets say we have a list of tuples as shown below :

In [4]:x=[(1,2),(3,4),(5,6)]

We can unpack and print the elements using tuple unpacking with the help of a for loop :

In [5]:for (a,b) in x:
                     print (a)
                     print (b)

That’s how you unpack a tuple. the values in x in the form of a tuple ( a,b ) will be fed into a for loop. You can come back to this topic after you complete the for loop course in this series. You can however tend to skip this unpacking topic for now.


Python : Crash Course Part 5 – Dictionaries

Curly brackets, a key & a value to the key – these are the three components of a dictionary in python. The key name has to be passed within single quotes if the key name is a string, single quotes is not required if the key is an integer.

In [1]: dictionary = {'key1':100}

Now dictionary[0] won’t work here as the data retrieval using a dictionary is already controlled by the keys within the dictionary.

If I want to retrieve the value 100 from the above dictionary, the syntax would be :

In [2]: dictionary['key1']
Out[2]: 100

We can also feed a list within a dictionary as shown below :

In [3]: dictionary={'k1':[1,2,3]}
In [4]: dictionary['k1']
Out[5]: [1,2,3]

If I want to retrieve just a number of my choice from the list, which is within the dictionary then the syntax would be :

In [6]: dictionary['k1'][2]
Out[7]: 3

Note that data retrieval method still holds good as it did when we explored indexing in python. We can also have a nested dictionary in python :

In [8]: dictionary{'k1':{'nested_dictionary'[1,2,3]}}

How would I retrieve the number 3 from the above dictionary, let me know in the comments below.

Let’s move on to Tuples.

Python : Crash Course Part 4 – Lists

Lists in python are a sequence of elements/objects
( character strings, numeric variable etc. )
enclosed within a square bracket and separated by a comma.

In [1]:[1,2,3]
In [2]:my_list = ['a','b','c']

If we need to add more character to the end of the list we use the append method as shown below :

In [3]:my_list.append('d')
In [4]:my_list

Note that to append a character at the end of the list we use the curve bracket and not a square bracket in the append method.

Even the list is indexed and will behave exactly like how we discussed indexing on the strings. If we have to grab the first item in the list it would be :

In [6]:my_list[0]

If I want to update ‘d’ as ‘data’ in my list I will have to :

In [7]:my_list[3] ='data'
In [8]:my_list

Note that if I have to update the 4th character I have to update the 3rd index position.

Nested Lists –

Python enables to have a list within a list as shown below

In [9] :nested = [1,2,[3,4]]
In [10]:nested

In order to retrieve elements within a nested list we can use the below syntax :

In [12]:nested[2][1]

The first square bracket above shifts pointer to the second list which is [3,4]. the next square bracket points to index number 1 of the nested list and retrieves 4.

Lets now move on to dictionaries in python.


Python : Crash Course Part 3 – Strings

After completing basic arithmetic and variable assignment, its time to move on to strings in python.

You can use single or double quotes to declare a string as shown below :

In[1] : ' hello world '
Out[1]: ' hello world '
In[2] : "hello world"
Out[2]: "hello world"

Wrapping double quotes around single quote for sentences can be dealt with as shown below :

In[3] : " I can't quit "
Out[3]: " I can't quit"

Print Statement –

This is the official way of displaying a string in python, note how the Out indicator & the single quotes are gone while using the print statement :

In [4]: x = ' hello world'
In [5]: print(x)
        hello world

Print Formatting –

This method is used when you want to repeatedly use variables within your statements. format is a special method of the string that enables inserting of variables within the {} brackets as per the order specified within the format curly bracket.

      y='the analytics engine'
      z='The number {} blog in the field of data science is {}'.format(x,y)
      The number 1 blog in the field of data science is the analytics engine

String Indexing –

Python indexes every character in a string. The indexing starts at 0 in python. Let’s say we have a string

In [8]:s = 'engine'

If we had to break it down then python would have indexed the first character ‘a’ at index 0, second character ‘n’ at index 1 and so on..

In [9]:s[0]

We can use the slice syntax using the colon notation (:) which can be used to grab a substring or set /block of characters.

In [10]:s[:4]

Since the indexing starts at 0, the slicing happens till the 4th character which is actually at index position 3.

“So basically the number after the slice indicator is the (position of the character) or (the position of the character – 1 which would be the index location internally).

In python terms it would be 4th character :4 or s[3]”

In [11]:s[2:4]

The above code retrieved data from index s[2] which is ‘g’ and s[3] which is ‘i’. It will not pick s[4] which is ‘n’ as mentioned above, it would pick the 4th character or s[3]

This is little tricky, but with some practice it would be easy and I will explain more of such in the next blog when we will discuss about python lists.

Python : Crash Course Part 2 – Variable Assignment

Have you tried out the arithmetic operations on python yet ? If not, I strongly recommend you to visit the first part of the crash course. For the rest of you, let’s get started.

The assignment operator in python is the “equal to” ( = ) sign as shown below:

In [1]: var = 2
In [2]: var
Out[2]: 2

Some more examples:

In [3]: X=4
In [4]: X+Y
Out[4]: 14

If your variable comprises of multiple words then separate the words with a “under score” ( _ ) symbol as shown below :

In [5]: multiple_words_var = 10


1) Variable names shouldn’t start with numbers.
2) Variable names cannot start with special symbols as well.
3) Variable names are case sensitive.

Feels like we are using C programming, aren’t we ? It’s that simple. Let’s go to strings now.

Python : Crash Course Part 1 – Arithmetic

Python and R are the two widely used programming languages for data science. I am going to start with some basic python coding on the jupyter notebook which should help us better understand how tough or easy it is to code.

Python has two basic number types : Integer and floating point. So pretty much any arithmetic function can be done in python on these data types. Here are some examples to get started with :

You can try these out on your local session of jupyter notebook

Addition –

In [1] : 1+1
Out[1] : 2

Subtraction –

In [2] : 1-1
Out[2] : 0

Multiplication –

In [3] : 2*2
Out[3] : 4

Division –

In [4] : 1/2
Out[4] : 0

Note that 1/2 in Python 2.7 will throw 0 as the output where as Python 3.6 will throw 0.5 as the output. Python 2.7 expects the denominator to be present in floating point format for the correct results.

In [5] : 1/2.0
Out[5] : 0.5

Exponent –

In [6]: 2**4
Out[6]: 16

Modulus –

In [7]: 4%2
Out[7]: 0

The modulus function helps identifying if the number is even or odd based on the output. MOD the input number by 2 and if the output is 0 then the input number is even else its odd.

In [8]: 5%2
Out[8]: 1

You can try out more of such arithmetic operations and let me know in the comments sections below. Let’s now move on to the topic of variable assignments in python.