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

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

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

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

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.'

Decimal Number to Binary number conversion

n=int(raw_input('Enter the Number'))
h=" "
while n>0:
  n1=n%2
  print 'R',n1
  n=n/2
  print 'V=',n
  h=h+str(n1)
print 'Binary Number is:',h[::-1]
 

Binary Number to Decimal Conversion

k=int(raw_input('Enter the binary number:'))
b=k
c=0
while b>0:
  b=b/10
  c=c+1
print c  
n=0
sum=0
while n<c:
  r=k%10
  k=k/10
  sum=sum + (r*(2**n))
  n=n+1
print 'Decimal Number is:', sum

Trigonometric Functions

import math
d=int(raw_input('Enter the angle in degree:'))
x= math.radians(d)
print 'Corresponding Radians is:',x           #Converts angle from degrees to radians.
f1=math.acos(x)                                                          # Return the arc cosine of x
print 'acos',f1
f2=math.asin(x)                                                           #Return the arc sine of x
print 'asin',f2
f3=math.atan(x)                                                          #Return the arc tangent of x
print 'atan',f3
f4=math.cos(x)                                                            #Return the cosine of x
print 'cos',f4
f5=math.sin(x)                                                             #Return the sine of x
print 'sin',f5
f6= math.tan(x)                                                            # Return the tangent of x
print 'tan',f6

u=int(raw_input('Enter the value of x:'))
v=int(raw_input('Enter the value of y:'))
e=math.hypot(u, v)                                                 #Return the Euclidean norm, sqrt(u*u + v*v)
print 'The Euclidean norm:',e

Accessing Values in Strings and Updating

var = 'Python Programming'
print "var[0]: ", var[0]
print "var[3:9]: ", var[3:9]
print "var[1:5]: ", var[1:5]
print "var[:]: ", var[:]           # List all elements in string
print "var[:2]: ", var[:2]       # List first 2 elements in string
print "var[2:]: ", var[2:]      # Except first 2 elements in string
print "var[::-1]: ", var[::-1]  # String Reverse
j=len(var)
print 'Length of string is:',j
i=0
while i<j:
      print 'Character in the string is:', var[i]
      i=i+1

x = 'Hello World!'

print "Updated String :- ", 'New ' + x[6:12]

Simple calculator using if elseif else and nested if else

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:
      c=a+b
      print 'Added value is:',c
elif n==2:
      if a>b:     #Nested if else
            c=a-b
            print 'Subtracted value is:',c
      else:
            c=b-a
            print 'Subtracted value is:',c
elif n==3:
      c=a*b
      print 'Multiplication value is:',c
elif n==4:
      if b==0:          #Nested if else
            print 'Division by zero not defined'
      else:
            c=float(a)/b
            print 'Divison value is:',c
elif n==5:
      c=a**b
      print 'Exponent value is:',c
else:
      print 'Invalid options'