0% found this document useful (0 votes)
37 views

Assaingment 2 0901CS201007

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Assaingment 2 0901CS201007

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

assignments

May 23, 2023

ASSIGNMENT 2
[ ]: # D1. Write a program to find greatest of three numbers.
a = int(input())
b = int(input())
c = int(input())

if(a > b and a > c):


maximum = a
elif(b > a and b > c):
maximum = b
else:
maximum = c

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)

[ ]: # D3. Find if given number is even or odd.


num = abs(int(input()))

if(num%2 == 0 or num == 0):


result = 'Even'
else:

1
result = 'Odd'

print(result)

[ ]: # D4. Get input a number from user (maximum 4 digits) and decide whether number␣
↪is a

# single/double/triple/four digit number.


import math
print('Enter the Number')
num = int(input())

num = math.floor(num/10)
count = 1
while num > 0:
num = math.floor(num/10)
count = count + 1

print('The number of digits are: ')


print(count)

[ ]: # S1. Python program to check whether the string is Palindrome


import math
print('Enter the number: ')
num = int(input())

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)

[ ]: # S2. Reverse words in a given String in Python


print('Enter the word here: ')
word = input()
revword = ''

for i in range(0, len(word)):


revword = revword + (word[len(word)-1-i])

2
print('The reverse is')
print(revword)

[2]: # S4. Count the Number of matching characters in a pair of string


def count(str1 ,str2) :

set_string1 = set(str1)
set_string2 = set(str2)
matched_characters = set_string1 & set_string2

print("No. of matching characters are : " +␣


↪str(len(matched_characters)) )

if __name__ == "__main__" :

str1 = input('Enter first string')


str2 = input('Enter second string')

count( str1 , str2 )

No. of matching characters are : 0

[4]: # Python program for removing i-th character from a string


string = input()
i = 5
a = string[: i]

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

while abs(y - z) >= 0.00001:


y = z

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

[ ]: # 3.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))

[ ]: # Python Program to Swap Two Variables


x = 5
y = 10

temp = x
x = y

4
y = temp

print('The value of x after swapping: {}'.format(x))


print('The value of y after swapping: {}'.format(y))

[ ]: # 5.Python Program to Convert Kilometers to Miles


kilometers = float(input("Enter value in kilometers: "))

convfac = 0.621371

miles = kilometers * convfac


print('%0.2f kilometers is equal to %0.2f miles' %(kilometers,miles))

[ ]: # 6.Python Program to Convert Celsius to Fahrenheit


celsius = 37.5

fahrenheit = (celsius * 1.8) + 32


print('%0.1f degree Celsius is equal to %0.1f degree Fahrenheit'␣
↪%(celsius,fahrenheit))

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)

[16]: # 2.Python Program to Display the multiplication Table

num = int(input())

for i in range(1, 11):


print(num, 'x', i, '=', num*i)

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

[ ]: # 3.Python Program to Print the Fibonacci sequence


nterms = int(input("How many terms? "))

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

[ ]: # 4.Python Program to Check Armstrong Number

num = int(input("Enter a number: "))

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")

[ ]: # 5.Python Program to Find the Sum of first 50 Natural Number


n=int(input("Enter a number: "))
sum1 = 0
while(n > 0):

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

list1 = [11, 5, 17, 18, 23]

for element in range(0, len(list1)):


total = total + list1[element]

print("Sum of all elements in given list: ", total)

[19]: #2.Write a python program to find largest number in a given list without using␣
↪max().

list1 = [10, 20, 4, 45, 99]

max = list1[0]

for x in list1:
if x > max:
max = x

print("Largest element is:", max)

Largest element is: 99

[22]: # 3.Write a python program to print all even numbers from a given list.

list1 = [10, 21, 4, 45, 66, 93]

for num in list1:


if num % 2 == 0:
print(num, end=" ")

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.

list1 = [10, 21, 4, 45, 66, 93]

7
for num in list1:
if num % 2 == 0:
print(num, end=" ")

print(end='\n')

for num in list1:


if num % 2 != 0:
print(num, end=" ")

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)

[2, 4, 10, 20, 5]

[28]: # 6.Write a python program to sort a given list of numbers without using sort()␣
↪function

my_list = [15, 26, 15, 1, 23, 64, 23, 76]


new_list = []

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)

[1, 15, 15, 23, 23, 26, 64, 76]


ASSIGNMENT 5
[31]: # T1. Write a Python program to find the index of an item of a tuple.

8
tuplex = tuple("index tuple")
print(tuplex)

index = tuplex.index("p")
print(index)

('i', 'n', 'd', 'e', 'x', ' ', 't', 'u', 'p', 'l', 'e')
8

[32]: # T2. Write a Python program to find the length of a tuple.


# Creating tuples
Tuple = (1, 3, 4, 2, 5, 6)

res = len(Tuple)
print('Length of Tuple is', res)

Length of Tuple is 6

[34]: # T3. Write a Python program to reverse a tuple.


tuples = ('z','a','d','f','g','e','e','k')
new_tup = tuples[::-1]

print(new_tup)

('k', 'e', 'e', 'g', 'f', 'd', 'a', 'z')

[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))

tup = [('item1', '12.20'), ('item2', '15.10'), ('item3', '24.5')]


print(Sort(tup))

[('item3', '24.5'), ('item2', '15.10'), ('item1', '12.20')]

[ ]: #S.1 Write a Python program to create an intersection of sets.


A = {2, 3, 5}
B = {1, 3, 5}

print(A.intersection(B))

[36]: # S2. Write a Python program to create a union of sets.


A = {2, 4, 5, 6}
B = {4, 6, 7, 8}

9
print("A U B :", A.union(B))

A U B : {2, 4, 5, 6, 7, 8}

[39]: # S3. Write a Python program to create set difference.


s1 = {4, 9, 12}
s2 = {1, 4, 8}
s = s1.difference(s2)

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}

for element in set2:


set1.discard(element)

print(set1)

{1, 3, 5}

10

You might also like