class three_d:
def __init__(self,a,b,c): #3D coordinates
self.x=a
self.y=b
self.z=c
#Display x,y,z coordinates - three_d inserter.
def __repr__(self):
return str(self.x)+", "+str(self.y)+", "+str(self.z)+"\n"
#Variable declaration
a=three_d(1,2,3)
b=three_d(3,4,5)
c=three_d(5,6,7)
#Result
print a,b,c
class thrnee_d:
def __init__(self,a,b,c): #3D coordinates
self.x=a
self.y=b
self.z=c
#Display x,y,z coordinates - three_d inserter.
__repr__=repr
#Friend function
def repr():
return str(self.x)+", "+str(self.y)+", "+str(self.z)+"\n"
#Variable declaration
a=three_d(1,2,3)
b=three_d(3,4,5)
c=three_d(5,6,7)
print a,b,c
class three_d:
def __init__(self,a,b,c): #3D coordinates
self.x=a
self.y=b
self.z=c
#Display x,y,z coordinates - three_d inserter.
def __repr__(self):
return str(self.x)+", "+str(self.y)+", "+str(self.z)
#Variable declaration
a=three_d(1,2,3)
print a
#User input
print "Enter X,Y,Z values:"
a=three_d(4,5,6)
print a
print '{0:+d}'.format(123), #for ios::showpos
if(123.23>0):
i='{0:e}'.format(123.23) #for ios::scientific
i='+'+i
print i
else :
print '{0:e}'.format(123.23)
import string
print '{0:+d}'.format(123), #for ios::showpos
if(123.23>0):
i='{0:e}'.format(123.23) #for ios::scientific
i='+'+i
print i
else :
print '{0:e}'.format(123.23)
print '{:10.2f}'.format(123.23), #2 digits left of decimal
if(123.23>0):
i='{0:.2e}'.format(123.23) #for ios::scientific
i='+'+i
print i
else :
print '{0:.2e}'.format(123.23)
print '{:#>10}'.format(str(123)), #for ios::fill
if(123.23>0):
i='{0:.2e}'.format(123.23) #for ios::scientific
i='+'+i
print i
else :
print '{0:.2e}'.format(123.23)
print '{0:.0e}'.format(1000.243) #for setprecision
print '{:>20}'.format("Hello There") #to set width and right align
print '{0:+d}'.format(123), #for ios::showpos
if(123.23>0):
i='{0:e}'.format(123.23) #for ios::scientific
i='+'+i
print i
else :
print '{0:e}'.format(123.23)
#User input
s=" Hello"
#Result
print s.lstrip() #lstrip removes leading spaces
def setup(s):
return '{:$<10}'.format(str(s))
#Result
print 10,setup(10)
def prompt():
print "Enter number using hex format:"
hex=0x46
return hex
#Result
i=prompt()
print i
out=open("test",'w')
if(not(out)):
print "Cannot open file."
else:
#Write to file
out.write("10 123.23\n")
out.write("This is a short text file.")
#Close the file
out.close()
In=open("test",'r')
if(not(In)):
print "Cannot open file."
else:
#Read file
i=In.read(2)
ch=In.read(1)
f=In.read(6)
str=In.read()
print i,f,ch
print str
#Close the file
out.close()
import sys
if not(len(sys.argv)==2):
print "Usage: PR <filename>\n"
else:
#Open a file
In=open(sys.argv[1],'r')
#In case file cannot open
if(not(In)):
print "Cannot open file."
else:
#Read file
ch=In.read()
print ch
In.close()
import sys
p="hello there"
out=open("test",'w')
#In case file cannot open
if(not(out)):
print "Cannot open file."
else:
#Write to file
for i in range(len(p)):
out.write(p[i])
import sys
n=[1,2,3,4,5]
#Open a file 'test'
out=open("test",'w')
#In case file cannot open
if(not(out)):
print "Cannot open file."
else:
#Write to file
for i in range(5):
out.write(chr(n[i]))
out.close()
for i in range(5): #clear array
n[i]=0
#Open the file
In=open("test",'r')
#In case file cannot open
if(not(In)):
print "Cannot open file."
else:
#Read file
for i in range(5):
n[i]=ord(In.read(1))
#Result, shows value from file
for i in range(5):
print n[i],
In.close()
import sys
if not(len(sys.argv)==2):
print "Usage: PR <filename>\n"
else:
#Open a file
In=open(sys.argv[1],'r')
#In case file cannot open
if(not(In)):
print "Cannot open file."
else:
#Read file
while True:
ch=In.read(1)
if not ch:
break
print ch,
#Close file
In.close()
out=open("test1",'w')
out.write("Hello")
out.close()
out=open("test2",'w')
out.write("There")
out.close()
f1=open("test1",'r')
if(not(In)):
print "Cannot open file."
f2=open("test2",'r')
if(not(In)):
print "Cannot open file."
print "Comparing files..."
buf1=f1.read()
buf2=f2.read()
print buf1,buf2
if len(buf1)==len(buf2):
print "Files are of different sizes."
f1.close()
f2.close()
else:
flag=1
for i in range(len(buf1)):
if not(buf1[i]==buf2[i]):
print "Files differ."
f1.close()
f2.close()
flag=0
break
if flag==1:
print "Files are the same."
print "Enter your name:"
str="hello world"
print str
out=open("test",'r+b')
out.write("Hello")
if(not(out)):
print "Cannot open file."
else:
out.seek(2,0)
out.write('X')
out.close()
out=open("test",'r+b')
out.write("Hello")
out.close()
In=open("test",'r')
if(not(In)):
print "Cannot open file."
else:
In.seek(2,0)
ch=In.read()
print ch
class three_d:
def __init__(self,a,b,c):
self.__x=a
self.__y=b
self.__z=c
def __repr__(self):
c=("%d"%self.__x)+", "+("%d"%self.__y)+", "+("%d"%self.__z)+"\n"
return c
a=three_d(1,2,3)
b=three_d(3,4,5)
c=three_d(5,6,7)
out=open("threed",'w')
if(not(out)):
print "Cannot open file."
else:
out.write(a.__repr__())
out.write(b.__repr__())
out.write(c.__repr__())