l1=list()
l2=list()
w=int(raw_input("Enter the elements in list:"))
for i in range(w):
n=int(raw_input("Enter the list:"))
l1.append(n)
for i in l1:
if i not in l2:
l2.append(i)
print ' Delete Duplicate numbers in a list: ', l2
Solve Problems by Coding Solutions - A Complete solution for python programming
Program to check the Perfect Number or Not
num=input("Enter the number:")
i=1
while i<=num/2:
s=i**2
if s==num:
print "perfect square"
break
i+=1
else:
print "Not a perfect square"
i=1
while i<=num/2:
s=i**2
if s==num:
print "perfect square"
break
i+=1
else:
print "Not a perfect square"
Program to find even number using functions without return
def even(n):
if n%2==0:
print 'Even number'
else:
print 'Odd Number'
n=int(raw_input('Enter the number to check even or not:'))
even(n) # No return from function but the message is displayed inside the function itself
if n%2==0:
print 'Even number'
else:
print 'Odd Number'
n=int(raw_input('Enter the number to check even or not:'))
even(n) # No return from function but the message is displayed inside the function itself
Simple Calculator using functions
def su(a,b):
c=a+b
print 'Added value is:',c
def subt(a,b):
if a>b:
c=a-b
print 'Subtracted value is:',c
else:
c=b-a
print 'Subtracted value is:',c
def mul(a,b):
c=a*b
print 'Multiplication value is:',c
def div(a,b):
if b==0:
print 'Division by zero not defined'
else:
c=float(a)/b
print 'Divison value is:',c
def exp(a,b):
c=a**b
print 'Exponent value is:',c
a=int(raw_input('Enter the value of a:'))
b=int(raw_input('Enter the value of b:'))
print ' 1. add 2.sub 3.mul 4. div 5.exp'
n=int(raw_input('Enter the choice:'))
if n==1:
su(a,b) # Function call
elif n==2:
subt(a,b) # Function call
elif n==3:
mul(a,b) # Function call
elif n==4:
div(a,b) # Function call
elif n==5:
exp(a,b) # Function call
else:
print 'Invalid options'
c=a+b
print 'Added value is:',c
def subt(a,b):
if a>b:
c=a-b
print 'Subtracted value is:',c
else:
c=b-a
print 'Subtracted value is:',c
def mul(a,b):
c=a*b
print 'Multiplication value is:',c
def div(a,b):
if b==0:
print 'Division by zero not defined'
else:
c=float(a)/b
print 'Divison value is:',c
def exp(a,b):
c=a**b
print 'Exponent value is:',c
a=int(raw_input('Enter the value of a:'))
b=int(raw_input('Enter the value of b:'))
print ' 1. add 2.sub 3.mul 4. div 5.exp'
n=int(raw_input('Enter the choice:'))
if n==1:
su(a,b) # Function call
elif n==2:
subt(a,b) # Function call
elif n==3:
mul(a,b) # Function call
elif n==4:
div(a,b) # Function call
elif n==5:
exp(a,b) # Function call
else:
print 'Invalid options'
Sum of digits of the a given number
n=int(raw_input('Enter the number: '))
sum=0
while n>0:
rem=n%10
sum=sum+rem
n=n/10
print 'Sum of digits of the number is: ',sum
sum=0
while n>0:
rem=n%10
sum=sum+rem
n=n/10
print 'Sum of digits of the number is: ',sum
Car Class Program
# define the Vehicle class
class Vehicle:
name = "" # initial value setting inisde the clas iteself
kind = "car" # initial value setting inisde the clas iteself
color = "" # initial value setting inisde the clas iteself
value = 100.00 # initial value setting inisde the clas iteself
def description(self): # creating function inside the class amd return as string
desc_str = "%s is a %s %s worth $%.2f." % (self.name, self.color, self.kind, self.value)
return desc_str
car= Vehicle() # creates a object [car1] for class vehicle
print car.description() # using created object calling the function inside class
car1 = Vehicle() # creates a object car for class vehicle
car1.name = "Fer" # using object[car1] variables on the class vehicle is assigned
car1.color = "red" # using object[car1] variables on the class vehicle is assigned
car1.kind = "convertible" # using object[car1] variables on the class vehicle is assigned
car1.value = 60000.00 # using object[car1] variables on the class vehicle is assigned
car2 = Vehicle()
car2.name = "Jump"
car2.color = "blue"
car2.kind = "van"
car2.value = 10000.00
print car.description() # Using object [car] created calling the functions[description()]inside the class
print car1.description() # Using object [car1] created calling the functions[description()]inside the class
print car2.description() # Using object [car2] created calling the functions[description()]inside the class
class Vehicle:
name = "" # initial value setting inisde the clas iteself
kind = "car" # initial value setting inisde the clas iteself
color = "" # initial value setting inisde the clas iteself
value = 100.00 # initial value setting inisde the clas iteself
def description(self): # creating function inside the class amd return as string
desc_str = "%s is a %s %s worth $%.2f." % (self.name, self.color, self.kind, self.value)
return desc_str
car= Vehicle() # creates a object [car1] for class vehicle
print car.description() # using created object calling the function inside class
car1 = Vehicle() # creates a object car for class vehicle
car1.name = "Fer" # using object[car1] variables on the class vehicle is assigned
car1.color = "red" # using object[car1] variables on the class vehicle is assigned
car1.kind = "convertible" # using object[car1] variables on the class vehicle is assigned
car1.value = 60000.00 # using object[car1] variables on the class vehicle is assigned
car2 = Vehicle()
car2.name = "Jump"
car2.color = "blue"
car2.kind = "van"
car2.value = 10000.00
print car.description() # Using object [car] created calling the functions[description()]inside the class
print car1.description() # Using object [car1] created calling the functions[description()]inside the class
print car2.description() # Using object [car2] created calling the functions[description()]inside the class
Simple Class Program in Python
class MyClass: # class is created
variable = "blah" # variable is created and assigned the value
def function(self): # funtion is created iniside the class as self
print "This is a message inside the class."
myobjectx = MyClass() # object[myobjectx] is created for class
myobjectx.variable # using created object variable in the class is accessed
myobjecty = MyClass() # object[myobjecty] is created for class
myobjecty.variable = "yackity" # using created object variable in the class new value is assigned
print myobjectx.variable # using corresponding object and variable the value is displayed
print myobjecty.variable # using corresponding object and variable the value is displayed
print MyClass.variable # using corresponding object and variable the value is displayed
myobjectx.function() # using corresponding object and function inside the class is accessed
variable = "blah" # variable is created and assigned the value
def function(self): # funtion is created iniside the class as self
print "This is a message inside the class."
myobjectx = MyClass() # object[myobjectx] is created for class
myobjectx.variable # using created object variable in the class is accessed
myobjecty = MyClass() # object[myobjecty] is created for class
myobjecty.variable = "yackity" # using created object variable in the class new value is assigned
print myobjectx.variable # using corresponding object and variable the value is displayed
print myobjecty.variable # using corresponding object and variable the value is displayed
print MyClass.variable # using corresponding object and variable the value is displayed
myobjectx.function() # using corresponding object and function inside the class is accessed
A database with integer key using pickle
import anydbm
db = anydbm.open('integer.db', 'c')
import pickle
# A limitation of anydbm is that the keys and values have to be strings.
# If you try to use any other type, you get an error
# The pickle module can help. It translates almost any type of object into a string suitable
# for storage in a database, and then translates strings back into objects.
k = 1
f=pickle.dumps(k)
db[f] = 'Babu'
print 'Value:',db[f]
print 'Key:',f
db.close()
r=pickle.loads(f)
print 'Key Value back:',r
db = anydbm.open('integer.db', 'c')
import pickle
# A limitation of anydbm is that the keys and values have to be strings.
# If you try to use any other type, you get an error
# The pickle module can help. It translates almost any type of object into a string suitable
# for storage in a database, and then translates strings back into objects.
k = 1
f=pickle.dumps(k)
db[f] = 'Babu'
print 'Value:',db[f]
print 'Key:',f
db.close()
r=pickle.loads(f)
print 'Key Value back:',r
Create database and store key value pairs
import anydbm
db = anydbm.open('captions.db', 'c')
db['c1'] = 'Photo of John Cleese.'
print db['c1']
db['c2'] = 'Photo of John Cleese doing a silly walk.'
print db['c2']
for key in db:
print key
db.close()
Value Meaning
'r' Open existing database for reading only (default)
'w' Open existing database for reading and writing
'c' Open database for reading and writing, creating it if it doesn’t exist
'n' Always create a new, empty database, open for reading and writing
anydbm in python.org package
dbm in anaconda package
db = anydbm.open('captions.db', 'c')
db['c1'] = 'Photo of John Cleese.'
print db['c1']
db['c2'] = 'Photo of John Cleese doing a silly walk.'
print db['c2']
for key in db:
print key
db.close()
Value Meaning
'r' Open existing database for reading only (default)
'w' Open existing database for reading and writing
'c' Open database for reading and writing, creating it if it doesn’t exist
'n' Always create a new, empty database, open for reading and writing
anydbm in python.org package
dbm in anaconda package
To use try except for error handling
try:
s=0
g=s/0 # Division by zero creates error
print 'Value of g :',g
except:
print 'Something went wrong.'
try:
s=0
g=s/10 # Division by zero by 10 creates no error
print 'Value of g :',g
except:
print 'Something went wrong.'
s=0
g=s/0 # Division by zero creates error
print 'Value of g :',g
except:
print 'Something went wrong.'
try:
s=0
g=s/10 # Division by zero by 10 creates no error
print 'Value of g :',g
except:
print 'Something went wrong.'
Subscribe to:
Posts (Atom)