Others

Saturday, 22 September 2018

Python Lab - Day 3


Python Labs : 3rd    Day

1.       User defined Exception
2.       Machine Learning Beginning
3.       File Demo
4.       Database Connection with Sqlite in Python
5.       Pickle  demo


Lab 1
User defined Exception
bb = 60

  

  class C(Exception) :

    pass

  

try :

    if bb >= 500 :

        print(bb)

    else :

        raise C("This is error")

  

  except C:

    print("money is in sufficent")

  else:

    print("something is better ")

  finally:

    print("Executed definetly")
 

Lab 2
Machine Learning Beginning


openCv is a tool

numpy ::

pandas :: reading data or file

seaborn  ::

matplotlib : to the the data

Scikit-learn

Prediction

  

Liner Regression

Logistic Regression

KNN

Random Forest and Decision Tree

  

Intruduction to statical Learning

ISLR

'''

# download the data set

#  Mockaroo

# kaggle


  

  

Lab 3 :
File Demo
# files  = internal storage

  

'''

Mode of write

r -  read

w - write

a - append

r+ - read write

Wa - write and append

  

  

  

'''

# download the data set

#  Mockaroo

# kaggle

  

  '''

f1 = open("Mine.txt","w")

f1.write("This is my first file  program")

f1.close()

'''

  

  f1 = open("Mine.txt","r")

  

a= f1.read();

  

  

  print(a)

f1.seek(5)

  

  print(f1.read())

  

f1.close()

  

  '''

Multiple file read and write

'''

'''

f1 = open("even.txt","w")

f2 = open("odd.txt","w")


for  i in range(100) :

    if  i % 2 == 0 :

        f1.write(str(i) + "\n")

    else :

        f2.write(str(i) + "\n")

  

f1.close()

f2.close()

'''

  f1 = open("even.txt","r")

  #x = f1.readlines()

  print(f1.read().splitlines() )

f1.close()


Lab 4
Database Connection with Sqlite in Python


  '''

  

SQLITEBROWSER

'''

  

  import sqlite3

  

conn = sqlite3.connect('test.db')

  print("Connected successfully")

  

  #print(conn.execute("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='employee';"))

  

  table = "CREATE TABLE IF NOT EXISTS employee(id int, name text, age)"

  

  

  conn.execute(table)

  

ins = "insert into employee(id, name, age) values(1,'satya prakash rathore',23)"

  

  conn.execute(ins)

conn.commit() # after insert update or delete need to perform the commit operations

  

  

  see = "select * from employee"

  val = conn.execute(see)

  

  for i in val :

    print("\n ID = {0}, Name = {1}, Age = {2}".format(i[0],i[1],i[2]))

   # print("\n , ID = {0}".format(i[0]))

  

  '''

dele = "delete from employee"

conn.execute(dele)

conn.commit()

'''

  

  conn.close()


Lab 5
Pickle  demo
# Pickle : it is the process of convrting all the python object to charater/byte streem

# process of doing pickle is called seralization, it is stored in locale disk

# it will dumping the python object and loading the pickle object

# pickle is using two method [Dumping and loading]

  

  

  import pickle

  

  

a= ["hello","chalo","jao","aao"]

  

  

f1 = open("testpi","wb")

pickle.dump(a,f1)

  

f1.close()

  # pulling the file

  

  f2 = open("testpi","rb")

  

hello = pickle.load(f2)

  print(hello)

f2.close();

  


No comments:

Post a Comment