Basics in Python for Data Science Series - Part 1
Hello all,
This is the first article in the series Python for Data Science. As a prerequisite to this tutorial series, it is expected that python is already installed in your computer.
In case if you haven't installed python, Please follow the instructions given in the page or in the below video tutorial.
This tutorial is focused mainly on the areas where coding is needed by Data Scientist for daily use. I will try to cover as much as possible. In case if i miss somethings, we are open for your suggestions.
The style of this tutorial would allow the user to get hands on and learn while coding. This style of learning is proven to be effective and encourage to be fast paced.
Run your Python Code from Notepad and Command Prompt
cd C:\Users\Raj\Desktop\openknowledgeshare.blogspot.com\Python\Codes\Day1
3. Now, lets say you would like to run python script intro_code.py which is located inside that folder . then enter the following command
python intro_code.py
4. This would run the python script and the output will be displayed on the command prompt itself.
Now that you know how to run your python script. Lets learn on what can be written inside python scripts
1. Print Statement
Print Statement in Python is usually of the form
print("Hello World")
semicolon is not mandatory in python at the end of statement unlike many programming and scripting languages.
The above statement would print Hello World on your output screen.
In case if you would like to print variable value. Here is a code snippet
a = 10
b = 5
print("The value of a is",a)
print("The value of a is "+str(a))
print("The value of a is {}".format(a))
print("The value of a is {} {}".format(a,b))
print("The value of b and a is {1} {0}".format(a,b))
print(f"The value of b and a is {b} {a}")
These are few various types of ways that can be used to print an integer along with String.
2. Arithmetic Operations
a = 10
# To print the type of variable 'a'
print(type(a))
b = 20
# To print the sum of variables a and b
print(a + b)
# To print the subtraction of variables b from a
print(a -b)
# To print the multiplication of variables a and b
print(a * b)
# To print the dividend of a divided by b
print(a/b)
# To print the remainder of a divided by b
print(a%b)
# To print the dividend of b divided by a
print(b/a)
#To calculate the b power of a
print(b**a)
c = a + b
print(c)
3. Other Complex Math Operations:
import math
Note: The import command need not be at the start of the script although it is good practice to do so, it should be added before using it.
Here is code snippet for few math operations
import math
print(math.exp(3))
print(math.sqrt(9))
#print(math.sqrt(-9))
print(math.pow(2,3))
a = math.pow(2,3)
print(type(a))
Please try and explore different other values and check its outputs
4. String Operations
a = "ML Study"
print(type(a))
a = "ML"
b = "Python"
c = 10
print(a + b)
print(a + " " +b)
#print(a + " " +b +c)
print(a + " " +b + str(c))
# Length of String
a = "ML Python"
print(len(a))
#Split string
a = "I am good at I Python"
print(a.split(" "))
#Modify String
a = "I am Good at I Python"
print(a[0])
print(a[0:6])
a = a.replace('I','p')
print(a)# Search within a string
a = 'string123'
b = 'ring'
print(b in a)
5. Relation Operators
print(a,b)
print(a == b)
a = 10
b = 20
print(a > b)
print(a < b)
print(type(a < b))
6. Increment/decrement operations
print(a)
# Increments variable a by 1
a += 1
print(a)
# Decrements variable a by 10
a -= 10
print(a)
7. Break point
import pdb
pdb.set_trace()
8. Lists
# Defining List
a = [10,20,30]
a = [10,"abc",30]
a = ['hahahh',10,"abc",30]
print(a)
print(len(a))
print(a[0])
print(a[0][0])
print(a[3])
print(a[-1])
print(a[0:3])
#Modify List elements
a[3] = 100
print(a)
#a[4] = 200
a.append(200)
print(a)
a.pop(1)
print(a)
print(dir(a))
# search within a list
a = ["string1","String2",'abc']
b = 'ab'
print(b in a)
print(10 in [20,30])
print(1 in [10])
9. Tuples
a = (10,20,30)a = (10,"abc",30)a = ('hahahh',10,"abc",30)print(a)print(len(a))print(a[0])print(a[0][0])print(a[3])print(a[-1])print(a[0:3])#Modify List elementsa[3] = 100print(a)
a = 10b = 20if(a > b):print(a)print(b)print("A greater than b")print("done")a = 20b = 10if(a > b):print(a)print(b)print("A greater than b")elif(a < b):print(a)print(b)print("A less than b")elif(a == b):print(a)print(b)print("A == b")else:print(a)print(b)print("Something Wrong")a = "qwe"b = "qwe"if(a is not b):print(a)print(b)print("A == b")
11. Loops
11.1. While
While Loop in c language will be of this formatwhile(condition){
code here
}
Where as while loop in python will be of the below format
while(condition):
codeline1codeline2
Code out of while loop
Here are few code snippets to explore while loop
i = 1while(i<=10):
print(i)i += 2
print("Done")
11.2. Do while
do{
codehere
}while(condition)
In case if there is do while loop in python, it should be in the below format.
i = 0do:
i = i + 1
print(i)
while(i < 10)
But There is no do while in python. You will receive error while writing a code like above
11.3. For Loop
For loop takes the folloing format in C language.for(initialization,condition,in/decrement){
codehere
}
for i in range(0,10):
print(i)
print(list(range(0,10)))a = [1,2,3,"wef",2]#for i in [0,1,2,3,4]:for i in range(0,len(a)):
print(a[i])
for item in [1,2,3,"wef",2]:
print(item)
even_list = []odd_list = []for i in range(0,10):
if(i%2 == 0):
even_list.append(i)
else:
odd_list.append(i)
print("Even List is ",even_list)print("Odd List is ",odd_list)
12. Break Statement
Break Statement can be used to break from the loop. Here is a code snippet in how to use it
i = 0while(i < 100):
print(i)i += 1if(i>10):
break
for i in range(10):
for j in range(5):
print(i,j)if(i>2):
break
13. Continue Statement
Continue statement can be used to skip that particular iteration in Loop. Here is a code snippet in how to use it
i = 0while(i < 10):
print(i)i += 1if((i>2) and (i < 5)):
continue
14. Dictionary
a = {}a['ML'] = 1a['Python'] = [3,4,5]a[0] = {}a[0]['jdjdj'] = [3003,2992]print(a)print(a.keys())for each_key in a.keys():
print(each_key,a[each_key])
15. Error Handlers
try:
Code here
except:
Error handling code
finally:
This code executes after try or except
Please refer this video for detailed explanation of Error Handlers
a = 10
b = 0
c = a/b
try:
c = a/b
except:
#print("Error Occured")
c = 0
print(c)
n = 5a = []for i in range(-5,5):
a.append(n/i)
import pdbn = 5a = []for i in range(-5,5):
try:
a.append(n/i)print("No Errors ........")
except :
print("Error at ",i)a.append("Error Occurred")#pdb.set_trace()
finally:
print("No Errors at",i)print(a)
n = 5a = []for i in range(-5,5):
try:
a.append(n/i)print("No Errors ........")
except ValueError:
continue
except ZeroDivisionError:
print("Error at ",i)a.append(0)#pdb.set_trace()
finally:
print("No Errors at",i)print(a)
16. Functions
function_name(int a,int b){
code here
}function_name(x,y)
def function_name(a,b):
code herecodeline2
function_name(x,y)
# To print N numbersdef print_n_numbers(n):
for i in range(0,n+1):
print(i)
a = print_n_numbers(10)print("The Value of a is ",a)print(type(a))def print_n_numbers_skip_few(n,skip_list):
for i in range(0,n):
if(i in skip_list):
print("Skipping this",i)continue
print(i)continue
print("This should not be printed")
print_n_numbers_skip_few(10,[2,3,6,100])print(2 in [2,3,6])def print_n_numbers_skip_few(n,skip_list=[]):
for i in range(0,n):
if(i in skip_list):print("Skipping this",i)continueprint(i)continueprint("This should not be printed")
print_n_numbers_skip_few(10)def sum_of_numbers_in_list(input_list):
sum = 0#for i in range(0,len(input_list)):
#sum = sum + input_list[i]
for each_item in input_list:
sum = sum + each_item
return sum
a = [10,20,40,-100,2.5]b = sum_of_numbers_in_list(a)print(b)print(type(b))
def print_n_numbers_skip_few(n=10,skip_list=[]):
for i in range(0,n):
if(i in skip_list):
print("Skipping this",i)continue
print(i)continueprint("This should not be printed")
print_n_numbers_skip_few()print_n_numbers_skip_few(skip_list=[2,5,6])print_n_numbers_skip_few(skip_list=[2,5,6],n= 20)def print_n_numbers_skip_few_stop_for_few(n,skip_list=[],stop_list = []):
print("Stop List is ",stop_list)for i in range(0,n):
if(i in skip_list):
print("Skipping this",i)continue
#if(i in stop_list):
# break
try:
if(i in stop_list):
break
except:
if(i == stop_list):
print("Stopping after ",i)break
print(i)continueprint("This should not be printed")
print_n_numbers_skip_few_stop_for_few()print_n_numbers_skip_few_stop_for_few(20,skip_list=[2,5,6])print_n_numbers_skip_few_stop_for_few(skip_list=[2,5,6],20)print_n_numbers_skip_few_stop_for_few(skip_list=[2,5,6],n= 20)print_n_numbers_skip_few_stop_for_few(skip_list=[2,5,6],n= 20,stop_list = 10)
17. OS Functions:
#import math#import pdbimport osmain_folder_path = r'E:\openknowledgeshare.blogspot.com\Python\Outputs\Folder_1'#print(os.listdir(main_folder_path))# Printing the content in the folderfor each_folder_name in os.listdir(main_folder_path):
print(each_folder_name)full_path = os.path.join(main_folder_path,each_folder_name)print(full_path)print(os.listdir(full_path))
def get_contents_in_folder(input_folder_path):
content_list = []for each_folder in os.listdir(input_folder_path):full_path = os.path.join(input_folder_path,each_folder)print(full_path)if(os.path.isdir(full_path)):content_list += get_contents_in_folder(full_path)else:content_list.append(full_path)return content_list
print("Final Contents",get_contents_in_folder(main_folder_path))# Creating Foldernew_folder_path = os.path.join(main_folder_path,'MLStduyFOlder_2')print(new_folder_path)def create_folder(input_folder_path):
if not os.path.exists(input_folder_path):
os.makedirs(input_folder_path)
def delete_folder(input_folder_path):
if os.path.exists(input_folder_path):
os.remove(input_folder_path)
delete_folder(new_folder_path)
17. Classes:
import osclass OSRelatedFunctions:
def get_contents_in_folder(self,input_folder_path):
content_list = []for each_folder in os.listdir(input_folder_path):
full_path = os.path.join(input_folder_path,each_folder)print(full_path)if(os.path.isdir(full_path)):
content_list += self.get_contents_in_folder(full_path)
else:
content_list.append(full_path)
return content_list
def create_folder(self,input_folder_path):
if not os.path.exists(input_folder_path):
os.makedirs(input_folder_path)
def delete_folder(self,input_folder_path):
if os.path.exists(input_folder_path):
os.remove(input_folder_path)
if __name__ == '__main__':
main_folder_path = r'E:\openknowledgeshare.blogspot.com\Python\Outputs\Folder_1'new_folder_path = os.path.join(main_folder_path,'CompletelyNewFolder')a = OSRelatedFunctions() # Creating an instance of classprint(a.get_contents_in_folder(main_folder_path))print(a.create_folder(new_folder_path))
#import class_codefrom class_code import OSRelatedFunctionsimport os#os_func = class_code.OSRelatedFunctions()os_func = OSRelatedFunctions()main_folder_path = r'E:\openknowledgeshare.blogspot.com\Python\Outputs\Folder_1'new_folder_path = os.path.join(main_folder_path,'CompletelyNewFolder')#os_func = OSRelatedFunctions() # Creating an instance of classprint(os_func.get_contents_in_folder(main_folder_path))print(os_func.create_folder(new_folder_path))
18. Files Read and Write Operations
18.1. Reading Text File
import osmain_folder_path = r'E:\openknowledgeshare.blogspot.com\Python\Outputs'text_file_path = os.path.join(main_folder_path,'meeting_saved_chat.txt')f = open(text_file_path,'rb')text_file_info = f.readlines()f.close()for each_item in text_file_info:
print(each_item)
Here is the code snippet for reading one line at a time
import osmain_folder_path = r'E:\openknowledgeshare.blogspot.com\Python\Outputs'text_file_path = os.path.join(main_folder_path,'meeting_saved_chat.txt')text_file_info = []f = open(text_file_path,'r')while(True):
this_line_text = f.read()print(this_line_text)text_file_info.append(this_line_text)if not this_line_text:
break
print(text_file_info)
18.2. Writing to Text File
Here is the code snippet for writing one line at a time
text_file_info = ["1\n","3\n","4\n","6\n"]folder_path = os.path.join(main_folder_path,'saved_text')if not os.path.exists(folder_path):
os.makedirs(folder_path)
output_text_path = os.path.join(main_folder_path,'saved_text','new_created_doc.txt')print(output_text_path)f = open(output_text_path,'w')for each_item in text_file_info:
f.write(str(each_item))
f.close()
Here is the code snippet for writing all lines at once
text_file_info = ["1\n","3\n","4\n","6\n"]folder_path = os.path.join(main_folder_path,'saved_text')if not os.path.exists(folder_path):
os.makedirs(folder_path)
output_text_path = os.path.join(main_folder_path,'saved_text','new_created_doc.txt')print(output_text_path)f = open(output_text_path,'w')f.writelines(text_file_info)f.close()
18.3. Appending to Text File
Here is the code snippet for appending one line at a timetext_file_info = ["1\n","3\n","4\n","6\n"]folder_path = os.path.join(main_folder_path,'saved_text')if not os.path.exists(folder_path):
os.makedirs(folder_path)
output_text_path = os.path.join(main_folder_path,'saved_text','new_created_doc.txt')print(output_text_path)f = open(output_text_path,'a')for each_item in text_file_info:
f.write(str(each_item))
f.close()
Here is the code snippet for appending all lines at once
text_file_info = ["1\n","3\n","4\n","6\n"]folder_path = os.path.join(main_folder_path,'saved_text')if not os.path.exists(folder_path):
os.makedirs(folder_path)
output_text_path = os.path.join(main_folder_path,'saved_text','new_created_doc.txt')print(output_text_path)f = open(output_text_path,'a')f.writelines(text_file_info)f.close()
18.4. Writing to CSV File
Here is the code snippet for writing one row at a time to a csv file.
import csvtext_file_info = [[1],[2,4],[3,7],[4,10],[5,10,20,30]]output_csv_path = os.path.join(main_folder_path,'saved_text','new_created_doc.csv')f = open(output_csv_path,'w',newline = "")writer_csv = csv.writer(f)for each_row in text_file_info:
writer_csv.writerow(text_file_info)
f.close()
Here is the code snippet for writing all rows at once
import csvtext_file_info = [[1],[2,4],[3,7],[4,10],[5,10,20,30]]output_csv_path = os.path.join(main_folder_path,'saved_text','new_created_doc.csv')f = open(output_csv_path,'w',newline = "")writer_csv = csv.writer(f)writer_csv.writerows(text_file_info)f.close()
18.5 Appending to CSV File
Here is the code snippet for appending one row at a time to a csv file.
import csv
text_file_info = [[1],[2,4],[3,7],[4,10],[5,10,20,30]]
output_csv_path = os.path.join(main_folder_path,'saved_text','new_created_doc.csv')
f = open(output_csv_path,'a',newline = "")
writer_csv = csv.writer(f)
for each_row in text_file_info:
writer_csv.writerow(text_file_info)
f.close()
Here is the code snippet for appending all rows at once
import csv
text_file_info = [[1],[2,4],[3,7],[4,10],[5,10,20,30]]
output_csv_path = os.path.join(main_folder_path,'saved_text','new_created_doc.csv')
f = open(output_csv_path,'a',newline = "")
writer_csv = csv.writer(f)
writer_csv.writerows(text_file_info)
f.close()
19. Using Pickle in Python
19.1 Dumping to Pickle File
Here is the code snippet to dumping to pickle file
import picklea = 10b = 20c = {'a':[3003,3001],'b':[22,11]}f = open(os.path.join(main_folder_path,"saved_data.pickle"),'wb')pickle.dump([a,b,c],f)f.close()
19.2 Loading from Pickle File
import picklef = open(os.path.join(main_folder_path,"saved_data.pickle"),'rb')[d,e,f] = pickle.load(f)print(d,type(d))print(e,type(e))print(f,type(f))import picklef = open(os.path.join(main_folder_path,"saved_data.pickle"),'rb')loaded_info = pickle.load(f)print(loaded_info)d = loaded_info[0]e = loaded_info[1]f = loaded_info[2]print(d,type(d))print(e,type(e))print(f,type(f))
20. Installing External Python Libraries
External python libraries can be installed by using following command in command prompt/terminal
pip install PACKAGE_NAME
where PACKAGE_NAME is a package/external library which needs to be installed.
For example,
In case you would like to install tensorflow library, you should run the following command
pip install tensorflow
21. Using Jupyter Notebook for Python
Instead of writing your code in .py file, you can also use Jupyter notebook on your browser for the same.
For using Jupyter Notebook, python library jupyter is needed. To install it, please use the following command
pip install jupyter
Now, Navigate to the folder where you would like to create a jupyter notebook and type the following command.
jupyter notebook
This would open the jupyter dashboard on your browser. Click on New and create a Python Notebook.
22. Using Google Colaboratory
In case if you don't have python installed in your system, you can still run your code online in cloud. One such way is using google colaboratory.
Go to the following URL
https://colab.research.google.com/
If you create new file, it will be similar to Jupyter Notebook and you can run it in the browser itself.
This notebook will be saved in google drive automatically.
One more Advantage, you can use this in any device (PC/Mobile) just that your browser should support it.
Main disadvantage is that each session can be maximum 12 hours after which it stops. You need to restart the kernel and run your code from beginning.
23. Classes in Python
import os
main_folder_path = r"E:\openknowledgeshare.blogspot.com\Python\Outputs"
class OSRelatedFunctions:
def get_contents_in_folder(self,input_folder_path):
content_list = []
for each_folder in os.listdir(input_folder_path):
full_path = os.path.join(input_folder_path,each_folder)
print(full_path)
if(os.path.isdir(full_path)):
content_list += self.get_contents_in_folder(full_path)
else:
content_list.append(full_path)
return content_list
def create_folder(self,input_folder_path):
if not os.path.exists(input_folder_path):
os.makedirs(input_folder_path)
def delete_folder(self,input_folder_path):
if os.path.exists(input_folder_path):
os.remove(input_folder_path)
os_func = OSRelatedFunctions()
1 Pickling class instances¶
import pickle
f = open(os.path.join(main_folder_path,'class.pickle'),'wb')
pickle.dump(os_func,f)
f.close()
f = open(os.path.join(main_folder_path,'class.pickle'),'rb')
new_class_loaded = pickle.load(f)
f.close()
dir(new_class_loaded)
new_class_loaded
2. Scope of Variables in Classes¶
2.1. varaiable within functions of class¶
class ArthimeticFunctions:
def __init__(self):
print("Initialzied")
self.a = 100
self.b = 200
def function1(self,c):
self.a = self.a + c
self.b = self.b -c
return self.a,self.b
def function2(self,d):
self.a = self.a*d
self.b = self.b/d
af1 = ArthimeticFunctions()
af2 = ArthimeticFunctions()
print(af1.a,af1.b)
print(af2.a,af2.b)
af1.function1(10)
af1.function1(10)
print(af1.a,af1.b)
print(af2.a,af2.b)
2.2. Defining varaible at scope of class¶
e = 1000
class ArthimeticFunctions:
def __init__(self):
print("Initialzied")
self.a = 100
self.b = 200
def function1(self,c):
self.a = self.a + c
self.b = self.b -c
return self.a,self.b
def function2(self,d):
self.a = self.a*d
self.b = self.b/d
def function3(self):
self.a = self.a + e
self.b = self.b - e
return self.a,self.b
af = ArthimeticFunctions()
af.function3()
class ArthimeticFunctions:
varaiable1 = 50
def __init__(self):
print(self)
print("Initialzied")
self.a = 100
self.b = 200
def function1(self,c):
self.a = self.a + c
self.b = self.b -c
return self.a,self.b
def function2(self,d):
a = self.a*d
b = self.b/d
return a,b
def function3(self):
self.a = self.a + e
self.b = self.b - e
return self.a,self.b
def function4(self):
print(varaiable1)
def function5(self):
print(self.varaiable1)
self.varaiable1 = 100
print(self.function3())
af_new
af_new = ArthimeticFunctions()
varaiable1
af_new.function4()
af_new.function5()
af_new.a,af_new.b,af_new.varaiable1
af_new.__init__()
af_new.a,af_new.b,af_new.varaiable1
af_new.function1(10)
af_new.a,af_new.b,af_new.varaiable1
af_new.function2(10)
af_new.a,af_new.b,af_new.varaiable1
Comments
Post a Comment