





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






name=input("Enter : ") length=len(name) name2=(name[ 0 :length+ 1 ])[::- 1 ] Status= True if name==name2 else False l=[] if Status==True: l.append(name) print(l) t1=(l) print(t1) else: name1 = ((((name.replace("a", "1")).replace("e", "2")).replace("i", "3")).replace("o", "4")).replace("u", "5") print(name1) t=(name1) print(t)
numberDetails={} with open("log.txt","r") as f: num=f.readline() while num!="": number=int(num) if number% 2 == 0 :
numberDetails.update({number:"Even"}) else: numberDetails.update({number:"odd"}) num=f.readline() print(f"Hello its My dictionary {numberDetails}") sorted_numberDetails=dict(sorted(numberDetails.items())) print(sorted_numberDetails.items()) with open("log1.txt","w") as f: for key,value in sorted_numberDetails.items(): f.write(f"{key} : {value}\n")
names=[] ages=[]
for item in odd: file.write(str(item)) file.write("\n") for item in even: file.write(str(item)) file.write("\n") def evensum(even): if not(even): return 0 return even.pop()+evensum(even) sum=evensum(even.copy()) print(f"Sum is {sum} ")
def listTOdictionary(wordlist): dict={} for word in wordlist : wordlength=len(word) dict[word]=wordlength return dict Sentence=input(" Enter Sentence \n") wordlist=Sentence.split(" ") mydict=listTOdictionary(wordlist) with open("data.txt","w") as fileofdata: for key in mydict: fileofdata.write(f"{key} {mydict[key]}\n") newdict={} with open("data.txt","r") as fileofdata: read=fileofdata.readline() while read!="": wordwithlen=read.split(" ") value=int(wordwithlen[ 1 ]) newdict[wordwithlen[ 0 ]]=value read=fileofdata.readline() print(newdict) sortdictionary=dict(sorted(newdict.items()))
print(sortdictionary) for keys in sortdictionary: print(F"{keys} : {sortdictionary[keys]}")
from random import randint class Person: def init(self,name,age,address): self.name=name self.age=age self.address=address def showdetails(self): print(f"name :{self.name}\n age : {self.age} \n address : {self.address}") class Empolyee(Person): def init(self, name, age, address,salary): super().init(name, age, address) self.salary=salary def showdetails(self): super().showdetails(self) print(f"salary :{self.salary}") def str(self): return f"name :{self.name}\n age : {self.age} \n address : {self.address} \n salary {self.salary}\n " listofEmpolyees=[] listofEmpolyees.append(Empolyee("Umar", 22 ," Ali town lahore",randint( 0 , 100000 ))) listofEmpolyees.append(Empolyee("Nimr", 21 ,"Town ship lahore",randint( 0 , 100000 ))) listofEmpolyees.append(Empolyee("Hifza", 19 ,"model town lahore",randint( 0 , 100000 ))) for empolyee in listofEmpolyees: if empolyee.salary> 50000 : print(empolyee) with open("data.txt","+a") as file: file.write(f"name :{empolyee.name}\n age : {empolyee.age} \n address : {empolyee.address} \n salary {empolyee.salary}")
characters=input("Enter : ") uniqueCharacter=set() uniqueCharacter=set(characters) uniqueCharacterlist=[] uniqueCharacterlist=list(uniqueCharacter) uniqueCharacterlist.sort() characterFruequency={} for element in uniqueCharacterlist: frequency=characters.count(element) characterFruequency[element]=frequency print(characterFruequency) with open("data.txt","w") as file: for key,value in characterFruequency.items(): file.write(f"{key} {value}\n") print(uniqueCharacterlist[::- 1 ])
from random import randint def student_Scores(results): add=0. length=len(results) for key in results: add+=results[key] average=add/length top_achievers=[] for student in results: if results[student] > average : top_achievers.append(student) return top_achievers studentdata={} for i in range( 0 , 4 ): name=input("Enter : ") studentdata[name]=randint( 0 , 100 ) toppers=tuple(student_Scores(studentdata)) print(studentdata)
with open("data.txt","w") as studentrecord: for item in toppers: studentrecord.write(f"{item}\n") names=[] with open("data.txt") as studentrecord: names=studentrecord.readlines() alphabeticsortednames=[] for item in names: alphabeticsortednames.append(item.strip("\n")) alphabeticsortednames.sort() print(alphabeticsortednames)
Sentences=[] for i in range( 0 , 3 ): Sentences.append(input(" ENTER : ")) words=[[],[],[]] words[ 0 ]=Sentences[ 0 ].split(" ") words[ 1 ]=Sentences[ 1 ].split(" ") words[ 2 ]=Sentences[ 2 ].split(" ") longestlength= 0 print(words) for wordlist in words: for word in wordlist: if longestlength<len(word): longestlength=len(word) longestword=word maximumFrequency= 0 maximumFrequencyword=None for wordlist in words: for word in wordlist: frequency= 0 for i in range( 0 ,len(words)): for oneword in words[i]: if word==oneword: frequency+= 1 if frequency>maximumFrequency: