PYTHON ASSIGNMENT
RollNo. 11720080
Name : ASHUTOSH KUMAR ANSHU
Q1 :
Program:
import os
if not os.path.isfile('./demo.txt'):
fp = open("Demo.txt" , "wt")
fp.write("Hello this is important file dont overwrite it")
fp.close()
else:
print("It already exists")
Q2:
PROGRAM:
with open("textfile.be","w+t") as fp:
fp.write("Hello i am AMAN\n")
with open("textfile.be","ab") as fp:
fp.write(bytearray(b'Hello i am HARITAS))
with open("textfile.be" , "rt") as fp:
print(fp.read())
Q3:
PROGRAM:
import tempfile
fp = tempfile.TemporaryFile()
fp.write(b"Hello There what is this")
fp.seek(0)
str1 = fp.read()
print(str1)
fp.close()
Q4:
PROGRAM:
with open("Data.jpeg" , "wb") as fp:
fp.write(b'\xff\xd8')
fp.write(b'Hello Bhai kaisa hai')
Q6:
PROGRAM:
a = str("Hello this is me and i doing ")
print(a)
b=str()
b=a.replace(" " ,"",10)
print(b)
Q7:
PROGRAM:
dic = dict(name= dict(first="AMAN" , last="SHARMA") ,
address=dict(dist="HISAR" ,state="haryana"))
for keys , dics in dic.items():
print(keys )
for key , value in dics.items():
print(key , value)
for keys in dic.keys():
print(keys)
for key in dic.get(keys).keys():
print(key , dic.get(keys).get(key))
Q8:
PROGRAM:
from collections import OrderedDict
dic1 = dict()
dic = OrderedDict(name="AMAN",dept="IT" , course="pyhton" ,
residence="hostel 10")
dic1["name"]="AMAN"
dic1["dept"]="IT"
dic1["course"]="pyhton"
dic1["residence"]="hostel 10"
print(dic1)
print(dic)
Q9:
PROGRAM:
dic = dict(name="AMAN",dept="IT" , course="pyhton" , residence="hostel 10")
lis = sorted(dic ,key=dic.__getitem__)
print(dic)
print(lis)
for i in lis:
print("{} {}".format(i,dic[i]))
Q11:
PROGRAM:
biglist = [
{'Name':'AMAN','link':'ashari.com'},
{'Name':'GAURAV','link':'gyadav.com'},
{'Name':'SAHIL','link':'skbro.com'}
]
known_links = set()
newlist = []
for d in biglist:
link = d['link']
if link in known_links: continue
newlist.append(d)
known_links.add(link)
biglist[:] = newlist
print(biglist)