1.
Write a Python Program to Solve Quadratic Equation
import cmath
a=1
b=5
c=6
d = (b**2) - (4*a*c)
sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)
print('The solution are {0} and {1}'.format(sol1,sol2))
2. Write a python program to calculate the area of a given circle,given the
centre and a point on the perimeter. Use a function to find radius as the
distance between two points.
def dist(x1, y1, x2, y2, r):
print("The shortest distance between a point and a circle is ",((((x2 -
x1)** 2) + ((y2 - y1)** 2))**(1/2)) - r);
print("area =",3.14*r*r)
x1 = 4;
y1 = 6;
x2 = 35;
y2 = 42;
r = 5;
dist(x1, y1, x2, y2, r);
3. write a python program to replace a substring with a new substring in the
given string.
def replacecwithb(input, pattern, replaceWith):
return input.replace(pattern, replaceWith)
if __name__ == "__main__":
input = 'cat'
pattern = 'c'
replaceWith = 'b'
print (replacecwithb(input,pattern,replaceWith))
4. write a python program to find the sum of digits in a number
def getSum(n):
sum = 0
for digit in str(n):
sum += int(digit)
return sum
n = int(input("enter a nummber"))
print(getSum(n))
5. write a python program to count the number of zeros and negative terms
in a given set on n terms.
list1 = [10, 0, 4, 45, 66, -93, 1]
pos_count, zero_count = 0, 0
for num in list1:
if num > 0:
pos_count += 1
elif num==0:
zero_count += 1
print("Positive numbers in the list: ", pos_count)
print("zero numbers in the list: ", zero_count)
6. python program to find the average of numbers in a list.also find the cube
of the numbers in the list.
number_list = [5,6,8,9]
avg = sum(number_list)/len(number_list)
print("The average is ", round(avg,2))
res=[]
for i in number_list:
res.append(i*i*i)
print(res)
7. write a python code to search an element in a list
test_list = [ 1, 6, 3, 5, 3, 4 ]
print("Checking if 4 exists in list ( using loop ) : ")
for i in test_list:
if(i == 4) :
print ("Element Exists")
8. write a python program to check the type of triangle from the given
vertices.
Without function
print("Input lengths of the triangle sides: ")
x = int(input("x: "))
y = int(input("y: "))
z = int(input("z: "))
if x == y == z:
print("Equilateral triangle")
elif x==y or y==z or z==x:
print("isosceles triangle")
else:
print("Scalene triangle")
with function
def is_valid_triangle(a,b,c):
if a+b>=c and b+c>=a and c+a>=b:
return True
else:
return False
# Function definition for type
def type_of_triangle(a,b,c):
if a==b and b==c:
print('Triangle is Equilateral.')
elif a==b or b==c or a==c:
print('Triangle is Isosceles.')
else:
print('Triangle is Scalane')
# Reading Three Sides
side_a = float(input('Enter length of side a: '))
side_b = float(input('Enter length of side b: '))
side_c = float(input('Enter length of side c: '))
# Function call & making decision
if is_valid_triangle(side_a, side_b, side_c):
type_of_triangle(side_a, side_b, side_c)
else:
print('Tringle is not possible from given sides.')
9. write a python program to create a dictionary of phone numbers and
names of five persons.display the contents of dictionary.
phonebook_directory = dict( )
n = input (" Enter number of friends you want to add with their phone
numbers”)
A=0
while A<=n:
y = input ("enter name")
z = input ("enter phone number")
phonebook_directory [y] = z
A=A+x
name = input ("enter the name to be searched ")
g=0
K= phonebook_directory . keys( )
for A in x :
if (cmp(A,name) == 0):
print ( "Phone number= ", phonebook_directory[A] )
g=x
if (g==0):
print ( "Unknown name")