Posts

WeCare insurance company wants to calculate premium of vehicles. Vehicles are of two types – “Two-Wheeler” and “Four-Wheeler”. Each vehicle is identified by vehicle id, type, cost and premium amount. Premium amount is 2% of the vehicle cost for two wheelers and 6% of the vehicle cost for four wheelers. Calculate the premium amount and display the vehicle details. Write a Python program to implement the class chosen with its attributes and methods.

class vehicle:     def _init_(self):         self.__vehicleid=None         self.__vehicletype=None         self.__vehiclecost=None         self.__vehiclepremium=None     def setvehicleid(self,vid):         self.__vehicleid=vid     def setvehicletype(self,typ):         self.__vehicletype=type     def setvehiclecost(self,cost):         self.__vehiclecost=cost     def setvehiclepremium(self):         if(self.__vehicletype=="two wheeler"):             self.__vehiclepremium=self.__vehiclecost*2/100             print("premium for two wheeler",self.__vehiclepremium)         elif(self.__vehicletype=="four wheeler"):             self.__vehiclepremium=self.__vehiclecost*6/100  ...

Write a program to print each line of a file in reverse order

 f=input("enter file name") file=open(f,'r').read() l=file.split("\n") for item in l:     print(item[::-1])

To install the package pandas write a python program to calculate the mean and standard deviation for list of numbers stored in excel file named data.xlsx.(Use JupyterNotebook or Spyder tool in Anaconda Navigator)

 import pandas af=pandas.read_excel("d:\\data.xlsx") print(af) print(af.mean()) print(af.std())

Write a python program to create a user-defined exception named “ShortInputException” that raises when the input text length is less than 3.

 class ShortInputException(Exception):     def __init__(self,length):         Exception(self)         self.len=length string=input("Enter The String") length=len(string) try:     if length<3:         raise ShortInputException(length) except ShortInputException as s:     print("The Length of String Is 3 Or Greater Than 3.\nBut Your Entered String Length Is",s.len) else:     print("No Exception")     

Write a python program to handle multiple errors with one except statement

 a=int(input("Enter The First Number:")) b=int(input("Enter The Second Number:")) list1=[1,2,3,4,5] try:     c=a/b     print("The Division Result is",c)     print(list1[10]) except (ZeroDivisionError,IndexError):     print("Please see the try block their is exception\nIt may be Zero Division or Invalid Index") else:     print("No Exception") finally:     print("GoodBye!")

Write a Python Program to count the number of characters in the string and store them in a dictionary.

 str1=input("Enter the string") dict1={str1:len(str1)} print("The resultant dictionary is",dict1)

Write a python program to find mean, median, mode for the given set of numbers in a list.

 from statistics import mean,median,mode l=[] elesize=int(input("Enter the size of the list")) print("Enter the list elements upto",elesize) for i in range(elesize):     ele=int(input())     l.append(ele) print("The list is",l) print("The mean of given list is",mean(l)) print("The median of given list is",median(l)) print("The mode of given list is",mode(l))

Write a function ball_collide that takes two balls as parameters and computes if they are colliding. Your function should return a Boolean representing whether or not the balls are colliding. Hint: Represent a ball on a plane as a tuple of (x, y, r), r being the radius, if (distance between two balls centers) <= (sum of their radii) then (they are colliding)

 def ball_collide(x1,y1,r1,x2,y2,r2):     dist=((x2-x1)**2+(y2-y1)**2)**0.5     center=dist/2     print("Collision Point",center)     r=r1+r2     print("Sum of radius",r)     if(center<=r):         print("Balls are Colliding")         return True     else:         print("Balls are not Colliding")         return False       c=ball_collide(4,4,3,2,2,3) print(c) c=ball_collide(100,200,20,200,100,10) print(c)

Write a python program to calculate the length of a string using recursion and check whether the given number is palindrome or not.

def length(str1):     if str1=="":         return 0     else:         return length(str[1:-1]) str1=input("Enter the  Data:") print("The Length of the Data:",len(str1)) if str1==str1[::-1]:     print("The Given Data:",str1, "is Palindrome") else:     print("The Given Data:",str1, "is not Palindrome")           

Write a python program to accept a string from a user and re-display the same after removing vowels from it.

def remove_vowel():     l=['a','e','i','o','u']     while True:         str1=input("Enter The String, Enter x to Terminate")         if (str1=='x'):             break         else:             for i in str1:                 if i in l:                     str1=str1.replace(i,'')         print("After remove vowels in string:",str1) remove_vowel()

Write a python program to compute GCD, LCM of two numbers (Each function shouldn’t exceed one line use predefined module).

 from math import gcd def gcd1(a,b):     return gcd(a,b) def lcm1(a,b):     return a*b//gcd(a,b) a=int(input("Enter the value of a")) b=int(input("Enter the value of b")) print("The gcd of two numbers is",gcd1(a,b)) print("The lcm of two numbers is",lcm1(a,b))

Create and access a user defined package ArithmeticPackage where the package contains a module named ArithmeticDemo, which inturn contains a method called sumtwo() , subtwo(), multwo() and divtwo() which takes two numbers as parameter and returns the result.

 def sumtwo(a,b):     return a+b def subtwo(a,b):     return a-b def multwo(a,b):     return a*b def divtwo(a,b):     return a//b from ArithmeticPackage import ArithmeticDemo a=int(input("Enter the a value:")) b=int(input("Enter the b value:")) print("The addition of two numbers is",ArithmeticDemo.sumtwo(a,b)) print("The sub of two numbers is",ArithmeticDemo.subtwo(a,b)) print("The multiplication of two numbers is",ArithmeticDemo.multwo(a,b)) print("The division of two numbers is",ArithmeticDemo.divtwo(a,b))

Write a python program that uses function to find the sum of the even-valued terms in the Fibonacci sequence whose values do not exceed ten thousand.

 def evenfib(n):     evensum,prev,next1=0,0,1     fib=[0]     while(next1<=n):         fib.append(next1)         if next1 % 2==0:             evensum = evensum+next1         prev,next1=next1,prev+next1     print("The sum of even valued fib sequence",fib,"is",evensum) n=10000 evenfib(n)

Write a python program to compute cumulative product of a list of numbers (write function cumulative_product).

 def cummulative_product(n):     l=[]     product=1     print("Read The List Of Elements Upto:",n)     for i in range(n):         ele=int(input())         l.append(ele)     for i in l:         product=product*i     return product n=int(input("Enter Size Of List:")) print("The Cummulative Product Of The List is:",cummulative_product(n))               

Write a python program to find sum of two numbers using command line arguments.

 import sys num1=float(sys.argv[1]) num2=float(sys.argv[2]) print("The num1 Value is",num1) print("The num2 Value is",num2) print("The sum of two numbers is",num1+num2)

Write a python program to print out the decimal equivalents of 1/2, 1/3, 1/4, . . . ,1/10 using for loop.

3)a) Write a python program to print out the decimal equivalents of 1/2, 1/3, 1/4, . . . ,1/10 using for loop.     a=int(input("Enter the last value of the series:"))   for   key in range(1,a+1):     print(1/key)

Write a python program to test whether a given number is even or odd using if-else statement.

2)a)Write a python program to test whether a given number is even or odd using if-else statement. a=int(input("Enter the munber:")) key=a%2 if   key==0:         print("The given number is even") else:         print("The given number is odd")

Write a Python program to compute distance between two points taking input from the user. Formula for Pythagorean theorem for compute distance between two points is:

1)b)Write a Python program to compute distance between two points taking input from the user. Formula for Pythagorean theorem for compute distance between two points is:   n1=int(input("Enter the value of point 1_ x-axis")) n2=int(input("Enter the value of point 1_y-axis")) n3=int(input("Enter the value of point 2 _x-axis ")) n4=int(input("Enter the value of point 2_y-axis")) distance=(((n3-n1)**2)+((n4-n2)**2))**(0.5) print("The distance for the given two cartesian points   are ",distance)