Tuesday, 8 June 2021

Top 5000 python programs examples with solutions - Part 4

Program 41:

How to write  a program to print Fibonacci series of a number?

def fib(n):
   p, q = 0, 1
   while(p < n):
       yield p
       p, q = q, p + q
k=int(input('Enter the Number\n'))
for i in fib(k):
   print(i)

 


Program 42:

Find out sum and average of given numbers using python?

5,8,2

def sum_average(*args):
    sum=0
    for i in args:
        if i != '':
            sum+=i

    tot=len(args)
    avg=sum/tot
    print('Total Arg:',tot)
    print('Total Sum:',sum)
    print('Total Avg:',avg)
    
    return(avg)
    
print(int(sum_average(5,8,2)))


Program 43:

How to get the size of list and tuple in bytes using python?

import sys
lst=[1,2,3,4,5,6,7]
tpl=('one','two','three','four','five')
print('The size of list in bytes',sys.getsizeof(lst))
print('The size of tuple in bytes',sys.getsizeof(tpl))


Program 44:

Find out anagram of two strings in python?

def anagram(a,b):
    a=a.lower()
    b=b.lower()
    
    if len(a) == len(b):
        
        a=sorted(a)
        b=sorted(b)
        
        if(a == b):
            print('Both Values are anagram')
        else:
            print('Both Values are not anagram')
            
    else:
        print('Both a={} and b={} are not anagram'.format(a,b))

anagram('apple','dusty')


Program 45:

Find the occurrences of character in a string using python dictionaries?

var=input('enter the input string\n')
dic={}
for i in var:
    if i in dic:
        dic[i]+=1
    else:
        dic[i]=1
        
print('\nThe occurances of charaters',dic)


Program 46:

print the * using dictionary values in python?

for k,v in dic.items():
    print(k,':',end='')
    for i in range(0,v):
        print('*',end='')
    print()


Program 47:

Find out sum and average of student marks and findout toppers using dictionaries in python?

students={
    'vijay':{'science':77,'english':34,'maths':100},
    'ajith':{'science':71,'english':37,'maths':89},
    'surya':{'science':98,'english':34,'maths':57}
}

topper_name=''
topper_marks=0

for name,details in students.items():
    total=0
    for sub,marks in details.items():
        total+=marks
        avg=int(total/3)
        
    students[name]={'Average':avg,'Total':total}
    
    if avg > topper_marks:
        topper_name=name
        topper_marks=avg

print('Average and Total Marks:',students)
print()
print('Topper Name:', topper_name)
print('topper_marks',topper_marks)


Program 48:

Create two dictionaries find out quantity purchased using python?

price={'pen':5,'bottle':30,'pencil':10,'books':30}
sales={'pen':10,'bottle':20,'pencil':40,'books':100}

total=0
for k,v in price.items():
    total+=price[k]*sales[k]

print('Total:',total)


Program 49:

Write a program to delete every item of dictionary one by one from last pair using python?

product={'apple':20,'mango':10,'orange':15,'banana':20}

for i in range(len(product)):
    print(product.popitem())


Program 50:

How to print the dictionary of dictionary values using python?

students={
    'vijay':{'science':77,'english':34,'maths':100},
    'ajith':{'science':71,'english':37,'maths':89},
    'surya':{'science':98,'english':34,'maths':57}
}

print(students['vijay']['science']) 
print(students['ajith']['english'])

kaavannan blogspot

Labels:

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home