Sunteți pe pagina 1din 33

#!

/usr/bin/python

a=10

b=10

if (a == b): print("True")

print("End of Program")

#! /usr/bin/python

a=10

b=10

if (a == b):

print("True")

print("End of Program")

----------------------------------module--------------------------------

#! /usr/bin/python

import math;

print(max(9.2,11.3,-22,34,-9.03));

print(min(9.2,11.3,-22,34,-9.03));

print(math.exp(3));

#! /usr/bin/python

import math;

print(cmp(10,20)) -1

print(cmp(20,10)) 1

print(cmp(20,20)) 0

#! /usr/bin/python

import random

print (random.random());

#! /usr/bin/python
import random

print (random.randrange(1,100));

#! /usr/bin/python

list = ['Sumit',1,11.2,1997,"Sumana"]

print ("list[0] :",list[0]);

print ("list[1:4] :",list[1:4]);

print ("len(list) :",len(list));

for i in range(0,len(list)):

print(list[i]);

for i in list:

print(i);

#! /usr/bin/python

list = ['Sumit',1,11.2,1997,"Sumana"]

print(list);

#list[2]="New Data"; #update

#list[5]=99; #Appending

#list[-1]=99; #Appending

#list.append(99);

del list[3] ; #deleting

print(list);

#del list;

#print(list);

list.insert(2,"Sachin");

print(list);

#! /usr/bin/python

list = ['Sumit',1,11.2,1997,"Sumana"]

print ("list[-1] :",list[-1]);


print ("list[-2] :",list[-2]);

print ("list[2:] :",list[2:]);

print ("list[::1] :",list[::1]);

print ("list[::2] :",list[::2]);

print ("list[::-1] :",list[::-1]);

print ("list[::-2] :",list[::-2]);

#! /usr/bin/python

list = ['Sumit',"1","11.2","1997","Sumana"]

print ("list :",list);

list.reverse();

print ("list :",list);

list.sort();

print ("list :",list);

--------------------tuple adding with list--------------------------

tuple1 = ('Sumit',1,11.2,1997,"Sumana")

list2 = ['Sam',2,11.4,1998,"Suman"]

list3=list(tuple1)+list2

print(list3);

----------------------dict------------------------------

#! /usr/bin/python

dict = {11:"Sumit",'pooja':453,11.2:'Sachin'}

print (dict);

print (dict.keys());

for i in dict.keys():

print(dict[i]);

-------------------------------------Func------------------------------------

#! /usr/bin/python

def add(a,b):

c=a+b
print("The addition is ",c);

print("Main Starts.....");

x = input("Enter The First Number")

y = input("Enter The Second Number")

x=int(x)

y=int(y)

add(x,y)

print("Main Ends.....");

-----------------------------call by value--------------

#! /usr/bin/python

def add(temp):

c=temp[0]+temp[1]

print("The addition is ",c);

temp[0]=89;

temp[1]=67;

return c;

print("Main Starts.....");

l1=[0,0];

l1[0] = int(input("Enter The First Number"))

l1[1] = int(input("Enter The Second Number"))

print("Before x=%d and y = %d"%(l1[0],l1[1]));

z=add(l1)

print("After x=%d and y = %d"%(l1[0],l1[1]));

print("In Main ... The addition is ",z);

print("Main Ends.....");

---------------------return in function----------------
#! /usr/bin/python

def add_sub(a,b):

c=a+b

d=a-b
print("The addition is ",c);

print("The subtraction is ",d);

return c,d;

print("Main Starts.....");

x = input("Enter The First Number")

y = input("Enter The Second Number")

x=int(x)

y=int(y)

z=add_sub(x,y)

print(z);

print("Main Ends.....");

-------------------position and named based--------------------


#! /usr/bin/python

def add(str,int,flot):

print("The String is ",str);

print("The integer is ",int);

print("The float is ",flot);

return

print("Main Starts ")

#add('new',22,44.9)

add(22,44.9,'new')

add(int=22,flot=44.9,str='new')

print("Main Ends ")

---------------default parameter----------------------------
default parameter should be an last parameter

#! /usr/bin/python

def add(str,int,flot=22.34):

print("The String is ",str);

print("The integer is ",int);

print("The float is ",flot);

return
print("Main Starts ")

add("New",22,44.9)

add("New",22)

#add(22,44.9,'new')

#add(int=22,flot=44.9,str='new')

print("Main Ends ")

#! /usr/bin/python

def add(str="Demo",int=0,flot=22.34):

print("The String is ",str);

print("The integer is ",int);

print("The float is ",flot);

return

print("Main Starts ")

add("New",22,44.9)

add("New",22)

add()

#add(22,44.9,'new')

#add(int=22,flot=44.9,str='new')

print("Main Ends ")

------------------any number of arguments *args-------------------

*args is tuple data type to hold any number of value


below one parameter is fixed and min 1 , max any number more then one

#! /usr/bin/python

def add(x,*vla):

sum=x;

for temp in vla:

sum=sum+temp;

print("The Sum is ",sum);

return sum
print("Main Starts ")

add(10);

add(10,20);

add(10,20,30);

print("Main Ends ")

min 0 , max any number more then 0

#! /usr/bin/python

def add(*mnp):

sum=0;

for temp in mnp:

sum=sum+temp;

print("The Sum is ",sum);

#print (mnp);

return sum

print("Main Starts ")

add();

add(10);

add(10,20);

add(10,20,30);

----------------------------------lamada function-------------------------
it is one line function

#! /usr/bin/python

multi = lambda a,b : a*b

print ("The Multiplication is ",multi(10,20))

x=multi(8,3)

print ("The Multiplication is ",x)

----------------------------------local/global variable--------------

global and local x are two different variable . in func x declare and
destroy after end of it

#! /usr/bin/python

def sample():
x=2 #Local

x += 1;

print ("X in Function",x);

print ("Main Starts");

x=10 #global

sample()

print ("X in Main",x);

print ("Main Ends");

If we have to use global variable in function like reference:

#! /usr/bin/python

def sample():

global x;

x += 1;

print ("X in Function",x);

print ("Main Starts");

x=10 #global

sample()

print ("X in Main",x);

print ("Main Ends");

----------------------------------Modules------------------------------------

-----function in file named as Maths------


def add(a,b):

print("In add of Module Maths")

c=a+b;

print("The addition is ",c);

return c;

def sub(a,b):

print("In sub of Module Maths")

c=a-b;

print("The Subtraction is ",c);


return c;

-----main code in same location---------

#! /usr/bin/python

import Maths;

print ("Main Starts");

x=int(input("Enter First No ..."));

y=int(input("Enter Second No ..."));

z=Maths.add(x,y);

print ("The addition is ",z);

z=Maths.sub(x,y);

print ("The Subtraction is ",z);

print ("Main Ends");

-----importing particular function from module-----

#! /usr/bin/python

from Maths import add;

from Maths import sub;

print ("Main Starts");

x=int(input("Enter First No ..."));

y=int(input("Enter Second No ..."));

z=add(x,y); #we dont have to used math.add

print ("The addition is ",z);

z=sub(x,y); #we dont have to used math.sub

print ("The Subtraction is ",z);

print ("Main Ends");

------importing all func from math in our code ------

#! /usr/bin/python

from Maths import *;

print ("Main Starts");

x=int(input("Enter First No ..."));


y=int(input("Enter Second No ..."));

z=add(x,y); # from maths all func are imported

print ("The addition is ",z);

z=sub(x,y);

print ("The Subtraction is ",z);

print ("Main Ends");

# Modules are collection to Functions and packages are collection of modules

-----------------------------Packages----------------------------------------

Packages must have __init__.py so That we an make difference between


directory and packages

-----main code---

#! /usr/bin/python

import Arithmatic; #arithmatic is package

print ("Main Starts");

x=int(input("Enter First No ..."));

y=int(input("Enter Second No ..."));

z=Arithmatic.addition.add(x,y);

print ("The addition is ",z);

z=Arithmatic.subtraction.sub(x,y);

print ("The Subtraction is ",z);

print ("Main Ends");

#inside arithmatic we have __init__.py,addition.py,subtraction.py and so on

which contain the functions add,sub,mult,div

-----__init__.py-------
from Arithmatic import addition

from Arithmatic import subtraction

from Arithmatic import multiplication

from Arithmatic import division

-----main code----

#! /usr/bin/python
from Arithmatic import addition;

from Arithmatic import *;

print ("Main Starts");

x=int(input("Enter First No ..."));

y=int(input("Enter Second No ..."));

z=addition.add(x,y);

print ("The addition is ",z);

z=division.div(x,y);

print ("The division is ",z);

print ("Main Ends");

---------------------------------File Handling------------------------------

#! /usr/bin/python

fhandler=open("sample.txt");

ch=fhandler.read(1);#read first char and put it in ch

while(ch):

print(ch,end="");print value of ch and continue on same line

ch=fhandler.read(1);#read next char and put in ch

fhandler.close();

-----read 4 char at a time-----

#! /usr/bin/python

fhandler=open("sample.txt","r");

ch=fhandler.read(4);

while(ch):

#print(ch,end="");

print(ch);

ch=fhandler.read(4);

fhandler.close();

-----read line by line-------

#! /usr/bin/python

fhandler=open("sample.txt","r");
ch=fhandler.readline();

while(ch):

print(ch,end="");

#print(ch);

ch=fhandler.readline();

fhandler.close();

----tell func of file-----

#! /usr/bin/python

fhandler=open("sample.txt","r");

ch=fhandler.readline();

while(ch):

print(ch,end="");

#print(ch);

print(fhandler.tell())

ch=fhandler.readline();

fhandler.close();

-----writing in file-----

#! /usr/bin/python

fhandler=open("sample.txt","w");

fhandler.write("Good Morning....");

fhandler.write("How are you?");

fhandler.write("We are Good ");

fhandler.close();
output:-
Good Morning....How are you?We are Good

----writing in file with new line-----

#! /usr/bin/python

fhandler=open("sample.txt","w");

fhandler.write("Good Morning....");

fhandler.write("\n");
fhandler.write("How are you?");

fhandler.write("\n");

fhandler.write("We are Good ");

fhandler.write("\n");

fhandler.close();
output:-
Good Morning....

How are you?

We are Good

-----renaming the file----

#! /usr/bin/python

import os;

os.rename("sample.txt","demo.txt");

mkdir= make directory


rmdir= removing or deleting only empty directory

-------------------------------Exception handling---------------------------

#! /usr/bin/python

def age_calc(a):

assert (a>0 and a<=120), "Enter Valid Age"

c=a/7.24*5;

return (c)

print("Main Starts ")

x = input("Enter The age ")

x=int(x)

z=age_calc(x)

print("The cal is ",z)

print("Main Ends ")

----Exception---
if exception occurs the exception block will execute and then the
program flow goes normally {Try to catch ValueError also}
#! /usr/bin/python

def div(a,b):

c=a/b

return (c)

print("Main Starts ")

x = input("Enter The First Number")

y = input("Enter The Second Number")

try:

x=int(x)

y=int(y)

z=div(x,y)

print("The division is ",z)

except ZeroDivisionError:

print ("Error : Enter The valid Denominator")


except ValueError:

print ("Error : Enter integer value")

print("Main Ends ")

----Exception to catch every type of error-----

but problem is that we donot able to know what the error is


because it wont give type of error we can use this with other
Exception to catch remaining Error

#! /usr/bin/python

def div(a,b):

c=a/b

return (c)

print("Main Starts ")

x = input("Enter The First Number")

y = input("Enter The Second Number")

try:

x=int(x)
y=int(y)

z=div(x,y)

print("The division is ",z)

except:

print ("Error : Error in Code ")

print("Main Ends ")


-------Finallly---------------

# Try,Catch,Exception and Finally all are block

#finally is used to release the resource or anything code , file


#after completion of code

#! /usr/bin/python

def div(a,b):

c=a/b

return (c)

print("Main Starts ")

x = input("Enter The First Number")

y = input("Enter The Second Number")

try:

x=int(x)

y=int(y)

z=div(x,y)

print("The division is ",z)

except ZeroDivisionError:

print ("Error : Enter The valid Denominator")

finally:

print ("I am in Finally Block ")

print("Main Ends ")

-----Raise------

#Creating raising and then catching our own exception


#Creating exception must be class

#! /usr/bin/python
class CreditCardError(RuntimeError): #creating own exception

def __init__(self,arg):

self.args = arg

print("Main Starts ")

x = input("Enter The CC Number Number")

try:

x=int(x)

if (x==0):

raise CreditCardError("Enter Valid CC Number") #raising own exception

print("CC Accepted")

except CreditCardError: #catching own exception

print ("CC Failure");

finally:

print ("I am in Finally Block ")

print("Main Ends ")

-----print message given in Raise------

#! /usr/bin/python

class CreditCardError(RuntimeError):

def __init__(self,arg):

self.args = arg

print("Main Starts ")

x = input("Enter The CC Number Number")

try:

x=int(x)

if (x==0):

raise CreditCardError("Enter Valid CC Number")

print("CC Accepted")

except CreditCardError as e:

print ("CC Failure",e.args);

finally:
print ("I am in Finally Block ")

print("Main Ends ")

----------Catching exception clean code------


#try is block in which we write code of part we thing error comes
ones exception occur it execute the except block and stopthe
remainig try block execution
#! /usr/bin/python

def div(a,b):

try:

c=a/b

except ZeroDivisionError:

print ("Error : Enter The valid Denominator1")

return 0

return (c)

print("Main Starts ")

x = input("Enter The First Number")

y = input("Enter The Second Number")

try:

x=int(x)

y=int(y)

z=div(x,y)

print("The division is ",z)

except ZeroDivisionError:

print ("Error : Enter The valid Denominator")

finally:

print ("I am in Finally Block ")

print("Main Ends ")

----nested try---------
#! /usr/bin/python

def div(a,b):

c=a/b
return (c)

print("Main Starts ")

x = input("Enter The First Number")

y = input("Enter The Second Number")

try:

x=int(x)

y=int(y)

try:

z=div(x,y)

except ZeroDivisionError:

print ("Exception in Inner Block")

z=0;

print("The division is ",z)

except:

print ("Exception in Outer Block")

finally:

print ("I am in Finally Block ")

print("Main Ends ")

---------------------------Classes and Objects-------------------------------

#! /usr/bin/python

class Rectangle:

def __init__(self,length,breadth):

self.l=length;

self.b=breadth;

def Area(self):

return (self.l*self.b);

def Perimeter(self):

return (2*(self.l+self.b));

def Display(self):

print("The Length is %d and Breadth is %d"%(self.l,self.b));


print("Main Starts...");

r1=Rectangle(10,20);

r1.Display();

print("The area is ",r1.Area());

print("The area is ",r1.Perimeter());

print("Main Ends...");

---- more than one instance of class(object)------------


#! /usr/bin/python

class Rectangle:

def __init__(self,length,breadth):

self.l=length;

self.b=breadth;

def Area(self):

return (self.l*self.b);

def Perimeter(self):

return (2*(self.l+self.b));

def Display(self):

print("The Length is %d and Breadth is %d"%(self.l,self.b));

print("Main Starts...");

r1=Rectangle(10,20);

r2=Rectangle(11,22);

r1.Display();

r2.Display();

print("The area is ",r1.Area());

print("The area is ",r1.Perimeter());

print("Main Ends...");

-------private and public------


# private : the variable is access only with in class
# public : the variable is access outside the class also
to make variable private use '__' in front of it
#! /usr/bin/python

class Rectangle:
def __init__(self,length,breadth):

self.__l=length;

self.b=breadth;

def Area(self):

return (self.__l*self.b);

def Perimeter(self):

return (2*(self.__l+self.b));

def Display(self):

print("The Length is %d and Breadth is %d"%(self.__l,self.b));

print("Main Starts...");

r1=Rectangle(10,20);

r1.Display();

print("The area is ",r1.Area());

print("The area is ",r1.Perimeter());

print("The Length is ",r1.__l);

print("The Breadth is ",r1.b);

print("Main Ends...");

-----class variable and object variable-----


# we have created the object but count created at first object creation
after that it does not initailize

#! /usr/bin/python

class Rectangle:

count=0;# class variable

def __init__(self,length,breadth):

self.__l=length;

self.b=breadth;

Rectangle.count+=1;

def Area(self):

return (self.__l*self.b);

def Perimeter(self):
return (2*(self.__l+self.b));

def Display(self):

print("The Length is %d and Breadth is %d"%(self.__l,self.b));

print("Main Starts...");

r1=Rectangle(10,20);

print("The Count is is ",r1.count);#displaying class variable

#print("The Count is is ",Rectangle.count);#same as above

r2=Rectangle(10,20);

print("The Count is is ",r2.count);

r3=Rectangle(10,20);

print("The Count is is ",r3.count);

print("The area is ",r1.Area());

print("The area is ",r1.Perimeter());

#print("The Length is ",r1.__l);

#print("The Breadth is ",r1.b);

print("Main Ends...");

-----destructor---------
Its is used to free up the memory space taken by object
after completion of work (__del__ default method called destructor)

#! /usr/bin/python

class Rectangle:

count=0;

def __init__(self,length,breadth):

self.__l=length;

self.b=breadth;

Rectangle.count+=1;

def Area(self):

return (self.__l*self.b);

def Perimeter(self):

return (2*(self.__l+self.b));

def Display(self):
print("The Length is %d and Breadth is %d"%(self.__l,self.b));

def __del__(self):

print ("I am in Destructor ")

print("Main Starts...");

r1=Rectangle(10,20);

print("The Count is is ",r1.count);

r2=Rectangle(10,20);

print("The Count is is ",r2.count);

r3=Rectangle(10,20);

print("The Count is is ",r3.count);

print("The area is ",r1.Area());

print("The area is ",r1.Perimeter());

#print("The Length is ",r1.__l);

#print("The Breadth is ",r1.b);

print("Main Ends...");

---------Constructor overloading-----
Constructor overloading will not present in python
so we cannot use two constructor with different args

for two constructor with different argument

#! /usr/bin/python

class Rectangle:

count=0;

def __init__(self):

self.__l=0;

self.b=0;

Rectangle.count+=1;

def __init__(self,length,breadth):

self.__l=length;

self.b=breadth;
Rectangle.count+=1;

def Area(self):

return (self.__l*self.b);

def Perimeter(self):

return (2*(self.__l+self.b));

def Display(self):

print("The Length is %d and Breadth is %d"%(self.__l,self.b));

def __del__(self):

print ("I am in Destructor ")

print("Main Starts...");

r1=Rectangle(10,20);

print("The Count is is ",r1.count);

r2=Rectangle(10,20);

print("The Count is is ",r2.count);

r3=Rectangle(10,20);

print("The Count is is ",r3.count);

r4=Rectangle();

print("The Count is is ",r4.count);

print("The area is ",r1.Area());

print("The area is ",r1.Perimeter());

#print("The Length is ",r1.__l);

#print("The Breadth is ",r1.b);

print("Main Ends...");

--------inheritance------
#inherit method from parents class
#! /usr/bin/python
# below with out inheritance
class Rectangle:

'Class to Create Rectangle'

def __init__(self):

self.l=0

self.b=0
print ("I am in Constructor of Rectangle")

def __init__(self,length,breadth):

self.l=int(length)

self.b=int(breadth)

print ("I am in Constructor of Rectangle")

def Area(self):

return (self.l*self.b);

def Perimeter(self):

return (2*(self.l+self.b));

def Display(self):

print("The Length %d and Breadth %d "%(self.l,self.b));

def __del__(self):

print ("I am in Destructor of Rectangle")

class Cuboid(Rectangle):

'Class to Create Cuboid'

def __init__(self,length,breadth,height):

self.l=int(length)

self.b=int(breadth)

self.h=int(height)
print ("I am in Constructor of Cuboid")

def Volume(self):

return (self.l*self.b*self.h);

def Display1(self):

print("The L= %d B= %d c= %d"%(self.l,self.b,self.h));

def __del__(self):

print ("I am in Destructor of Cuboid")

print("Main Starts ")

rect1 = Rectangle(10,20);

rect1.Display();

print ("The area is ",rect1.Area());


print ("The Perimeter is ",rect1.Perimeter());

cub1= Cuboid(11,22,33);

cub1.Display();

print ("The area is ",cub1.Area());

print ("The Perimeter is ",cub1.Perimeter());

print ("The volume is ",cub1.Volume());

cub1.Display1();

print("Main Ends")

----------with inheritance----
# below is single level inheritance
# if some new method use cuboid also then it is multilevel
# like class new(cuboid)
# new(rectangle) and cuboid(rectangle) then hierarical inheritance
# new (cuboid,rectangle) then multiple inheritance

#! /usr/bin/python

class Rectangle:

'Class to Create Rectangle'

def __init__(self,length,breadth):

self.l=int(length)

self.b=int(breadth)

print ("I am in Constructor of Rectangle")

def Area(self):

return (self.l*self.b);

def Perimeter(self):

return (2*(self.l+self.b));

def Display(self):

print("The Length %d and Breadth %d "%(self.l,self.b));

def __del__(self):

print ("I am in Destructor of Rectangle")

class Cuboid(Rectangle):

'Class to Create Cuboid'

def __init__(self,length,breadth,height):
Rectangle.__init__(self,length,breadth);

self.h=int(height)

print ("I am in Constructor of Cuboid")

def Volume(self):

return (self.l*self.b*self.h);

def Display1(self):

print("The L= %d B= %d c= %d"%(self.l,self.b,self.h));

def __del__(self):

print ("I am in Destructor of Cuboid")

print("Main Starts ")

rect1 = Rectangle(10,20);

rect1.Display();

print ("The area is ",rect1.Area());

print ("The Perimeter is ",rect1.Perimeter());

cub1= Cuboid(11,22,33);

cub1.Display();

print ("The area is ",cub1.Area());

print ("The Perimeter is ",cub1.Perimeter());

print ("The volume is ",cub1.Volume());

cub1.Display1();

print("Main Ends")

-----method overloading----

#! /usr/bin/python

class Rectangle:

'Class to Create Rectangle'

def __init__(self,length,breadth):

self.l=int(length)

self.b=int(breadth)

print ("I am in Constructor of Rectangle")


def Area(self):

return (self.l*self.b);

def Perimeter(self):

return (2*(self.l+self.b));

def Display(self):#same method

print("The Length %d and Breadth %d "%(self.l,self.b));

def __del__(self):
print ("I am in Destructor of Rectangle")

class Cuboid(Rectangle):

'Class to Create Cuboid'

def __init__(self,length,breadth,height):

Rectangle.__init__(self,length,breadth);

self.h=int(height)

print ("I am in Constructor of Cuboid")

def Volume(self):

return (self.l*self.b*self.h);

def Display(self):# same method

print("The L= %d B= %d c= %d"%(self.l,self.b,self.h));

def __del__(self):
print ("I am in Destructor of Cuboid")

print("Main Starts ")

rect1 = Rectangle(10,20);

rect1.Display();

print ("The area is ",rect1.Area());

print ("The Perimeter is ",rect1.Perimeter());

cub1= Cuboid(11,22,33);

cub1.Display();

print ("The area is ",cub1.Area());

print ("The Perimeter is ",cub1.Perimeter());

print ("The volume is ",cub1.Volume());

cub1.Display();
print("Main Ends")

----using two objects adding and giving in third one(polymorphism)----------

#! /usr/bin/python

class Rectangle:

'Class to Create Rectangle'

def __init__(self):

self.l=0

self.b=0

print ("I am in Constructor of Rectangle")

def __init__(self,length,breadth):

self.l=int(length)

self.b=int(breadth)

print ("I am in Constructor of Rectangle")

def Area(self):

return (self.l*self.b);

def Perimeter(self):

return (2*(self.l+self.b));

def add(self,param2):

temp=Rectangle(0,0);

temp.l=self.l+param2.l;

temp.b=self.b+param2.b;

return temp;

def Display(self):

print("The Length %d and Breadth %d "%(self.l,self.b));

print("Main Starts ")

rect1 = Rectangle(10,20);

rect2 = Rectangle(30,40);

rect3 = Rectangle(0,0);

rect1.Display();
print ("The area is ",rect1.Area());

rect3 = rect1.add(rect2);

rect3.Display();

print("Main Ends")

-----operator overloading------

above code change def add to def __add__


and rect3 = rect1.add(rect2) to rect3 = rect1+rect2
#! /usr/bin/python

class Rectangle:

'Class to Create Rectangle'

def __init__(self):

self.l=0

self.b=0

print ("I am in Constructor of Rectangle")

def __init__(self,length,breadth):

self.l=int(length)

self.b=int(breadth)

print ("I am in Constructor of Rectangle")

def Area(self):

return (self.l*self.b);

def Perimeter(self):

return (2*(self.l+self.b));

def __add__(self,param2):

temp=Rectangle(0,0);

temp.l=self.l+param2.l;

temp.b=self.b+param2.b;

return temp;

def Display(self):

print("The Length %d and Breadth %d "%(self.l,self.b));

print("Main Starts ")


rect1 = Rectangle(10,20);
rect2 = Rectangle(30,40);

rect3 = Rectangle(0,0);

rect1.Display();

print ("The area is ",rect1.Area());

#rect3 = rect1.add(rect2);

rect3 = rect1 + rect2;

rect3.Display();

print("Main Ends")

---------------------------------Regular Expression-----------------
it is pattern marking step
'.' indicates single char any
'*' indicate any number of repetition
'.*' indicate entire line

#! /usr/bin/python

import re

line = 'L AND D of Capgemini is in M6, Airoli,Mumbai,Maharashtra'

matchobj = re.match('(.*)',line);

print (matchobj);

print (matchobj.group());

----different groups----
grp1 starts and end on last ',' if we dont use '?' else go to fisrt ','
remaning part is group2

#! /usr/bin/python

import re

line = 'L AND D of Capgemini is in M6, Airoli,Mumbai,Maharashtra'

matchobj = re.match('(.*?),(.*)',line);

#print (matchobj);

print (matchobj.group());

print (matchobj.group(1));

print (matchobj.group(2));
----search-----
#! /usr/bin/python

import re
line = 'L AND D of Cpagemin is in M6, Airoli,Mumbai,Maharashtra'

matchobj = re.search('AND(.*?),(.*)',line);#start search from AND

#matchobj = re.match('AND(.*?),(.*)',line);does not work here

#print (matchobj);

print (matchobj.group());

print (matchobj.group(1));

print (matchobj.group(2));

----------------------------------Substitute----------------------
subsitute some value to other value

#! /usr/bin/python

import re

line = '99-6703-4563 #My Email Id amit.sali@capgemini.com'

result = re.sub("#.*$","",line); from # till end remove everything

print (result);

#! /usr/bin/python

import re

line = '99-6703-4563 #My Email Id amit.sali@capgemini.com'

#result = re.sub("#.*$","",line);

result = re.sub("\D","",line);

print (result);

^ : Starts with

$ : end with

[0-9] : one digit

[^0-9] : Anything other than digit

x+ : 1 or more

x? : 0 or 1

x* : o or more

[Cap|cap|CAP] :

m{3} : Exactly 3 times "m" shold be there mmmm mmmmmmmm

m{3,} : 3 or more times


m{3,7} : min 3 max 7 times

\D : Non Digit Char

\d : digit char

S-ar putea să vă placă și