0% found this document useful (0 votes)
37 views15 pages

Python Programming Basics and Examples

Contains programs for begginers
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views15 pages

Python Programming Basics and Examples

Contains programs for begginers
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

write a program to print Fibonacci series


n=input("enter a value:")

t1,t2=0,1

print"fibonacci series\n"

print"%d\n%d"%(t1,t2)

i=1

while i<=n:

t3=t1+t2

print"%d"%t3

t1,t2=t2,t3

i=i+1

OUTPUT:
Enter a value 5
Fibonacci series
0

8
2. sum and products of all items in the list
3. multiplication table of a number
n=input("enter a value:")
i=1
while i<=10:
print "%d*%d=%d"%(n,i,n*i)
i=i+1

OUTPUT:
Enter a value: 5

5*1=5

5*2=10

5*3=15

5*4=20

5*5=25

5*6=30

5*7=35

5*8=40

5*9=45

5*10=50
4. print largest and smallest items in the list
L=[1,2,3,5,3,1,6,8,99,10,2,5,13,67]

print L

print "smallest element:",min(L)

print "largest element:",max(L)

OUTPUT:
[1,2,3,5,3,1,6,8,99,10,2,5,13,67]
Smallest element: 1

Largest element: 99
5. Remove duplicates from list
l=[1,2,3,5,3,1,6,8,99,10,2,5,13,67]

s=[]

print l

for i in l:

if i not in s:

[Link](i)

print"list after removing duplictes:",s

OUTPUT:
[1,2,3,5,3,1,6,8,99,10,2,5,13,67]

List after removing duplicates :[1]

List after removing duplicates: [1,2]

List after removing duplicates:[1,2,3]

List after removing duplicates:[1,2,3,5]

List after removing duplicates:[1,2,3,5,6]

List after removing duplicates:[1,2,3,5,6,8]

List after removing duplicates:[1,2,3,5,6,8,99]

List after removing duplicates:[1,2,3,5,6,8,99,10]

List after removing duplicates:[1,2,3,5,6,8,99,10,13]

List after removing duplicates:[1,2,3,5,6,8,99,10,13,67]


6. Print a specified list after removing elements
[Link] ON CLASS AND OBJECTS
class student:

def __init__(self,n,a):

self.full_name=n

[Link]=a

def get_age(self):

return [Link]

f=student("bobsmith",23)

print f.full_name

print f.get_age()

OUTPUT:
[Link] ON THREADS
import thread

from time import sleep,ctime

def loop0():

print"start loop0 at:",ctime()

sleep(4)

print"loop0 done at:",ctime()

def loop1():

print"start loop1 at:",ctime()

sleep(2)

print"loop1 done at:",ctime()

def main():

print"starting at:",ctime()

thread.start_new_thread(loop0,())

thread.start_new_thread(loop1,())

sleep(6)

print"all done at:",ctime()

main()

OUTPUT:
[Link] FOR ACCESING ATTRIBUTES IN CLASS
class counter:

overall_total=0

def __init__(self):

self.my_total=0

def increment(self):

counter.overall_total+=1

self.my_total+=1

a=counter()

b=counter()

[Link]()

[Link]()

print a.my_total

print a.__class__.overall_total

print b.my_total

print b.__class__.overall_total

OUTPUT:
[Link] FOR ILLUSTRATING METHOD
OVERRIDING
class parent:

def mymethod(self):

print("calling parent mehod")

class child(parent):

def mymethod(self):

[Link](self)

print("calling child method")

c=child()

[Link]()

OUTPUT:
[Link] FOR ILLUSTRATING METHOD
OVERLOADING
def add(instanceof,*args):

if instanceof=='int':

result=0

if instanceof=='str':

result=''

for i in args:

result+=i

return result

print add('int',3,4,5)

print add('str','i','am','in','python')

OUTPUT:
[Link] HELLO WORLD CLICKING ON A BUTTON
from Tkinter import*
import tkMessageBox
top=Tk()
def message():
[Link]("helloworld")
button=Button(top,text='click',command=message)
[Link]()
[Link]()

OUTPUT:
36. GUI PROGRAM TO DISPLAY THE MESSAGE AFTER
SELECTING THE RADIO BUTTON
from Tkinter import*
def sel():
selection="you have selected the option",+str([Link]())
[Link](text=selection)
root=Tk()
var=IntVar()
R1=Radiobutton(root,text='option1',variable=var,value=1,co
mmand=sel)
[Link]()
R2=Radiobutton(root,text='option2',variable=var,value=2,co
mmand=sel)
[Link]()
R3=Radiobutton(root,text='option3',variable=var,value=3,co
mmand=sel)
[Link]()
label=Label(root)
[Link]()
[Link]()
OUTPUT:
[Link] DISPLAY LIST BOX
from Tkinter import*
top=Tk()
Lb1=Listbox(top)
[Link](1,"python")
[Link](2,"perl")
[Link](3,"c")
[Link](4,"php")
[Link](5,"jsp")
[Link](6,"ruby")
[Link]()
[Link]()

OUTPUT:
40. CHECK BUTTON
from Tkinter import*

top=Tk()

checkvar1=IntVar()

checkvar2=IntVar()

c1=Checkbutton(top,text='music',variable=checkvar1,onvalue=1,off
value=0,height=5,width=20)

c2=Checkbutton(top,text='video',variable=checkvar2,onvalue=1,off
value=0,height=5,width=20)

[Link]()

[Link]()

[Link]()

OUTPUT:

You might also like