0% found this document useful (0 votes)
16 views

Writing Programs

Uploaded by

kambledjavi27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Writing Programs

Uploaded by

kambledjavi27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Writing Programs

1. Write a Python program to search a given string from the list of strings using recursion.
list = ['kabir', 'manav','nishtha', 'jayant', 'uday','daksh']

def search(nm, i):


if i<len(list):
#print(list[i])
#print(nm)
if list[i]==nm:
return int(i)
else:
return search(nm,i=i+1)
else:
return -1

name=input('Enter the string you want to search=')


res=search(name,0)
#print("res=",int (res))
if res==-1:
print('string not found')
else:
print('string is found at location=',res)
2. Write a Python program to input any 10 numbers and find out the second largest
number without using sort function.
def second_largest(num):
first=max(num[0],num[1])
second=min(num[0],num[1])
for n in num[2:] :
if n > first:
first, second = n, first
elif first > n > second:
second = n
return second

num=[]
#taking runtime input
print('Enter any 10 numbers')
for n in range(5):
item=input('num'+str(n+1)+'=')
num.append(item)
#calling function
sl=second_largest(num)
print('Second Largest Number=',sl)
3. Write a program to print first 10 prime numbers.
num=2
cnt=0
while(cnt<10):
prime = True
for i in range(2,num):
if (num%i==0):
prime = False
if prime:
print (num)
cnt+=1
num+=1
Output:

4. Write a program to demonstrate the concept of inheritance in python.


Rectangle.py
from Polygon import *

class Rectangle( Polygon ) :


def area( self ) :
return self.width * self.height
from Polygon import *

Triangle.py
class Triangle( Polygon ) :
def area( self ) :
return ( self.width * self.height ) / 2
Inherit.py
from Rectangle import *
from Triangle import *
rect = Rectangle()
trey = Triangle()
rect.set_values( 4 , 5 )
trey.set_values( 4 , 5 )
print( 'Rectangle Area:' , rect.area() )
print( 'Triangle Area:' , trey.area() )
5. Write a program to demonstrate the concept of method overriding in python.
class Parent(object):
def __init__(self): #double underscore
self.value = 4

def get_value(self):
print("Parent Class")
return self.value

class Child(Parent):
def get_value(self):
print("Child Class")
return self.value + 1

objc=Child()
print("value=",objc.get_value())

6. Write a Python program to print Fibonacci series for n terms using generator.
def fibonacci_generator() :
a=b=1
while True :
yield a
a,b=b,a+b

fib = fibonacci_generator()
for i in fib :
if i > 100 :
break
else :
print( i )
7. Write a program to create three buttons labeled as red, green and blue respectively.
Background color of the window should be change accordingly when user click on the
button.
from tkinter import *
window = Tk()
window.title( 'Button Example' )

def bgRed() :
window.configure( bg = 'red' )

def bgGreen() :
window.configure( bg = 'green' )

def bgBlue() :
window.configure( bg = 'blue' )

btn1 = Button( window , text = 'Red' , command = bgRed )


btn2 = Button( window , text = 'Green' , command = bgGreen )
btn3 = Button( window , text = 'Blue' , command = bgBlue)

btn1.pack( padx = 120 , pady = 20)


btn2.pack( padx = 120 , pady = 20)
btn3.pack( padx = 120 , pady = 20)

window.mainloop()
8. Write a program for gender selection using radio button. When the user clicks on
submit button it should display the selected gender on a label.
from tkinter import *
window = Tk()
window.geometry('300x200')
var = IntVar()

def sel():
if var.get()==1 :
selection = "You selected Male"
else:
selection = "You selected Female"
label2.config(text = selection)

label1 = Label(window, text="select Gender", bg="grey", fg="black") #.pack(side = "top")


label1.pack()

R1 = Radiobutton(window, text="Male", variable=var, value=1)


R1.pack()

R2 = Radiobutton(window, text="Female", variable=var, value=2)


R2.pack()

btn = Button( window , text = 'Submit' , command = sel )


btn.pack()

label2 = Label(window,text=' ')


label2.pack()
window.mainloop()
9. Write a program to select a course from list box. When the user clicks on submit
button, it should display the message about selected course.
from tkinter import *
import tkinter.messagebox as box

window = Tk()
window.geometry('200x200') #width*height
window.title( 'Listbox Example' )
def dialog() :
box.showinfo( 'Selection' , 'Your Choice: ' + \
lbox.get( lbox.curselection() ) )

lbox = Listbox(window,width=10,height=6)
lbox.insert(1, "Python")
lbox.insert(2, "Java")
lbox.insert(3, "C Sharp")
lbox.insert(4, "PHP")
lbox.insert(5, "HTML")

btn = Button( window, text = 'Submit', command=dialog )

lbox.pack(padx=10,pady=20)
btn.pack(padx=10,pady=10)
window.mainloop()
10.Write a program that allows the user select the items using checkbox. When the user
clicks on submit button it should display the selected items name.
from tkinter import *
import tkinter.messagebox as box
window = Tk()
window.title( 'Check Button Example' )
frame = Frame( window )
var1 = IntVar()
var2 = IntVar()
var3 = IntVar()
item1 = Checkbutton( frame , text = 'Joystick' ,variable = var1 , onvalue = 1 , offvalue = 0 )
item2 = Checkbutton( frame , text = 'Mouse' , variable = var2 , onvalue = 1 , offvalue = 0 )
item3 = Checkbutton( frame , text = 'Keyboard' , variable = var3 , onvalue = 1 , offvalue = 0 )

def dialog() :
str = 'Your Choice:'
if var1.get() == 1 : str += '\n Joystick'
if var2.get() == 1 : str += '\n Mouse'
if var3.get() == 1 : str += '\n Keyboard'
box.showinfo( 'Selection' , str )

btn = Button( frame , text = 'Submit' , command = dialog )

btn.pack( side = RIGHT , padx = 5 )


item1.pack( side = LEFT )
item2.pack( side = LEFT )
item3.pack( side = LEFT )
frame.pack( padx = 30, pady = 30 )

window.mainloop()

You might also like