Posts

Showing posts from September, 2017

Python Program to demonstrate file operations like read, append, write.

#Python Program to demonstrate file operations like read, append, write. readWRITE = 'w+' READ = 'r' Append = 'a' list0 = []  def create():     file = input("Enter name for a file: ")     filename = (file+".txt")     f = open(filename, readWRITE)     N = int(input("Enter number of lines you want to print:"))     for i in range (N):         data = input("%d:"% i)         list0.append(data)             for i in range(N):         f.write(list0[i]+"\n")     f.close()     print("File Written successfully.") def read():     filename = input("\nEnter name of a file with extension that you want to open:")     print("\ncontent of the file is:\n")     f = open(filename, READ)   ...

Python Program to create and concatenate n number of dictionaries.

#Python Program to create and concatenate n number of dictionaries. dic = {} dict1 = {} N = int(input("Enter number of dictionaries you want to create:")) for i in range (N):     dic["dic%d" % i] = {}     a= int(input("\nEnter number of items in dictionary %d:"% i))     list0 = []     list1 = []         for j in range(a):         key=input("Enter key for item %d:"% j)         list0.append(key)     for k in range(a):         value=input("Enter value for key "+list0[k]+":")         list1.append(value)     for l in range(0,a):         dic['dic%d'% i][list0[l]] = list1[l]         if l == a:             del list0...

Python Program to sum all the items(values) in a dictionary.

#Python Program to sum all the items(values) in a dictionary. dict1={} list0=[] list1=[] def createDict():     for i in range(N):         key=input("Enter key for item %d:"% i)         list0.append(key)     print("\nNote: Please enter values for key in degits to get sum.")     for i in range(N):         value=int(input("Enter value for key "+list0[i]+":"))         list1.append(value)     for i in range(N):         dict1[list0[i]]=list1[i]     print("\nEntered dictionary is:")     print(dict1) def sumval():     print("\nSum of values entered in dictionary is:")     print(sum(dict1.values())) N = int(input("\nEnter number of items to enter in dictionary:")) createDict() sumval()

Python Program to sort (by Ascending or Descending) a dictionary by key or by value.

#Python Program to sort (by Ascending or Descending) a dictionary by key or by value. dict1={} list0=[] list1=[] def createDict():     for i in range(N):         key=input("Enter key for item %d:"% i)         list0.append(key)     print("\n")     for i in range(N):         value=input("Enter value for key "+list0[i]+":")         list1.append(value)     for i in range(N):         dict1[list0[i]]=list1[i]     print("\nEntered dictionary is:")     print(dict1) def sortByKey():     A = int(input("Enter 1 to sort in Ascending order or enter 0 to sort in descending order:"))     if A == 1:         print("\nDictionary sorted by key in Ascending order is:")   ...

Python program to clone or copy list.

#Python program to clone or copy list. list1=[] list2=[] N=int(input("Enter number of elements in list1:")) for i in range (N):     array=int(input("Enter an element for position %d:"%i))     list1.append(array) print("Original list is:") print(list1) list2=list1 print("\nCloned list is:") print(list2)

Python program to remove element of specific position from list.

#Python program to remove element of specific position from list. list1=[] N=int(input("Enter Number of elements in list:")) for i in range(N):     array=int(input("Enter element for position %d:"% i))     list1.append(array) def delete():     e=int(input("Press 1 to continue or 0 to exit:"))     if e==1:         p=int(input("Enter position of element to delete:"))         del list1[p]         print("New linked list is:")         display()     else:         exit def display():     print(list1)     delete() delete()