Professional Documents
Culture Documents
Assaingment 2 0901CS201007
Assaingment 2 0901CS201007
ASSIGNMENT 2
[ ]: # D1. Write a program to find greatest of three numbers.
a = int(input())
b = int(input())
c = int(input())
print(maximum)
[ ]: # D2. Write a program to find the grade of a student for marks out pf 100.
print('Enter the Marks : ')
marks = int(input())
if(marks >= 85):
grade = 'A'
elif(marks >=70 and marks <=84):
grade = 'B'
elif(marks >= 55 and marks <=69):
grade = 'C'
elif(marks >=40 and marks <=54):
grade = 'D'
else:
grade = 'Fail'
print(grade)
1
result = 'Odd'
print(result)
[ ]: # D4. Get input a number from user (maximum 4 digits) and decide whether number␣
↪is a
num = math.floor(num/10)
count = 1
while num > 0:
num = math.floor(num/10)
count = count + 1
store = []
while num > 0:
rem = num%10
num = math.floor(num/10)
store.append(rem)
for i in range(0,len(store)):
if(store[i] == store[len(store)-i-1]):
result = True
else:
result = False
print(result)
2
print('The reverse is')
print(revword)
set_string1 = set(str1)
set_string2 = set(str2)
matched_characters = set_string1 & set_string2
if __name__ == "__main__" :
b = string[i + 1:]
c = a + b
print(c)
hespeo
ASSIGNMENT 1
[7]: # 1. Python Program to Find the Square Root without using Square-root function.
x = int(input())
if x < 2:
ans = x
y = x
z = (y + (x/y)) / 2
3
z = (y + (x/y)) / 2
ans = z
print(ans)
7.416198487095675
[10]: # Python Program to Calculate the Area of a Triangle where its three sides a,␣
↪b, c are
# given.
# s=(a+b+c)/2
# Area=square root of s(s-a)(s-b)(s-c)
import math
a = int(input())
b = int(input())
c = int(input())
s = (a+b+c)/2
area = math.sqrt(s*(s-a)*(s-b)*(s-c))
print(area)
6.0
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)
temp = x
x = y
4
y = temp
convfac = 0.621371
ASSIGNMENT 3
[ ]: # 1.Python Program to Find the Factorial of a Number
num = 7
factorial = 1
if num < 0:
print("factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
num = int(input())
13 x 1 = 13
13 x 2 = 26
13 x 3 = 39
13 x 4 = 52
13 x 5 = 65
5
13 x 6 = 78
13 x 7 = 91
13 x 8 = 104
13 x 9 = 117
13 x 10 = 130
n1, n2 = 0, 1
count = 0
if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
n1 = n2
n2 = nth
count += 1
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
6
sum1=sum1+n
n=n-1
print("The sum of first n natural numbers is",sum1)
ASSIGNENT 4
[ ]: # 1.Write a python program to find the sum of all numbers in a list
total = 0
[19]: #2.Write a python program to find largest number in a given list without using␣
↪max().
max = list1[0]
for x in list1:
if x > max:
max = x
[22]: # 3.Write a python program to print all even numbers from a given list.
10 4 66
[24]: # 4.Write a python program to create a list of even numbers and another list of␣
↪odd numbers from a
# given list.
7
for num in list1:
if num % 2 == 0:
print(num, end=" ")
print(end='\n')
10 4 66
21 45 93
[27]: # 5.Write a python program to remove repeated elements from a given list␣
↪without using built-in
# methods.
list = [2, 4, 10, 20, 5, 2, 20, 4]
final_list = []
for num in list:
if num not in final_list:
final_list.append(num)
print(final_list)
[28]: # 6.Write a python program to sort a given list of numbers without using sort()␣
↪function
while my_list:
min = my_list[0]
for x in my_list:
if x < min:
min = x
new_list.append(min)
my_list.remove(min)
print(new_list)
8
tuplex = tuple("index tuple")
print(tuplex)
index = tuplex.index("p")
print(index)
('i', 'n', 'd', 'e', 'x', ' ', 't', 'u', 'p', 'l', 'e')
8
res = len(Tuple)
print('Length of Tuple is', res)
Length of Tuple is 6
print(new_tup)
[35]: # T4. Write a Python program to sort a list of tuple by its float element.
def Sort(tup):
return(sorted(tup, key = lambda x: float(x[1]), reverse = True))
print(A.intersection(B))
9
print("A U B :", A.union(B))
A U B : {2, 4, 5, 6, 7, 8}
print(s)
{9, 12}
[40]: # S4.Write a Python program to check if two given sets have no elements in␣
↪common.
set1 = {1, 2, 3}
set2 = {4, 5, 6}
is_disjoint = set1.isdisjoint(set2)
print(is_disjoint)
True
[42]: # S5. Write a Python program to remove the common element of a 2nd set from the␣
↪1st set
set1 = {1, 2, 3, 4, 5}
set2 = {2, 4}
print(set1)
{1, 3, 5}
10