Python for Data Science : Beginners Part 1

Nihal Tahariya
4 min readMay 9, 2018

This is gonna be a hands on practice tutorial for beginners, which means you can follow along the tutorial and practice the code as you go . Just open the online python compiler you don’t even need to install it locally for this tutorial. Ok, what’s my expectation. Zero, yes you heard it right you got to have zero programming experience, just familiarity with the basic paradigms of programming . In this article we will see about Lists, Tuples, conditional statements, looping and list comprehension. we will build each of them in this tutorial. Most common data types which you will see are integer, float, object(string), boolean.

Data Types

You will use type method to check what is the type of data .

type(True)    ## o/p  bool
type(1) ## o/p int
type("Awesome") ## o/p str
type(1.2) ## o/p float
type("21") ## o/p int , no it is a string.

You got to remember that anything which is enclosed by “ ” is string for ex “34.5”, “Anand45” or “dhfnoe@#$wqh43h” all these are string .

Operators :

Comparison operator: ==, <, >, <=
Boolean operators: and, or ,not

# Comparison operator
print(3 > 2)
print(3!=2)
# Boolean operators
print(True and False)
print(True or False)

Lists

As the name suggest it could be the list of one or more element of the same data type or different data type(example in code). It is mutable i.e. we can make changes after its declaration. We can make changes to either single or multiple elements at the same time. This concept is list comprehension. we will discuss it after we are done with the concept of iteration and looping. For now, lets just explore about lists.

a = [11,22,33,55,66]
type(a) ## list
## acess list via [](indexing)
## python indexing starts from zero
print(a[0]) ## 11
print(a[1]) ## 22
## change the value of list
a[2] = 4
print(a) ## [11,22,4,55,66]
a[3] = "ready" ## example in code
print(a) ## [11,22,4,'ready',66]

There is one more way we can build a list, this is what you will often see in python codes especially while looping.

r = range(3)   ## will create a list [0,1,2]
print(r)

Ok, you must have some rough idea about the looping. Lets refresh your concepts of for and while loops.

While loop

While loop is conditional, since it executes its statement with respect to a condition.

# Stay in loop if condition( i is not equal 5) is true
i = 0
while i != 5 :
print('i is: ',i)
i +=1
print(i,' is equal to 5')

For loop

For loop is iterative in nature, we can pass a list and it will iterate over each element of list.

lis = [1,2,3,4,5]
for i in lis:
print('i is: ',i)
print('')
## we can also use the range method to build the list , which you will often see in python codes for i in range(4): ## will create a list [0,1,2,3]
print('i is: ',i)
print('')

Conditional Statement

Conditional statements uses if-else construct. For ex:

if (a > b):
max=a
else:
max=b
## thats the simplest notation , but we use a different version of the above code.max = a if(a>b) else b

List Comprehension

You can replace single or multiple elements in a list via list comprehension. We will try to put each and every concept in action which we learned till now. This will be interesting.

a = [1,2,3]
s = [i*4 for i in a]
print(s) ## [4,8,12]
## it can also be applied to the tuples but the drawback is it returns a list in the final output , not a tuple .

I appreciate that you come this far, as I promised that I will show you how to change single or multiple elements in the list via list comprehension. Lets combine all the concepts which we have learned so far.

num1 = [5,10,15]
num2 = [i**2 if i == 10 else i-5 if i < 7 else i+5 for i in num1]
print(num2)
## output : [0,100,20]
## one more example:r = [6,7,9]
e = [4 if i == 7 else 5 if i == 6 else i for i in r]
print(e)
## [5,4,9]

Play around with the conditional list comprehension and also experiment putting the while loop in the list comprehension.

Tuple

You can consider tuple as a list with some modifications. Unlike list, tuples are immutable i.e you cannot change the value of tuples. Tuples are generally faster than lists. Tuples are represented as ( )

## defining tuple
r =('w' , 'r' , 'i') ## tuple
r[0] ## 'w'
## Lets try to modify the tuple
r[1] = 56 ## key error

So, does that mean you can never change the values inside a tuple. No, there are some ways to do this and I am going to demonstrate one of the ways. Convert the tuples into a list change the value of the list and then convert back to the tuple. This will be a short code , try to get it yourself.

r  =('w' , 'r' , 'i')
w = list(r)
w[1] = 34
r = tuple(w)
print(r) ## ('w' , 34, 'i')

Here is a task for you, how will you create a list with one element ?

e = [5]  ## easy, I knew you will figure out

Now try the same for one tuple element, don’t look at the code before trying ?

e = (5 ,)  ## something is fishy, but this is how python works

Without comma python treats (5) as an int value, rather than the tuple value.

e = (5)
type(e) ## int

Ok Time to Sleep, See you in part 2 .

--

--