Harri Es
Harri Es
NO NO
1 To input a welcome message and 3
display it
2 To input two numbers and display the 4
largest/smaller number
3 To input three numbers and display the 4-5
largest/smaller number
1.
Aim: To input a welcome message and display it
Program:
a=input("enter a wlecome message:")
print(a)
output:
enter a wlecome message:hello
hello
2.
Aim: To input two numbers and display the largest/smaller number
Program:
a=int(input("enter a number:"))
3
b=int(input("enter a number:"))
if a>b:
print(a,"is greater")
print(b,"is smaller")
else:
print(a,"is smaller")
print(b,"is greater")
output:
enter a number:3
enter a number:5
3 is smaller
5 is greater
3.
Aim: To input three numbers and display the largest/smaller number
Program:
a=int(input("enter a number:"))
b=int(input("enter a number:"))
c=int(input("enter a number:"))
if a>b and a>c:
print(a,"is greater")
if b<c:
print(b,"is smaller")
else:
print(c,"is smaller")
4
elif b>a and b>c:
print(b,"is greater")
if a<c:
print(a,"is smaller")
else:
print(c,"is smaller")
elif c>a and c>b:
print(c,"is greater")
if a<b:
print(a,"is smaller")
else:
print(b,"is smaller")
output:
enter a number:5
enter a number:6
enter a number:4
6 is greater
4 is smaller
4.
Aim: To generate the following patterns using nested loop
Program:
for i in range(1,7):
for j in range(1,i):
print("*",end='')
5
print()
output:
*
**
***
****
*****
5.
Aim: To generate the following pattern using nested loop
Program:
for i in range(7,1,-1):
for j in range(1,i):
print(j,end='')
print()
output:
123456
12345
1234
123
12
1
6.
Aim: To generate the following pattern using nested loop
Program:
6
for i in range(1,7):
a=65
for j in range(1,i):
print(chr(a),end='')
a=a+1
print()
output:
A
AB
ABC
ABCD
ABCDE
7.
Aim: To write a program to input the value of x and n and print the
sum of the following series
Program:
sum=1
x=int(input("enter a number"))
n=int(input("enter a number"))
for i in range(1,n+1):
sum=sum+x**i
print(sum)
output:
enter a number2
7
enter a number5
63
8.
Aim: To write a program to input the value of x and n and print the
sum of the following series
Program:
sum=1
x=int(input("enter a number"))
n=int(input("enter a number"))
for i in range(1,n+1):
y=x**i
if i%2!=0:
y=y*-1
sum=sum+y
print(sum)
output:
enter a number2
enter a number5
-21
9.
Aim: To write a program to input the value of x and n and print the
sum of the following series
Program:
x=int(input("enter a number:"))
8
n=int(input("enter a number:"))
sum=0
for i in range(2,n+1):
y=x**i/i
sum=sum+y
print(sum)
output:
enter a number:5
enter a number:9
280447.3115079365
10.
Aim: To write a program to input the value of x and n and print the
sum of the following series
Program:
import math
x=int(input("enter a number:"))
n=int(input("enter a number:"))
sum=0
for i in range(2,n+1):
y=x**i/math.factorial(i)
sum=sum+y
print(sum)
9
output:
enter a number:2
enter a number:5
6.266666666666667
11.
Aim: To determine whether a number is a perfect number
Program:
n=int(input("enter a number"))
sum=0
for i in range(1,n):
if(n%i==0):
sum=sum+i
if(sum==n):
print(n,"is perfect number")
output:
enter a number6
6 is perfect number
12.
Aim: To determine whether a number is an armstrong number
Program:
n=int(input("enter a 3 digit number:"))
sum=0
x=sum
10
while(x>0):
y=x%10
sum=y**3
x//=10
if(n==sum):
print(n,"is armstrong number")
else:
print(n,"is not armstrong number")
output:
enter a 3 digit number:412
412 is not armstrong number
13.
Aim: To determine whether a number is a palindrome
Program:
n=int(input("enter a number:"))
x=str(n)
y=x[::-1]
if x==y:
print(n,"is palindrome")
else:
print(n,"is not palindrome")
output:
enter a number:12321
12321 is palindrome
11
14.
Aim: To input a number and check if the number is a prime or
composite number
Program:
n=int(input("enter a number"))
for i in range(2,n):
if n%i==0:
print("composite")
break
else:
print("prime")
output:
enter a number3
prime
15.
Aim: To display the terms of a Fibonacci series
Program:
n=int(input("enter a number"))
x=0
y=1
print(x)
print(y)
for i in range(n-2):
z=x+y
12
print(z)
x=y
y=z
output:
enter a number4
0
1
1
2
16.
Aim: To compute the greatest common divisor
Program:
import math
n1=int(input("enter a number"))
n2=int(input("enter a number"))
print(math.gcd(n1,n2))
output:
enter a number2
enter a number6
2
17.
Aim: To compute the least common divisor
Program:
import math
13
n1=int(input("enter a number"))
n2=int(input("enter a number"))
print((n1*n2)//math.gcd(n1,n2))
output:
enter a number6
enter a number8
24
18.
Aim: To count and display the number of
vowels,consonants,uppercase,lowercase characters in string
Program:
s=input("enter a string")
u=0
l=0
v=0
c=0
for i in s:
if i.isupper():
u+=1
if i.islower():
l+=1
if i in "aeiouAEIOU":
v+=1
elif i.isalpha():
14
c+=1
print("no of upper case:",u)
print("no of lower case:",l)
print("no of vowels:",v)
print("no of consonant:",c)
output:
enter a stringHArrIEs
no of upper case: 4
no of lower case: 3
no of vowels: 3
no of consonant: 4
19.
Aim: To input a string and determine whether it is a palindrome or
not
Program:
s=input("enter a string")
if s==s[::-1]:
print(s,"is palindrome")
else:
print(s,"is not palindrome")
output:
enter a string:dad
dad is palindrome
20.
15
Aim: To convert the case of character in string
Program:
s=input("enter a string :")
print(s.swapcase())
output:
enter a string :JAsim
jaSIM
21.
Aim: To find largest/smallest number in list
Program:
n=eval(input("enter a list of numbers"))
print("maximum",max(n))
print("minimum",min(n))
output:
enter a list of numbers[20,24,35,69,58]
maximum 69
minimum 20
22.
Aim: To find largest/smallest number in tuple
Program:
n=eval(input("enter a tuple of number"))
print("maximum",max(n))
print("minimum",min(n))
output:
16
enter a tuple of number(65,25,48,14)
maximum 65
minimum 14
23.
Aim: To input a list of numbers and swap elements at even location
with the elements at the odd location
Program:
l=eval(input("enter a list of numbers"))
for i in range(0,len(l)-1,2):
l[i],l[i+1]=l[i+1],l[i]
print(l)
output:
enter a list of numbers[25,14,35,65]
[14, 25, 65, 35]
24.
Aim: To input a list of element , search for a given elements in the list
Program:
l=eval(input("enter a list of numbers:"))
x=int(input("enter the number to be found:"))
y=l.index(x)
print(x,"is at the index",y)
output:
enter a list of numbers:[25,14,35,65]
enter the number to be found:14
17
14 is at the index 1
25.
Aim: To input a tuple of element , search for a given elements in the
tuple
Program:
l=eval(input("enter a tuple of numbers:"))
x=int(input("enter the number to be found:"))
y=l.index(x)
print(x,"is at the index",y)
output:
enter a tuple of numbers:(25,45,35,65,85)
enter the number to be found:35
35 is at the index 2
26.
Aim: To create a dictionary with roll number, name and marks of n
students in a class and display the name of students who have mark
above 75
Program:
n=int(input("enter a number:"))
d={}
for i in range(n):
roll=int(input("enter the roll number:"))
name=input("enter the name:")
mark=int(input("enter the mark of student:"))
d[roll]=[name,mark]
18
print(d)
for i in d:
if d[i][1]>75:
print(d[i][0])
output:
enter a number:3
enter the roll number:1
enter the name:midhun
enter the mark of student:72
enter the roll number:2
enter the name:jasim
enter the mark of student:80
enter the roll number:3
enter the name:dhivakar
enter the mark of student:88
{1: ['midhun', 72], 2: ['jasim', 80], 3: ['dhivakar', 88]}
jasim
Dhivakar
27.
Aim:To find absolute value
Program:
x=int(input("enter a number:"))
if x<0:
print(x*-1)
19
else:
print(x)
output:
enter a number:5
5
28.
Aim:To sort 3 numbers
Program:
x=int(input("enter a number:"))
y=int(input("enter a number:"))
z=int(input("enter a number:"))
if x<y and x<z:
print(x,end=" ")
if y<z:
print(y,z,sep=" ")
else:
print(z,y,sep=" ")
elif y<x and y<z:
print(y,end=" ")
if x<z:
print(x,z,sep=" ")
else:
print(z,x,sep=" ")
elif z<x and z<y:
20
print(z,end=" ")
if x<y:
print(x,y,sep=" ")
else:
print(y,x,sep=" ")
else:
print("all values are equal")
output:
enter a number:25
enter a number:65
enter a number:35
25 35 65
29.
Aim: To find divisibility of a number
Program:
x=int(input("Enter a number:"))
if x%10==0:
print(x,"is divisible of 10")
else:
print(x,"is not divisible of 10")
output:
Enter a number:25
25 is not divisible of 10
30.
21
Aim: to find the factorial of a positive number
Program:
x=int(input("enter a number:"))
n=1
for i in range(1,x+1):
n=n*i
print(n)
output:
enter a number:6
720
31.
Aim: To find mean numeric values stored in the list
Program:
l=[20,30,50,47,12]
print(sum(l)/len(l)
ouput:
31.8
32.
Aim: To find linear search on list of numbers
Program:
l=[20,58,2,14,57]
x=int(input("enter the number to be found:"))
for i in l:
if x==i:
22
print(x,"is in the index of",l.index(x))
break
else:
print(x,"is not in the list")
output:
enter the number to be found:57
57 is in the index of 4
33.
Aim: To find frequency of elements in the list
Program:
l=[4,2,4,6,8,2,1,4]
x=int(input("enter the number:"))
c=0
for i in l:
if x==i:
c=c+1
print(x,"appears",c,"in the list")
ouput:
enter the number:4
4 appears 3 in the list
34.
Aim:To find mean of values stored in the tuple
Program:
x=(20,45,35,85,65)
23
print(sum(x)/len(x))
output:
50.0
35.
Aim: To find linear search on a tuple of numbers
Program:
x=(20,45,68,95,58)
n=int(input("enter the number to be found:"))
for i in x:
if n==i:
print(n,"is in the index",x.index(n))
break
else:
print(n,"is not in the tuple")
output:
enter the number to be found:95
95 is in the index 3
36.
Aim: To count the frequency of element in a tuple
Program:
x=(25,47,65,65,25,47,65)
c=0
n=int(input("enter a number:"))
for i in x:
24
if n==i:
c=c+1
print(n,"appears",c,"in the tuple")
output:
enter a number:25
25 appears 2 in the tuple
37.
Aim: to count the number of times a character appears in a given
string using dictionary
Program:
s="hari ram"
d={}
for i in s:
if i not in d:
d[i]=1
else:
d[i]=d[i]+1
print(d)
output:
{'h': 1, 'a': 2, 'r': 2, 'i': 1, ' ': 1, 'm': 1}
38.
Aim: To create a dictionary with names of employees their salary and
access them
Program:
25
d={}
x=int(input("enter a number:"))
for i in range(x):
name=input("enter the name:")
salary=int(input("enter the salary:"))
d[name]=salary
print(d)
output:
enter a number:3
enter the name:a
enter the salary:159
enter the name:b
enter the salary:357
enter the name:c
enter the salary:258
{'a': 159, 'b': 357, 'c': 258}
26
27