Assignment 1
1.What is Object Oriented Programming in Python.
Ans:-
Python is a multi-paradigm programming language. It supports different
programming approaches.
One of the popular approaches to solve a programming problem is by creating
objects. This is known as Object-Oriented Programming (OOP).
An object has two characteristics:
● attributes
● behavior
Let's take an example:
A parrot is an object, as it has the following properties:
● name, age, color as attributes
● singing, dancing as behavior
The concept of OOP in Python focuses on creating reusable code. This
concept is also known as DRY (Don't Repeat Yourself).
In Python, the concept of OOP follows some basic principles:
2.What is Self Keyword in Python
Ans:-
In object-oriented programming, whenever we define methods for a
class, we use self as the first parameter in each case. Let's look at the
definition of a class called Cat.
In this case all the methods, including __init__, have the first parameter
as self.
We know that class is a blueprint for the objects. This blueprint can be used to
create multiple numbers of objects. Let's create two different objects from the
above class.
The self keyword is used to represent an instance (object) of the given class.
In this case, the two Cat objects cat1 and cat2 have their own name and age
attributes. If there was no self argument, the same class couldn't hold the
information for both these objects.
However, since the class is just a blueprint, self allows access to the
attributes and methods of each object in python. This allows each object to
have its own attributes and methods. Thus, even long before creating these
objects, we reference the objects as self while defining the class.
3.Explain Class and Object
Ans:-
Class:-
The class can be defined as a collection of objects. It is a logical entity that has some
specific attributes and methods. For example: if you have an employee class, then it
should contain an attribute and method, i.e. an email id, name, age, salary, etc.
Example:-
1. class ClassName:
2. <statement-1>
3. .
4. .
5. <statement-N>
Object:-
The object is an entity that has state and behavior. It may be any real-world object like
the mouse, keyboard, chair, table, pen, etc.
Everything in Python is an object, and almost everything has attributes and methods. All
functions have a built-in attribute __doc__, which returns the docstring defined in the
function source code.
When we define a class, it needs to create an object to allocate the memory. Consider
the following example.
Example:-
1. class car:
2. def __init__(self,modelname, year):
3. self.modelname = modelname
4. self.year = year
5. def display(self):
6. print(self.modelname,self.year)
7.
8. c1 = car("Toyota", 2016)
9. c1.display()
5.What is Inheritance
Ans:-
Inheritance of a feature of OOPs which allows classes to
inherit common properties from other classes.For Example If
there is a class such as “vehicle” other classes like
car,bike,etc can inherit common properties from the Vehicle
Class.This property helps you to get the rid of redundant code
thereby reducing the overall size of the code.
Example:-
class Polygon:
def __init__(self, no_of_sides):
self.n = no_of_sides
self.sides = [0 for i in range(no_of_sides)]
def inputSides(self):
self.sides = [float(input("Enter side "+str(i+1)+" : ")) for i in
range(self.n)]
def dispSides(self):
for i in range(self.n):
print("Side",i+1,"is",self.sides[i])
6.What is Init Method
Ans:-
The examples above are classes and objects in their simplest form, and
are not really useful in real life applications.
To understand the meaning of classes we have to understand the built-in
__init__() function.
All classes have a function called __init__(), which is always executed when the
class is being initiated.
Use the __init__() function to assign values to object properties, or other
operations that are necessary to do when the object is being created:
Example:-
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
print(p1.name)
print(p1.age)
7.What is Delete Function
Ans:-
The del keyword is used to delete objects. In Python everything is an object, so
the del keyword can also be used to delete variables, lists, or parts of a list etc.
Example:-
x = "hello"
del x
print(x)
8.Types of Inheritance in Python
Ans:-
The types of inheritance depend on the number of children and parents involved.
There are four kinds of inheritance available in Python:
Single Inheritance:- Single inheritance allows a derivate class to inherit properties of
one parent class, and this allows code reuse and the introduction of additional features
in existing code.
Multiple Inheritance :-Multiple inheritance comes into picture when a class inherits
more than one base class
Example:- A Class Defining a child inherits from two base classes mother and Father.
Multilevel inheritance,:- Multilevel inheritance mean a class inherits from another class
which it self is a subclass of some other base class
Example:- A Class describing a sport car will inherit from a base class caer which inturn
inherits another class Vehicle.
Hierarchical Inheritance:- Hierarchical inheritance refers to inheritance where one base
class has more than one subclass.For Example the vehicle class can have ‘car’,”bike”,etc
as its subclasses
9.What is Method Overriding
Ans:-
Method overriding is a feature of OOPs by which the child class or the subclass
can redefine present in the base class or parent class Here the method that is
overridden has the same name as well as the signature meaning the arguments
passed and the return type..
Example:-
class Parent(object):
def __init__(self):
self.value = 4
def get_value(self):
return self.value
class Child(Parent):
def get_value(self):
return self.value + 1
10.What is Super Keyword in Python
Ans:-
Following are three constraints that we must have to follow to use the super() function
in a Python program:
● The arguments are given in the super() function and the arguments in the
function that we have called should match.
● Every occurrence of the method that we are using should include the super()
keyword after we use it.
● We have to specify the class and methods present in it, which are referred to by
the super() function.
Following are the benefits of using a super() function in child class:
● We don't need to remember the parent's class name while using the super()
function. This is because we don't have to specify the name of the parent class to
access the methods present in it.
● We can use the super() function with the single inheritance and multiple
inheritances.
● The super() function in Python implements code reusability and modularity as
there is no need for us to rewrite the whole function again and again.
● The super() function in Python is known as dynamical function, as we all know
that Python is a dynamically typed programming language.
class Parent:
def __init__(self, txt):
self.message = txt
def printmessage(self):
print(self.message)
class Child(Parent):
def __init__(self, txt):
super().__init__(txt)
x = Child("Hello, and welcome!")
x.printmessage()
Q.11 What is OOPs in python.
Ans:-
Object-Oriented Programming is a type of programming that is based on
object rather than just function and procedures,individuals objects are
grouped into the class OOPs implement real-world entities like
Inheritance,Polymorphism,Hiding etc. into programming it also allow bining
data and code together.
Major principles of object-oriented programming system are given below.
● Class
● Object
● Method
● Inheritance
● Polymorphism
● Data Abstraction
● Encapsulation
Assignment-2
1.What is Tkinter in Python
Ans:-
Tkinter is the standard GUI library for Python. Python when combined
with Tkinter provides a fast and easy way to create GUI applications. Tkinter
provides a powerful object-oriented interface to the Tk GUI toolkit.
Creating a GUI application using Tkinter is an easy task. All you need to do is
perform the following steps −
● Import the Tkinter module.
● Create the GUI application main window.
● Add one or more of the above-mentioned widgets to the GUI
application.
○ Enter the main event loop to take action against each event triggered
by the user.
2. What is the use Tkinter Module.
Ans:-
3. Write down a Basic code to generate
label,entry,listbox,spinner,radiobutton,checkbox.
Ans:-
Radio Button:-
1. from tkinter import *
2.
3. def selection():
4. selection = "You selected the option " + str(radio.get())
5. label.config(text = selection)
6.
7. top = Tk()
8. top.geometry("300x150")
9. radio = IntVar()
10. lbl = Label(text = "Favourite programming language:")
11. lbl.pack()
12. R1 = Radiobutton(top, text="C", variable=radio, value=1,
13. command=selection)
14. R1.pack( anchor = W )
15.
16. R2 = Radiobutton(top, text="C++", variable=radio, value=2,
17. command=selection)
18. R2.pack( anchor = W )
19.
20. R3 = Radiobutton(top, text="Java", variable=radio, value=3,
21. command=selection)
22. R3.pack( anchor = W)
23.
24. label = Label(top)
25. label.pack()
26. top.mainloop()
LABEL:-
1. from tkinter.
2. import * a = Tk()
3. a.geometry("400x400")
4. a.title("test")
5. label = Label(a, text = "c# corner", bg = "green"\, bd = 100, fg = "white", font =
"Castellar")
6. label.pack()
7. a.mainloop()
List Box
from tkinter import *
top = Tk()
listbox = Listbox(top, height = 10,
width = 15,
bg = "grey",
activestyle = 'dotbox',
font = "Helvetica",
fg = "yellow")
top.geometry("300x250")
label = Label(top, text = " FOOD ITEMS")
listbox.insert(1, "Nachos")
listbox.insert(2, "Sandwich")
listbox.insert(3, "Burger")
listbox.insert(4, "Pizza")
listbox.insert(5, "Burrito")
label.pack()
listbox.pack()
top.mainloop()
Entry;-
import tkinter as tk
root = tk.Tk()
root.title("First Tkinter Window")
root.mainloop()
Check Box:-
from tkinter import *
root = Tk()
root.geometry("300x200")
w = Label(root, text ='GeeksForGeeks', font = "50")
w.pack()
Checkbutton1 = IntVar()
Checkbutton2 = IntVar()
Checkbutton3 = IntVar()
Button1 = Checkbutton(root, text = "Tutorial",
variable = Checkbutton1,
onvalue = 1,
offvalue = 0,
height = 2,
width = 10)
Button2 = Checkbutton(root, text = "Student",
variable = Checkbutton2,
onvalue = 1,
offvalue = 0,
height = 2,
width = 10)
Button3 = Checkbutton(root, text = "Courses",
variable = Checkbutton3,
onvalue = 1,
offvalue = 0,
height = 2,
width = 10)
Button1.pack()
Button2.pack()
Button3.pack()
mainloop()
Spinner:-
current_value = tk.StringVar(value=0)
spin_box = ttk.Spinbox(
container,
from_=0,
to=30,
textvariable=current_value,
wrap=True)
Q.4 What is difference between radiobutton and checkbox?
Ans:-
Sr.N Radio button Checkbox Button
o
1 It is used only when we have one It is used only when we have one or many
option to be selected from options to be selected, for Example,
several available options, for Hobby.
Example, gender, etc.
2 It is created using an HTML tag It is also created using an HTML tag
named <input>, and the type named <input>, and the type attribute is
attribute is set to the radio. set to Checkbox.
Syntax: Syntax:
<input type = "radio" name = <input type = "checkbox" name =
"radio_name" value = "choice_id"> "checkbox_name" value = "choice_id"
checked>
3 It is represented as small circle It is represented as a small square box on
buttons on the screen. the screen.
4 There are two states in the Radio There are three states in the Checkbox.
button. It can be either true or These areas are Checked, unchecked &
false. indeterminate.
5 It is used to limit the user's It is used when you want to allow the
choice to select only one option user to select more than one option or
from the list of the options none of the options from the list of
provided in the radio box. choices in the Checkbox.
Q.5. Explain the message box in python.
Ans:-
The messagebox module is used to display the message boxes in the python
applications. There are the various functions which are used to display the relevant
messages depending upon the application requirements.
The syntax to use the messagebox is given below.
Syntax:-messagebox.function_name(title, message [, options])
Parameters
● function_name: It represents an appropriate message box function.
● title: It is a string which is shown as a title of a message box.
● message: It is the string to be displayed as a message on the message box.
● options: There are various options which can be used to configure the message
dialog box.
The two options that can be used are default and parent.
1. default
The default option is used to mention the types of the default button, i.e. ABORT, RETRY,
or IGNORE in the message box.
2. parent
The parent option specifies the parent window on top of which, the message box is to
be displayed.
Q.6 Write a code with label ,Entry and Button to display
square and cube of given number.
Ans:-