Documente Academic
Documente Profesional
Documente Cultură
/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"]
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[5]=99; #Appending
#list[-1]=99; #Appending
#list.append(99);
print(list);
#del list;
#print(list);
list.insert(2,"Sachin");
print(list);
#! /usr/bin/python
list = ['Sumit',1,11.2,1997,"Sumana"]
#! /usr/bin/python
list = ['Sumit',"1","11.2","1997","Sumana"]
list.reverse();
list.sort();
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=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]
temp[0]=89;
temp[1]=67;
return c;
print("Main Starts.....");
l1=[0,0];
z=add(l1)
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);
return c,d;
print("Main Starts.....");
x=int(x)
y=int(y)
z=add_sub(x,y)
print(z);
print("Main Ends.....");
def add(str,int,flot):
return
#add('new',22,44.9)
add(22,44.9,'new')
add(int=22,flot=44.9,str='new')
---------------default parameter----------------------------
default parameter should be an last parameter
#! /usr/bin/python
def add(str,int,flot=22.34):
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')
#! /usr/bin/python
def add(str="Demo",int=0,flot=22.34):
return
add("New",22,44.9)
add("New",22)
add()
#add(22,44.9,'new')
#add(int=22,flot=44.9,str='new')
#! /usr/bin/python
def add(x,*vla):
sum=x;
sum=sum+temp;
return sum
print("Main Starts ")
add(10);
add(10,20);
add(10,20,30);
#! /usr/bin/python
def add(*mnp):
sum=0;
sum=sum+temp;
#print (mnp);
return sum
add();
add(10);
add(10,20);
add(10,20,30);
----------------------------------lamada function-------------------------
it is one line function
#! /usr/bin/python
x=multi(8,3)
----------------------------------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;
x=10 #global
sample()
#! /usr/bin/python
def sample():
global x;
x += 1;
x=10 #global
sample()
----------------------------------Modules------------------------------------
c=a+b;
return c;
def sub(a,b):
c=a-b;
#! /usr/bin/python
import Maths;
z=Maths.add(x,y);
z=Maths.sub(x,y);
#! /usr/bin/python
#! /usr/bin/python
z=sub(x,y);
-----------------------------Packages----------------------------------------
-----main code---
#! /usr/bin/python
z=Arithmatic.addition.add(x,y);
z=Arithmatic.subtraction.sub(x,y);
-----__init__.py-------
from Arithmatic import addition
-----main code----
#! /usr/bin/python
from Arithmatic import addition;
z=addition.add(x,y);
z=division.div(x,y);
---------------------------------File Handling------------------------------
#! /usr/bin/python
fhandler=open("sample.txt");
while(ch):
fhandler.close();
#! /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();
#! /usr/bin/python
fhandler=open("sample.txt","r");
ch=fhandler.readline();
while(ch):
print(ch,end="");
#print(ch);
ch=fhandler.readline();
fhandler.close();
#! /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.close();
output:-
Good Morning....How are you?We are Good
#! /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("\n");
fhandler.close();
output:-
Good Morning....
We are Good
#! /usr/bin/python
import os;
os.rename("sample.txt","demo.txt");
-------------------------------Exception handling---------------------------
#! /usr/bin/python
def age_calc(a):
c=a/7.24*5;
return (c)
x=int(x)
z=age_calc(x)
----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)
try:
x=int(x)
y=int(y)
z=div(x,y)
except ZeroDivisionError:
#! /usr/bin/python
def div(a,b):
c=a/b
return (c)
try:
x=int(x)
y=int(y)
z=div(x,y)
except:
#! /usr/bin/python
def div(a,b):
c=a/b
return (c)
try:
x=int(x)
y=int(y)
z=div(x,y)
except ZeroDivisionError:
finally:
-----Raise------
#! /usr/bin/python
class CreditCardError(RuntimeError): #creating own exception
def __init__(self,arg):
self.args = arg
try:
x=int(x)
if (x==0):
print("CC Accepted")
finally:
#! /usr/bin/python
class CreditCardError(RuntimeError):
def __init__(self,arg):
self.args = arg
try:
x=int(x)
if (x==0):
print("CC Accepted")
except CreditCardError as e:
finally:
print ("I am in Finally Block ")
def div(a,b):
try:
c=a/b
except ZeroDivisionError:
return 0
return (c)
try:
x=int(x)
y=int(y)
z=div(x,y)
except ZeroDivisionError:
finally:
----nested try---------
#! /usr/bin/python
def div(a,b):
c=a/b
return (c)
try:
x=int(x)
y=int(y)
try:
z=div(x,y)
except ZeroDivisionError:
z=0;
except:
finally:
#! /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):
r1=Rectangle(10,20);
r1.Display();
print("Main Ends...");
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("Main Starts...");
r1=Rectangle(10,20);
r2=Rectangle(11,22);
r1.Display();
r2.Display();
print("Main Ends...");
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("Main Starts...");
r1=Rectangle(10,20);
r1.Display();
print("Main Ends...");
#! /usr/bin/python
class Rectangle:
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("Main Starts...");
r1=Rectangle(10,20);
r2=Rectangle(10,20);
r3=Rectangle(10,20);
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("Main Starts...");
r1=Rectangle(10,20);
r2=Rectangle(10,20);
r3=Rectangle(10,20);
print("Main Ends...");
---------Constructor overloading-----
Constructor overloading will not present in python
so we cannot use two constructor with different args
#! /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):
def __del__(self):
print("Main Starts...");
r1=Rectangle(10,20);
r2=Rectangle(10,20);
r3=Rectangle(10,20);
r4=Rectangle();
print("Main Ends...");
--------inheritance------
#inherit method from parents class
#! /usr/bin/python
# below with out inheritance
class 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)
def Area(self):
return (self.l*self.b);
def Perimeter(self):
return (2*(self.l+self.b));
def Display(self):
def __del__(self):
class Cuboid(Rectangle):
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):
rect1 = Rectangle(10,20);
rect1.Display();
cub1= Cuboid(11,22,33);
cub1.Display();
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:
def __init__(self,length,breadth):
self.l=int(length)
self.b=int(breadth)
def Area(self):
return (self.l*self.b);
def Perimeter(self):
return (2*(self.l+self.b));
def Display(self):
def __del__(self):
class Cuboid(Rectangle):
def __init__(self,length,breadth,height):
Rectangle.__init__(self,length,breadth);
self.h=int(height)
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):
rect1 = Rectangle(10,20);
rect1.Display();
cub1= Cuboid(11,22,33);
cub1.Display();
cub1.Display1();
print("Main Ends")
-----method overloading----
#! /usr/bin/python
class Rectangle:
def __init__(self,length,breadth):
self.l=int(length)
self.b=int(breadth)
return (self.l*self.b);
def Perimeter(self):
return (2*(self.l+self.b));
def __del__(self):
print ("I am in Destructor of Rectangle")
class Cuboid(Rectangle):
def __init__(self,length,breadth,height):
Rectangle.__init__(self,length,breadth);
self.h=int(height)
def Volume(self):
return (self.l*self.b*self.h);
print("The L= %d B= %d c= %d"%(self.l,self.b,self.h));
def __del__(self):
print ("I am in Destructor of Cuboid")
rect1 = Rectangle(10,20);
rect1.Display();
cub1= Cuboid(11,22,33);
cub1.Display();
cub1.Display();
print("Main Ends")
#! /usr/bin/python
class Rectangle:
def __init__(self):
self.l=0
self.b=0
def __init__(self,length,breadth):
self.l=int(length)
self.b=int(breadth)
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):
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------
class Rectangle:
def __init__(self):
self.l=0
self.b=0
def __init__(self,length,breadth):
self.l=int(length)
self.b=int(breadth)
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):
rect3 = Rectangle(0,0);
rect1.Display();
#rect3 = rect1.add(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
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
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'
#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
print (result);
#! /usr/bin/python
import re
#result = re.sub("#.*$","",line);
result = re.sub("\D","",line);
print (result);
^ : Starts with
$ : end with
x+ : 1 or more
x? : 0 or 1
x* : o or more
[Cap|cap|CAP] :
\d : digit char