Python Record
Python Record
AIM:
To develop the python program for elementary data items, list, dictionaries
and conditional statement and loops.
PROCEDURE:
Step2: Create the list and enter the Number of data elements in the list.
PROCEDURE:
RESULT:
Thus the program is executed and verified Succesfully
EXNO:2 PROGRAM USING FUNCTIONS
AIM:
To Develop a python program using function.
PROCEDURE:
RESULT:
Thus the program is executed and verified successfully.
EX.NO:3 PROGRAM USING EXCEPTION HANDLING
AIM:
To Develop a python program using function.
PROCEDURE:
Step1: Start the program.
Step4:If the given value is less than the alphabet value, raise the Exception
“InputTooSmallError”.
Step5:If the given value is greater than the alphabet value, raise the Exception
“InputTooLargeError”.
Step6:The given alphabet value is equal to the alphabet value ,display the
statement “congratulation ! you guessed it correctly” .
RESULT:
Thus the program is executed and verified successfully
EX.NO:4 PROGRAM USING CLASSES AND OBJECTS
AIM:
To Develop a python program using classes and objects.
PROCEDURE:
Step3: Define a class init function with argument necessary for bookstore class.
b1.bookinfo()
b2.bookinfo()
b3.bookinfo()
print("BookStore.noofbooks:",BookStore.noofbooks)
OUTPUT:
RESULT:
Thus the program is executed and verified successfully.
EX.NO: 5 PROGRAM USING INHERITANCE
AIM:
To Develop a python program using inheritance.
PROCEDURE:
Step4: Create a Subclass named ‘rec’ to derive the methods from the base class
‘SHAPES’
Step5: Define the method named ‘FIND AREA’ to calculate area of the Rectangle.
def getcapacity(self):
return self.__capacity
def setcapacity(self,capacity):
self.__capacity=capacity
def getvariant(self):
return self.__variant
def setvariant(self,variant):
self.__variant=variant
class vehicle(car):
def __init__(self,model,capacity,variant,color):
super().__init__(model,capacity,variant)
self.__color = color
def vehicleinfo(self):
return self.getmodel()+" "+self.getvariant()+" in" +self.__color+ "with"+
self.getcapacity()+ "seats"
v1 = vehicle("i20 active","4","SX","Bronze")
print(v1.vehicleinfo())
print(v1.getmodel())
v2 = vehicle("fortuner","7","MT2755","white")
print(v2.vehicleinfo())
print(v2.getmodel())
OUTPUT:
RESULT:
Thus the program is executed and verified successfully.
EX.NO: 6 PROGRAM USING POLYMORPHISM
AIM:
To Develop a python program using polymorphism.
PROCEDURE:
Step5: Create a object for classes parrot as blu and penguin as peggy.
def flying_test(bird):
bird.fly()
blu = Parrot()
peggy = Penguin()
flying_test(blu)
flying_test(peggy)
OUTPUT:
RESULT:
Thus the program is executed and verified successfully.
EX.NO: 7 PROGRAM TO IMPLEMENT FILE OPERATION
AIM:
To Develop a python program to implement file operation.
PROCEDURE:
Step6: Using the tell() function ,disply the current file position.
Step7: Using the read() function again read the data and display the data.
RESULT:
Thus the program is executed and verified successfully.
EX.NO: 8 PROGRAM USING MODULES
AIM:
To Develop a python program using modules.
PROCEDURE:
Step3: If the number is negative ,display the message “Sorry,Factorial does not
exist for negative number.
Step4: If the number is zero ,display the message “the Factorial of 0 is 1”.
Step5: If the number is greater than one calculate the factorial value and display
the result.
RESULT:
Thus the program is executed and verified successfully.
EX.NO : 9 PROGRAMS FOR CREATING DYNAMIC AND INTERACTIVE
WEB PAGES USING FORMS.
AIM:
To write a program for creating dynamic and interactive web pages using forms
in Python.
Procedure and Coding:
Python Flask: Make Web Apps with Python
from flask import Flask, render_template, flash, request
from wtforms import Form, TextField, TextAreaField, validators, StringField,
SubmitField
# App config.
DEBUG = True
app = Flask(__name__)
app.config.from_object(__name__)
app.config['SECRET_KEY'] = '7d441f27d441f27567d441f2b6176a'
class ReusableForm(Form):
name = TextField('Name:', validators=[validators.required()])
print form.errors
if request.method == 'POST':
name=request.form['name']
print name
if form.validate():
# Save the comment here.
flash('Hello ' + name)
else:
flash('All the form fields are required. ')
If you will submit an empty form, you will get an error. But if you enter your
name, it will greet you. Form validation is handled by the wtforms module,
but because we gave the argument name = TextField('Name:',
validators=[validators.required()])
OUTPUT:
RESULT:
Thus the program is executed and verified successfully.
Ex.No : 10 Program using database connection and web services
AIM:
To write a Program using database connection and web services in Python.
Install SQLite:
Use this command to install SQLite:
Execute with:
$ python test1.py
It should output:
SQLite version: 3.8.2
The script connected to a new database called test.db with this line:
con = lite.connect('test.db')
It then queries the database management system with the command
SELECT SQLITE_VERSION()
which in turn returned its version number. That line is known as an SQL query:
with con:
cur = con.cursor()
cur.execute("CREATE TABLE Users(Id INT, Name TEXT)")
cur.execute("INSERT INTO Users VALUES(1,'Michelle')")
cur.execute("INSERT INTO Users VALUES(2,'Sonya')")
cur.execute("INSERT INTO Users VALUES(3,'Greg')")
SQLite is a database management system that uses tables. These tables can
have relations with other tables: it’s called relational database management
system or RDBMS. The table defines the structure of the data and can hold the
data. A database can hold many different tables. The table gets created using
the command:
From GUI: If you want to use a GUI instead, there is a lot of choice. Personally
I picked sqllite-man but there are many others. We install using:
sudo apt-get install sqliteman
We start the application sqliteman. A gui pops up.
se those queries in a Python program:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sqlite3 as lite
import sys
con = lite.connect('user.db')
with con:
cur = con.cursor()
cur.execute("SELECT * FROM Users")
rows = cur.fetchall()
for row in rows:
print row
This will output all data in the Users table from the database
$ python get.py
(1, u'Michelle')
(2, u'Sonya')
(3, u'Greg')
Creating a user information database
We can structure our data across multiple tables. This keeps our data
structured, fast and organized. If we would have a single table to store
everything, we would quickly have a big chaotic mess. What we will do is
create multiple tables and use them in a combination. We create two tables:
import sqlite3 as lite
import sys
con = lite.connect('system.db')
with con:
cur = con.cursor()
cur.execute("CREATE TABLE Users(Id INT, Name TEXT)")
cur.execute("INSERT INTO Users VALUES(1,'Michelle')")
cur.execute("INSERT INTO Users VALUES(2,'Howard')")
cur.execute("INSERT INTO Users VALUES(3,'Greg')")
cur.execute("CREATE TABLE Jobs(Id INT, Uid INT, Profession TEXT)")
cur.execute("INSERT INTO Jobs VALUES(1,1,'Scientist')")
cur.execute("INSERT INTO Jobs VALUES(2,2,'Marketeer')")
cur.execute("INSERT INTO Jobs VALUES(3,3,'Developer')")
The jobs table has an extra parameter, Uid. We use that to connect the two
tables in an SQL query:
SELECT users.name, jobs.profession FROM jobs INNER JOIN users ON
users.ID = jobs.uid
You can incorporate that SQL query in a Python script:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sqlite3 as lite
import sys con = lite.connect('system.db')
with con:
cur = con.cursor()
cur.execute("SELECT users.name, jobs.profession FROM jobs INNER JOIN
users ON users.ID = jobs.uid")
rows = cur.fetchall()
for row in rows:
print row
output:
$ python get2.py
(u'Michelle', u'Scientist')
(u'Howard', u'Marketeer')
(u'Greg', u'Developer')
RESULT:
Thus the program is executed and verified successfully.