PythonLab_R-2019
PythonLab_R-2019
______________________________________________________________________________
______________________________________________________________________________
Subject In charge
EXPERIMENT 01
and Logical AND: True if both the operands are true x and y
return x + y
DATTA ME DATTA MEGHE COLLEGE OF ENGINEERING
Department of Information Technology
LAB MANUAL
______________________________________________________________________________
return x - y
return x * y
return x / y
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
DATTA ME DATTA MEGHE COLLEGE OF ENGINEERING
Department of Information Technology
LAB MANUAL
______________________________________________________________________________
while True:
if choice == '1':
______________________________________________________________________________
break
else:
print("Invalid Input")
Output
Select operation.
1.Add
2.Subtract
3.Multiply
4.Divide
Enter choice(1/2/3/4): 3
:
DATTA ME DATTA MEGHE COLLEGE OF ENGINEERING
Department of Information Technology
LAB MANUAL
______________________________________________________________________________
EXPERIMENT 02:
Aim:
EXPERIMENT 03:
AIM:- To check if the number is Armstrong number or not.
THEORY:-
The armstrong number, also known as the narcissist number, is of special interest
to beginners. It kindles the curiosity of programmers just setting out with learning
a new programming language. In number theory, an armstrong number in a given
number base b is a number that is the sum of its own digits each raised to the
power of the number of digits.
To put it simply, if I have a 3-digit number then each of the digits is raised to the
power of three and added to obtain a number. If the number obtained equals the
DATTA ME DATTA MEGHE COLLEGE OF ENGINEERING
Department of Information Technology
LAB MANUAL
______________________________________________________________________________
original number then, we call that armstrong number. The unique property related
to the armstrong numbers is that it can belong to any base in the number system.
For example, in decimal number system, 153 is an armstrong number.
1^3+5^3+3^3=153
Consider the 6 -digit number 548834 .Calculate the sum obtained by adding the
following terms.
We obtain the same number when we add the individual terms. Therefore, it
satisfies the property.
We need two parameters to implement armstrong numbers. One being the number
of digits in the number, and second the sum of the digits raised to the power of
number of digits. Let us look at the algorithm to gain a better understanding:
Algorithm:
2. The individual digits are obtained by performing num mod 10, where mod is the
remainder operation.
DATTA ME DATTA MEGHE COLLEGE OF ENGINEERING
Department of Information Technology
LAB MANUAL
______________________________________________________________________________
3. The digit is raised to the power of the number of digits and stored
5. Steps 2,3 and 4 are repeated until the value of num is greater than 0
PROGRAM:-
# initialize sum
sum = 0
temp = num
digit = temp % 10
sum += digit ** 3
temp //= 10
______________________________________________________________________________
if num == sum:
else:
OUTPUT:-
DATTA ME DATTA MEGHE COLLEGE OF ENGINEERING
Department of Information Technology
LAB MANUAL
______________________________________________________________________________
EXPERIMENT 04:
AIM:- Writing data into CSV file
THEORY:-
CSV (Comma Separated Values) is a simple file format used to store tabular data,
such as a spreadsheet or database. CSV file stores tabular data (numbers and text)
in plain text. Each line of the file is a data record. Each record consists of one or
more fields, separated by commas. The use of the comma as a field separator is the
source of the name for this file format.
Python provides an in-built module called csv to work with CSV files. There are
various classes provided by this module for writing to CSV:
csv.writer class is used to insert data to the CSV file. This class returns a writer
object which is responsible for converting the user’s data into a delimited string. A
csvfile object should be opened with newline='' otherwise newline characters
inside the quoted fields will not be interpreted correctly.
DATTA ME DATTA MEGHE COLLEGE OF ENGINEERING
Department of Information Technology
LAB MANUAL
______________________________________________________________________________
Parameters:
writerow(): This method writes a single row at a time. Field row can be written
using this method.
Syntax:
writerow(fields)
writerows(): This method is used to write multiple rows at a time. This can be used
to write rows list.
Syntax:
writerows(rows)
PROGRAM:-
DATTA ME DATTA MEGHE COLLEGE OF ENGINEERING
Department of Information Technology
LAB MANUAL
______________________________________________________________________________
import csv
# field names
filename = "university_records.csv"
csvwriter = csv.writer(csvfile)
DATTA ME DATTA MEGHE COLLEGE OF ENGINEERING
Department of Information Technology
LAB MANUAL
______________________________________________________________________________
csvwriter.writerow(fields)
csvwriter.writerows(rows)
OUTPUT:-
DATTA ME DATTA MEGHE COLLEGE OF ENGINEERING
Department of Information Technology
LAB MANUAL
______________________________________________________________________________
EXPERIMENT 05
AIM:
THEORY:
In multilevel inheritance, features of the base class and the derived class is
inherited into the new derived class.
In the multiple inheritance scenario, any specified attribute is searched first in the
current class. If not found, the search continues into parent classes in depth-first,
left-right fashion without searching the same class twice.
CODE:
DATTA ME DATTA MEGHE COLLEGE OF ENGINEERING
Department of Information Technology
LAB MANUAL
______________________________________________________________________________
class employee1:
def
__init__(self, name, age, salary):
self.name
= name
self.age =
age
self.salary = salary
def
printname(self):
class childemployee(employee1):
def
__init__(self,name,age,salary,id):
______________________________________________________________________________
self.id =
id
def
welcome(self):
x = childemployee('harshit',22,1000,101)
x.printname()
x.welcome()
OUTPUT:
DATTA ME DATTA MEGHE COLLEGE OF ENGINEERING
Department of Information Technology
LAB MANUAL
______________________________________________________________________________
EXPERIMENT 06:
AIM:- To learn single inheritance
THEORY:-
In python single inheritance, a derived class is derived only from a single parent
class and allows the class to derive behaviour and properties from a single base
class. This enables code reusability of a parent class, and adding new features to a
class makes code more readable, elegant and less redundant. And thus, single
inheritance is much more safer than multiple inheritances if it’s done in the right
way and enables derived class to call parent class method and also to override
parent classe’s existing methods.
Syntax:
class Parent_class_Name:
class Child_class_Name(Parent_class_name):
______________________________________________________________________________
The above syntax consists of two classes declared. One is the base class, or by
other means, the parent class and the other one is for the child class, which acts as
the derived class.
Each of these classes has its own code block. So as per single inheritance, every
element present in the parent class’s code block can be wisely used in the child
class. To attain a single inheritance syntax ally, the name of the parent class is
mentioned within the child class’s arguments.
PROGRAM:-
class Person:
self.firstname = fname
DATTA ME DATTA MEGHE COLLEGE OF ENGINEERING
Department of Information Technology
LAB MANUAL
______________________________________________________________________________
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
p=Person("ana","xyz")
p.printname()
class Student(Person):
pass
x = Student("pqr", "aaa")
x.printname()
DATTA ME DATTA MEGHE COLLEGE OF ENGINEERING
Department of Information Technology
LAB MANUAL
______________________________________________________________________________
OUTPUT:-
DATTA ME DATTA MEGHE COLLEGE OF ENGINEERING
Department of Information Technology
LAB MANUAL
______________________________________________________________________________
EXPERIMENT 07:
AIM:
THEORY:
Creating a new class creates a new type of object, allowing new instances of that
type to be made.
Each class instance can have attributes attached to it for maintaining its state. Class
instances can also have methods (defined by their class) for modifying their state.
class ClassName:
# Statement-1
.
.
.
DATTA ME DATTA MEGHE COLLEGE OF ENGINEERING
Department of Information Technology
LAB MANUAL
______________________________________________________________________________
# Statement-N
CODE:
class Person:
def
__init__(self, name, age):
self.name = name
self.age
= age
def
myfunc(self):
p1 = Person("John", 36)
p1.myfunc()
OUTPUT:
DATTA ME DATTA MEGHE COLLEGE OF ENGINEERING
Department of Information Technology
LAB MANUAL
______________________________________________________________________________
EXPERIMENT 08
THEORY: -
Menus are the important part of any GUI. A common use of menus is to provide
convenient access to various operations such as saving or opening a file, quitting a
program, or manipulating data. Top level menus are displayed just under the title
bar of the root or any other top level windows.
PROGRAM :-
DATTA ME DATTA MEGHE COLLEGE OF ENGINEERING
Department of Information Technology
LAB MANUAL
______________________________________________________________________________
top = Tk()
menubar = Menu(top)
file.add_command(label="New")
file.add_command(label="Open")
file.add_command(label="Save")
file.add_command(label="Save as...")
file.add_command(label="Close")
file.add_separator()
file.add_command(label="Exit", command=top.quit)
menubar.add_cascade(label="File", menu=file)
edit.add_command(label="Undo")
edit.add_separator()
edit.add_command(label="Cut")
DATTA ME DATTA MEGHE COLLEGE OF ENGINEERING
Department of Information Technology
LAB MANUAL
______________________________________________________________________________
edit.add_command(label="Copy")
edit.add_command(label="Paste")
edit.add_command(label="Delete")
edit.add_command(label="Select All")
menubar.add_cascade(label="Edit", menu=edit)
help.add_command(label="About")
menubar.add_cascade(label="Help", menu=help)
top.config(menu=menubar)
top.mainloop()
OUTPUT :-
DATTA ME DATTA MEGHE COLLEGE OF ENGINEERING
Department of Information Technology
LAB MANUAL
______________________________________________________________________________
Expt 09:
DATTA ME DATTA MEGHE COLLEGE OF ENGINEERING
Department of Information Technology
LAB MANUAL
______________________________________________________________________________
Tkinter provides various controls, such as buttons, labels and text boxes used in a
GUI application. These controls are commonly called widgets.
There are currently 15 types of widgets in Tkinter.
PROGRAM:
import tkinter
# Let's create the Tkinter window.
window = tkinter.Tk()
window.title("GUI")
# You will first create a division with the help of Frame class and align them on
TOP and BOTTOM with pack() method.
top_frame = tkinter.Frame(window).pack()
bottom_frame = tkinter.Frame(window).pack(side = "bottom")
# Once the frames are created then you are all set to add widgets in both the
frames.
btn1= tkinter.Button(top_frame, text = "Button1", fg = "red").pack() #'fg or
foreground' is for coloring the contents (buttons)
btn2 = tkinter.Button(top_frame, text = "Button2", fg = "green").pack()
DATTA ME DATTA MEGHE COLLEGE OF ENGINEERING
Department of Information Technology
LAB MANUAL
______________________________________________________________________________
OUTPUT:
EXPERIMENT 10:
AIM :- To run module in python.
THEORY :-
What is a Module?
Consider a module to be the same as a code library.
Create a Module
To create a module just save the code you want in a file with the file extension .py:
Variables in Module
DATTA ME DATTA MEGHE COLLEGE OF ENGINEERING
Department of Information Technology
LAB MANUAL
______________________________________________________________________________
The module can contain functions, as already described, but also variables of all
types (arrays, dictionaries, objects etc):
Example
Save this code in the file mymodule.py
person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
Example
Import the module named mymodule, and access the person1 dictionary:
import mymodule
a = mymodule.person1["age"]
print(a)
Naming a Module
You can name the module file whatever you like, but it must have the file
extension .py
Re-naming a Module
You can create an alias when you import a module, by using the as keyword:
Example
Create an alias for mymodule called mx:
import mymodule as mx
a = mx.person1["age"]
print(a)
Built-in Modules
There are several built-in modules in Python, which you can import whenever you
like.
DATTA ME DATTA MEGHE COLLEGE OF ENGINEERING
Department of Information Technology
LAB MANUAL
______________________________________________________________________________
Example
Import and use the platform module:
import platform
x = platform.system()
print(x)
Using the dir() Function
There is a built-in function to list all the function names (or variable names) in a
module. The dir() function:
Example
List all the defined names belonging to the platform module:
import platform
x = dir(platform)
print(x)
Note: The dir() function can be used on all modules, also the ones you create
yourself.
Example
The module named mymodule has one function and one dictionary:
def greeting(name):
print("Hello, " + name)
person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
DATTA ME DATTA MEGHE COLLEGE OF ENGINEERING
Department of Information Technology
LAB MANUAL
______________________________________________________________________________
Example
Import only the person1 dictionary from the module:
print (person1["age"])
PROGRAM :-
result = a + b
return result
#main.py
import add1, file
name = input("Enter the values?")
file.displayMsg(name)
print(add1.add(4,5.5))
OUTPUT :-
DATTA ME DATTA MEGHE COLLEGE OF ENGINEERING
Department of Information Technology
LAB MANUAL
______________________________________________________________________________
EXPERIMENT 11:
AIM :- To show Data visualization using Bar chart
THEORY :-
A bar plot or bar chart is a graph that represents the category of data with
rectangular bars with lengths and heights that is proportional to the values which
they represent. The bar plots can be plotted horizontally or vertically. A bar chart
describes the comparisons between the discrete categories. One of the axis of the
plot represents the specific categories being compared, while the other axis
represents the measured values corresponding to those categories.
______________________________________________________________________________
The function creates a bar plot bounded with a rectangle depending on the given
parameters.
PROGRAM :-
import numpy as np
plt.show()
OUTPUT :-
DATTA ME DATTA MEGHE COLLEGE OF ENGINEERING
Department of Information Technology
LAB MANUAL
______________________________________________________________________________