Python Basics : 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 Basics : 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 Basics : 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 Basics : 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 Basics : 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.

Udemy : Python For Data Science & Machine Learning Bootcamp

If you haven’t read about my initial start with PCI and then deviating to learning Python, you can read it here.

While I completed the first free module in datacamp, I wanted to upgrade my free subscription and end up buying the all year package which would give me access to all the data science courses available on datacamp. Just before I could do that, I got my hands on :

“Udemy : Python For Data Science & Machine Learning Bootcamp”

This Udemy course was an upgrade for me from the free course provided for everyone on datacamp. They say, when you really want to get going – the universe will help you in all the ways possible. This is true, and this happens when you put in efforts to learn something with an open mind, knowing very well – that the road the completion will not be that easy. This course too begins with a similar module which I completed in datacamp ( so it would be a refresher for me ) and goes all the way in exploring several data science packages that python provides ( I am talking about NumPy, Scipy, Pandas, Matplotlib, Scikit-Learn and more ).

This makes perfect sense and you know you are going in the right direction. If you are an aspiring data scientist and you know quite a bit about programming ( even the tiny basics will do ) then you should target for learning these packages to begin with than learning python basics / python for programmers course which is available in Udemy / datacamp. They have exercises and also capstone projects which you can complete after the course. The excitement just goes one step higher when you come across boot camps which you feel were outlined just for you and just at the right time. After all the efforts you put in, you landing at the right spot feels heavenly.

I will let you know about this course in about a months time or sooner when I complete the course, probably in another separate blog. Stay Tuned !

Python : Installing The NumPy Package

How to install the numpy package within the python shell on a windows machine in less than 10 steps !

Unlike ‘ R ‘ the installation of packages in Python is slightly different.

Step1 : Open a command line window ( START > RUN > CMD )

Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.


Step 2 : Navigate to the ” Python Scripts ” directory as shown below

C:\Users\harsh> CD C:\
C:\> CD Python27
C:\Python27> CD Scripts

Step 3 : make sure you are connected to the internet and then type pip install numpy

C:\Python27\Scripts> pip install numpy
Collecting numpy
 Downloading numpy-1.13.3-2-cp27-none-win32.whl (6.7MB)
 100% |################################| 6.7MB 85kB/s
Installing collected packages: numpy
Successfully installed numpy-1.13.3

Step 4 : Import numpy like you would import any other installed packages in Python on the Python Shell ! make sure numpy is all lower case else the package will not load and the shell will return an error.

Hope you found this post easy enough to get started with numpy.