Posts

Showing posts from August, 2020

Python Program to input any sentence and create a dictionary with consonant and vowels.

name= input ( 'Enter Any Sent.?' ) v_cnt=c_cnt=oth_cnt= 0  vowels= 'aeiou' for i in name :        if i.isalpha ():         if i in vowels :              v_cnt+= 1         else :             c_cnt+= 1    else :          oth_cnt+= 1 print ( v_cnt , c_cnt )  d1= { 'consonant' : c_cnt , 'vowels' : v_cnt , 'Other' : oth_cnt } print ( d1 )

Python program to combine two dictionary adding values for common keys

  d1 = {'a': 100, 'b': 200, 'c':300} d2 = {'a': 300, 'b': 200, 'd':400}  d=dict() for i in d2:     if i in d2:         d[i]=d1[i]+d2[i]     else:         d.update(d1)         d.update(d2) print(d)

Python script to generate and print a dictionary that contains a number (between 1 and n) in the form (number, square of number)

n=int(input('Enter Any number ?'))  d=dict() for i in range(1,n+1):     print(d.update({i:i*i}))

Python script to concatenate following dictionaries to create a new one.

dic1={1:10, 2:20}  dic2={3:30, 4:40} dic3={5:50,6:60} dic1.update(dic2) dic1.update(dic3) d4=dic1 print(d4)

Python Program that reads a string and display it in reverse order.

Str=input('enter any string') l=len(str) a=str[::-1] print(' original string ',Str) print(' reversed string ',a) 

Python Program to input any string and substring and search for substring and print the occurrence of substring in main string if it is there

  # To understand find or search and index function str = input ('Enter any sentence: ') str=str.lower() print (str) ch = input ('Enter character to be found: ') occ= int (input('Enter occurance of character to be found: ') ) c=str.count(ch) if (occ>c):     print ('Not Found') else:     for j in range (len(str)):         i=str[j]         if i==ch:             c-=1         if c==0:             print (occ,'occerance of ',ch,' is at position:',j)             break

Python Program to input any string and differenciat vowels and consonants

  sen=input('Enter a sentence.. ') a=' ' b=' ' for i in sen:   if i in 'aeiou':     a=a+i   elif i==' ':     continue   else:     b=b+i print('vowels=',a,'consonants=',b)

Python Program to input any string and print words starts with consonant and vowels inthe different list

str = input ('Enter any sentence') str+=' ' ns='' vo_word=[] conso_word=[] for ch in str:     if (ch==' '):         c1=ns[0]         if (c1 in 'AEIOUaeiou'):             vo_word.append (ns)         else:             conso_word.append (ns)         ns=''     elif (ch=='.'):         pass     else:         ns+=ch print ('Original string :', str) print ('Vowel words:', vo_word) print ('Consonant words: ',conso_word)

Python Program to input any number(N) and print sum of 1 to N.

n=int(input('Enter Any Number ?')) sum=0 for i in range(1,n+1):     sum=sum+i print(sum)

Python Program to Remove all duplicates from a given string

str = input ("Enter string") len1= len(str) x='' for i in range (len1):     ch = str[i]     if (str.count(ch)==1):         x+=ch print('String after removal of duplicates: ',x)

Python program to print even length words in a string

  str = input ('Enter String') str +=' ' length = len(str) x='' for i in range (length):     ch = str[i]     if (ch!=' '):         x=x+ch     else :         len1 = len(x)         if (len1%2==0):             print (x)         x=''

Python Program to understand length function in python

  str = ' Python Program' x='' c=0 while (x!=str):     c = str[c]      x += ch     c+=1 print (c)

Python Program to Check if a Substring is Present in a Given String

  str = input ('Enter String') word = input ('Enter String to be searched') str +=' ' len = len(str) x='' for i in range (len):     ch = str[i]     if (ch!=' '):         x=x+ch     else :         if (x==word):             print ('Substring is found')             break         x=''

Python program to remove n’th character from string.

  str = input ('Enter string: ') n = int(input ('Enter index to remove character from string: ')) len = len(str) if (n<len):     x=''     for i in range (len):         ch = str[i]         if (n!=i):             x+= ch     print ('New string: ',x)

Python program that accepts a sentence and calculate the number of different type of characters .

  str = input ('Enter string') len = len (str) print ('Length of str = ',len) cu,cc,cd,cs,csd=0,0,0,0,0 for j in range (len):     i=str[j]     if (ord(i)>=65 and ord (i)<=90):         cu+=1     elif (ord(i)>=97 and ord (i)<=122):         cl+=1     elif (i==' '):         cs+=1     elif (ord(i)>=48 and ord (i)<=57):         cd+=1     else :         csd+=1 print ('Upper Case Letters = ', cu) print ('Lower Case Letters = ', cl) print ('Letters = ', cl+cu) print ('Digits = ', cd) print ('Space = ', cs) print ('Special Digits = ', csd) print ('Total length = ', cv+cc+cd+cs+csd)

Python Program that computes the net amount of a bank account based a transaction log from console input.

  #D or d means deposit while W  or w means  withdrawal. bal=0 while('true'):     ch = input ('''Enter d or D for Deposit Enter w or W to Withdraw Enter x to exit ''')     if (ch=='d' or ch == 'D'):         amt = int (input ('Enter amount: '))         bal += amt     elif (ch=='w' or ch == 'W'):         amt = int (input ('Enter amount: '))         bal-= amt     else:         print (bal)         break

Python Program which will find all such numbers between 1000 and 3000 (both included) such that each digit of the number is an even number.The numbers obtained should be printed in a comma-separated sequence on a single line

  x='' for i in range (1000,3001):     n=i     flag =1     while (n>0):         rem = n%10         if (rem%2!=0):             flag = 0             break         n=n//10     if (flag == 1):         x+= str(i)+' , ' print (x)

Python Program to input any string and make it the encrypt code by 2 charactern

s= input ('Enter a string') x='' len = len(string) for i in range (len):     ch = string[i]     if (ch=='y'):         x=x+'a'     elif (ch=='z'):         x=x+'b'     elif (ch=='Y'):         x=x+'A'     elif (ch=='Z'):         x=x+'B'     else :         a = ord(ch) + 2         x=x+ chr(a) print ('String after encrption: ', x)

Python Program to perform all arithmetic operations in menu

print(''' main menu 1.add 2.sub 3.div 4.milt 5.exit''') n1=int(input('enter any n1')) n2=int(input('enter any n2')) while True:   ch=int (input('enter your choice'))   if ch==1:     print(n1+n2)   elif ch==2:     print(n1-n2)   elif ch==3:     print(n1*n2)   elif ch==4:     print(n1/n2)   elif ch==5:     print('you have exited!')     break   else:     print('invalid choses')

Python Program to input your percentage and print grade

' ''  Percentage(%)           Grade      91-100                   A      81-90                    B      71-80                    C      0-70                     D   ''' per=int(input('enter perentage')) if per>=91 and per<=100:   print('Grade A') elif per>=81 and per<=90:   print('Grade B') elif per>=71 and per<=80:   print('Grade c') elif per>=0 and per<=70:   print('Grade D') else:   print('invalid marks')

Python Program to input percentage and print result

cri=int(input('enter criteria(percentage) for pass and fail')) per=int(input('enter perentage')) if per>=cri:   print('pass') else:   print('fail')

Python Program to input any number and check whether it is perfect number or not

n = int(input ('Enter a no.')) sum = 0 n1=n for i in range (1,n):     if (n%i==0):         sum = sum + i if (sum ==n1):     print (n1,' is a perfect no.') else :     print (n1,' is not a perfect no.')

Python Program to print sum of series # 1/1! + 2/2! + 3/3! + ....... + n/n!

# sum = 1/1! + 2/2! + 3/3! ... n/n! n = int (input ('Enter value for n: ')) sum =0 for i in range (1,n+1):     f=1     for j in range(1,i+1):         f=f*j     sum+= (i/f) print ('Sum of series= ', sum)

Python Program to input N number and print sum of N geometric terms

  # sum = a + ar + ar**2 + ar**3 + ..... + ar**n n= int (input ('Enter no of terms')) a= int (input ('Enter value for a ')) r= int (input ('Enter value for r')) sum=a for i in range (1,n+1):     sum = sum + (a*r**i)     print (a*r**i, sum) print ('Sum of series = ',sum)

Python Program to print N number of geometric terms

a =int(input(" Enter First Number of an G.P Series: : ")) n = int(input(" Enter the Total Numbers in this G.P Series: : ")) r = int(input("Please Enter the Common Ratio : ")) total = 0 value = a print("\nG.P Series :", end = " ") for i in range(n):     print("%d " %value, end = " ")     total = total + value     value = value * r print("\nThe Sum of Geometric Progression Series = " , total)

Python Program to input any number and check whether it is automatic or not

n=0 cnt=0 n=int(input('Enter any no...')) sq=n**2 print(sq) while(n>0):   cnt=cnt+1   n=n//10 no=sq%(10**cnt) print(no) if(n==no):   print('The no. is automorphic') else:   print('The no. is not automorphic') 

Python Program to input 3 digit number and print the sum of all digits

n=int(input('3 digit no.')) sum=0 for i in range(3):   a=n%10   sum+=a   n=n//10 print(sum) 

Python Program to input any number and check whether it is positive, negative or zero

n=int(input('enter any number')) if (n>0):   print(n,'is positive') elif (n<0):   print(n,'is negative') else:   print(n,'zero') 

Python Program to input any number and check whether it is odd or even

n=int(input('enter any number')) if n<0:     print(n,':number is negative') else:     if n%2==0:         print(n,':number is even')     else:         print(n,':number is odd')

Python Program to calculate compound interest

p=int(input('enter principle amount')) r=int(input('enyer rate of intrest')) t=int(input('time of intrest')) ci=p*(1+r/100)**t print('compound intrest',ci)

Python Program to calculate simple interest

p=int(input('enter principle amount')) r=int(input('enyer rate of intrest')) t=int(input('time of intrest')) si=p*r*t/100 print('simple intrest',si)

Python Program to print circumference of circle

r=int(input('enter radius of circle')) pie=22/7 circum=pie*r*2 print('circumference of circle=',circum)

Python Program to input radius of circle and print area of circle

r=int(input('enter radius of circle')) pie=22/7 area=pie*r**2 print('area of circle=',area)

Python Program to input two dates and to print gap between them # calculate your age

dd = int(input ('Enter Date: ')) mm = int(input ('Enter Month: ')) yy = int(input ('Enter Year: ')) td = int(input ('Enter Today\'s Date: ')) tm = int(input ('Enter Today\'s Month: ')) ty = int(input ('Enter Today\'s Year: ')) ay=ad=am=0 if (mm<tm):     ay=ty-yy     if (mm==(tm-1)):         if (dd>tm):             am=0     else:         if (dd>tm):             am = tm-mm-1         else:             am = tm-mm         elif (mm>tm):     ay=ty-yy-1     if (dd<td):         am = 12-mm+tm     else:         am=11-mm+tm         else :         if (dd<td):         ay=ty-yy         ad = td - dd         am=0     elif (dd>td):         ay=ty-yy-1         am=11         if ( mm==2 or mm== 4 or mm==6 or mm==8 or mm==9 or mm==11 or mm==1):             ad = 31-dd+td         elif (mm== 5 or mm==7 or mm==10 or mm==12):             ad = 30-dd+td         else:            if (ty%4==0):               ad = 29-dd+td            else:

Python Program to input your name, class and age and print them

name=input('enter your name') class=int(input('enter your class') age=int(input('enter your age') print(name,"is studying in",class,"and",age,"years old")