Posts

Showing posts from 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()

Python program to take two lists and display common members in both list.

#Python program to take two lists and display common members in both list. array=[] arr=[] N=int(input("Enter number of elements in list1:")) for i in range(N):     list1=int(input("Enter element %d:"% (i+1)))     array.append(list1) n=int(input("\nEnter number of elements in list2:")) for i in range(n):     list2=int(input("Enter element %d:"% (i+1)))     arr.append(list2) print("\n") if(set(array).intersection(arr)):     print("common members are: ",set(array).intersection(arr)) else:     print("No common members in list 1 and list 2.")

Python program to print numbers less than 5 from the list.

#Python program to print numbers less than 5 from the list. array=[] arr=[] N=int(input("Enter number of elements in list:")) for i in range(N):     list=int(input("Enter element %d:"% (i+1)))     array.append(list) for i in range(N):     if(array[i]<=5):         arr.append(array[i]) print("Entered list is:\n", array) print("\nList with only numbers less than 5 is:\n",arr)

Python program to check if the entered string is pangram or not using function.

#Python program to check if the entered string is pangram or not using function. from string import ascii_lowercase  sentence=input("Enter a string to check if its pangram:").lower() def pangram(sentence):     if set(ascii_lowercase).issubset(sentence):         print("Entered string is pangram.")     else:         print("Entered string is not pangram.") pangram(sentence)

Python program that compute length of given string or list.

#Python program that compute length of given string or list. string=input("Enter string or list to compute length.") print("Length of entered string or list is:",len(string))

Python Program using function that takes character and returns true if its vowel.

#Python Program using function that takes character and returns true if its vowel. def vowels():     vow=['a' , 'e' , 'i' , 'o' , 'u']        if N in vow:         print(True)     else:         print(False) N=input("\nEnter a character:").lower() vowels()

Python program to find factorial of number using recursive function.

#Python program to find factorial of number using recursive function. def fact(n):     if(n==1):         return n     else:             return n*fact(n-1) N=input("Enter number to find its factorial:") N=int(N) if(N==0):     print("Factorial of %d is 1."% N) else:     print("Factorial of %d is:"% N,fact(N))

Python program to check whether entered number is palindrom and armstrong by using function.

#Python program to check whether entered number is palindrom and armstrong by using function. def armstrong():     N=input("Enter number to check if its armstrong number or not:")     lenght=len(str(N))     N=int(N)     sum=0     temp=N     while (temp>0):         R=temp%10         sum=sum+R**lenght         temp=temp//10     if(N==sum):         print("\n%d"% N,"is an armstrong number.")     else:         print("%d"% N,"is not an armstrong number.")     return def palindrome():     N=int(input("\nEnter a number to check if its palindrome or not:"))         temp=N     rev=0     while(temp>0):  ...

Python program to reverse user defined value using function.

#Fucntion that reverse userdefined value. N = int(input("Enter Number to reverse: ")) def REVERSE():     global N     Rev = 0     O = N     while N != 0 :         Rev = Rev * 10         Rev = Rev + (N%10)         N = N // 10     print("Reverse of %d"% O,"is: %d" % Rev)     return REVERSE()

Python program to generate the Febonacci series.

#Program to generate the Febonacci series. N = input("Enter Number of terms you want to print: ") N = int(N) F1 = 0 F2 = 1 i = 3 print(F1) print(F2) for i in range(N-2) :     F3 = F1+F2     print(F3)     F1=F2     F2=F3

Python program to check whether the given number is even or odd.

#Program that will check whether entered number is even or odd. N = input("Enter Number to check: ") N = int(N) if N%2 == 0:     print("%d is an even number."% N) else:     print("%d is an odd number."% N)

Python program to calculate age and year when user will be 100 years old

#Program that will ask user his name and his date of birth and print his age and year when he will turn 100 years old. #to enter your date use format like 8-8-1996 import datetime name = input("Enter your name: ") today = datetime.date.today() birthIn = input("Enter your Birhtday in format Date-Month-Year: ") birthday = datetime.datetime.strptime(birthIn, "%d-%m-%Y") print("Todays date is:", today.strftime("%d-%m-%Y")) print("Your Birthday is on: ", birthIn) age = today.year-birthday.year print(name +" your age is: ",age) old = (100-age)+today.year print("In %d you will be 100 years old" % old)