Utkarsh - Midsem Break
Utkarsh - Midsem Break
UTKARSH AMRITRAJ
20243301
Q 1(a)
list = ['Ramesh','Naveen','Rajesh','Alok']
letters = [word[0] for word in list]
print(letters)
OUTPUT:
Q1 (b)
num1 = [10,20,30,40]
num2=[30,40,50,60]
common = list(set(num1)&set(num2))
print("The common list is ",common)
OUTPUT:
Q 1(c)
name =
['the','quick','brown','fox','jumps','over','the','l
azy','dog']
u = len(name)
list1 = []
for i in name:
k = [i,len(i)]
print(k,end=" ")
OUTPUT:
Q 2)
def index(string):
lenth = len(string)
for i in range(length):
print("The positive index is", i ,"and the
negative index is",i-length,"for",string[i])
OUTPUT:
Q 3)(a)
for i in range(1,6):
for j in range(1,6):
print('*',end=" ")
print()
OUTPUT:
Q 3(b)
for i in range(1,6):
for j in range(0,6):
print(i,end=" ")
print()
OUTPUT:
Q 3(c)
for i in range(1,6):
for j in range(0,5):
print(chr(65+j),end=" ")
print()
OUTPUT:
Q 3(d)
for i in range(1,6):
for j in range(5,0,-1):
print(j,end=" ")
print()
OUTPUT:
Q 3(e)
for i in range(1,6):
for j in range(0,i):
print("*",end=" ")
print()
OUTPUT:
Q 3(f)
for i in range(1,6):
for j in range(1,i+1):
print(j,end=" ")
print()
OUTPUT:
Q 3(g)
for i in range(1,6):
for j in range(5,i-1,-1):
print("*",end=" ")
print()
OUTPUT:
Q 3 (h)
for i in range(1,6):
for j in range(5,i-1,-1):
print(chr(65+i-1),end=" ")
print()
OUTPUT:
Q3 (i)
for i in range(1,6):
for j in range(1,6-i):
print(" ",end="")
for k in range(1,i+1):
print('*',end=" ")
print(" ")
OUTPUT:
Q 3(j)
n = int(input("Enter number of rows : "))
for i in range(1,n+1):
for j in range(1,n+1-i):
print(" ",end="")
for k in range(1,i+1):
print(chr(65+n-k),end=" ")
print()
OUTPUT:
Q 4(a)
def largest_of_three(a,b,c):
if a>=b and a>=c:
print(a,"is the largest")
elif b>=a and b>=c:
print(b,"is the largest")
else:
print(c,"is the largest")
largest_of_three(a,b,c)
OUTPUT:
Q 4)(b)
def smallest_of_two(a,b):
if a>b:
print(b,"is the smallest")
else:
print(a,"is the smallest")
a=int(input("Enter first number :"))
b=int(input("Enter second number :"))
smallest_of_two(a,b)
OUTPUT:
Q 4)(c)
def gcd(x,y):
while(y!=0):
x=x%y
x,y=y,x
print("The GCD of the two numbers is : ",x)
x=int(input("Enter first number : "))
y=int(input("Enter second number : "))
gcd(x,y)
0UTPUT:
Q 4(d)
def lcm(x,y):
a=x
b=y
while(y!=0):
x=x%y
x,y=y,x
return (a*b)/x
OUTPUT:
Q 4(e)
def right_triangle(a,b,c):
u=max(a,b,c)
v=min(a,b,c)
w=a+b+c-u-v
if(u*u==v*v + w*w):
print("The triangle is right angled")
else:
print("The triangle is not right angeled")
right_triangle(a,b,c)
OUTPUT:
Q 4(f) def leapyear(a):
if(a%4==0):
print(a,"is a leap year")
else:
print(a,"is not a leap year")
a=int(input("Enter year : "))
leapyear(a)
OUTPUT:
Q 4(g)
def armstrong(a):
num=a
length = len(str(a))
new_num=0
while(a!=0):
u=a%10
new_num=new_num+pow(u,length)
a=a//10
if(num==new_num):
print(num,"is Armstrong number")
else:
print(num,"is not an Armstrong number")
a=int(input("Enter a number : "))
armstrong(a)
OUTPUT:
Q 4(h)
def fib(n):
a=0
b=1
print(a,end=" ")
print(b,end=" ")
for i in range(3,n+1):
num=a+b
print(num,end=" ")
a=b
b=num
n=int(input("Enter the number of digits : "))
fib(n)
OUTPUT
Q 4(i)
def table(n):
for i in range(1,11):
u=n*i
print(u,end=" ")
n=int(input("Enter the number : "))
table(n)
OUTPUT:
Q 4(j)
def palindrome(a):
b=a[::-1]
if(b==a):
print(a,"is a palindrome")
else:
print(a,"is not a palindrome")
a = input("Enter set of strings: ")
palindrome(a)
OUTPUT:
Q 4(k)
def punc(char):
pun = "?,&,.:;!"
c=""
for char in char:
if char not in pun:
c=c+char
print("String without punctuations : ",c)
a=input("Enter a string : ")
punc(a)
OUTPUT:
Q 4(l)
def vowel(char):
vowel=("aeiou")
for i in char:
if i in vowel:
print(char,"is a vowel")
else:
print(char,"is not a vowel")
char = input("Enter an alphabet : ")
vowel(char)
OUTPUT:
Q 4(m)
def count_vowels(char):
vowels = "a" , "e" ,"i" , "o" , "u"
n=0
for i in char:
if i in vowels:
n=n+1
print("The number of vowels in character is
",n)
char = input("Enter a string : ")
count_vowels(char)
OUTPUT: