Practical List XI CS
Practical List XI CS
Practical List XI CS
Output is:
Please Enter the First Value Number 1: 8
Please Enter the Second Value Number 2: 2
The Sum is: 10.0
The Subtraction is : 6.0
The Multiplication is: 16.0
The Division is: 4.0
The Modulus is : 0.0
The Exponent Value is: 64.0
2) Write a program to find largest of 3 numbers.
Source code:
a=int(input("Enter first no : "))
b=int(input("Enter Second no : "))
c=int(input("Enter third no : "))
if(a>b and a>c):
print(("%d is greatest")%a)
elif(b>c):
print(("%d is greatest")%b)
else:
print(("%d is greatest")%c)
Output:
Enter first no : 23
Enter Second no : 87
Enter third no : 43
87 is greatest
3) Write a Program in python that reads three integers and prints them in
ascending order.
Source code:
x = int(input("Input first number: "))
y = int(input("Input second number: "))
z = int(input("Input third number: "))
a1 = min(x, y, z)
a3 = max(x, y, z)
a2 = (x + y + z) - a1 - a3
print("Numbers in sorted order: ", a1, a2, a3)
Output is:
Input first number: 34
Input second number: 5
Input third number: -1
Numbers in sorted order: -1 5 34
4) Write a Program in python to calculate and print the sums of even and odd
integers of the first n natural numbers.
Source code:
maximum = int(input(" Please Enter the Maximum Value : "))
even_total = 0
odd_total = 0
for number in range(1, maximum + 1):
if(number % 2 == 0):
even_total = even_total + number
else:
odd_total = odd_total + number
print("The Sum of Even Numbers :", even_total)
print("The Sum of Odd Numbers :", odd_total)
Output is:
Please Enter the Maximum Value : 5
The Sum of Even Numbers : 6
The Sum of Odd Numbers : 9
5) Write a program in python which accepts number of days as integer and display
total number of years, months and days in it.
For example: if user input as 856 days the output should be 2 years 4 months 6
days.
Source code:
days=int(input("Enter Number of days:"))
no_of_years=days//365
no_of_months=(days%365)//30
no_of_days=(days%365)%30
print(days,"dayshas:",no_of_years,"Years",no_of_months,"Months",no_of_days,"Days")
Output is:
Enter Number of days:856
856 days has: 2 Years 4 Months 6 Days
6) Write a program in python to find out whether a year entered through keyboard
is leap year or not.
Source code:
y=int(input("Enter the year in yyyy format"))
j=y%4
k=y%400
l=y%100
if(j==0 or k==0 or l==0):
print("It is a leap year")
else:
print("It is not a leap year ")
Output is:
Enter the year in yyyy format2020
It is a leap year
7) Write a Program in python to display a menu for calculating area of a circle &
perimeter of a circle.
Source code:
ch='Y'
while (ch=='Y' or ch=='y'):
print("Enter 1 : Area of circle:")
print("Enter 2 : Perimeter of circle:")
opt=int(input('enter ur choice:='))
if opt==1:
r=int(input('enter radius:'))
a=3.14*r*r
print('Area of circle is:',a)
elif opt==2:
r=int(input('enter radius:'))
p=2*3.14*r
print('Perimeter of circle is:',p)
else:
print('invalid choice')
ch=(input('want to continue?'))
Output is:
Enter 1 : Area of circle:
Enter 2 : Perimeter of circle:
enter ur choice:=1
enter radius:1
Area of circle is: 3.14
want to continue?y
Enter 1 : Area of circle:
Enter 2 : Perimeter of circle:
enter ur choice:=2
enter radius:1
Perimeter of circle is: 6.28
want to continue?n
8) Write a Program in python to check whether number is a palindrome number or
not.
Source code:
n = int(input("Enter any number: "))
a=0
t=n
while(n>0):
r=n%10
a=r+(a*10)
n=n//10
if a==t:
print('no. is palindrome')
else:
print('no. is not palindrome')
Output is:
Enter any number: 101
no. is palindrome
9) Write a Program in python to input a number and test if it is a prime number.
Source code:
number = int(input("Enter any number: "))
if number > 1:
for i in range(2, number):
if (number % i) == 0:
print(number, "is not a prime number")
break
else:
print(number, "is a prime number")
else: print(number, "is not a prime number")
Output is:
Enter any number: 1
1 is not a prime number
10) Write a Program in python to print fibonacci series. Some initial elements of a
Fibonnaci series are:
0 1 1 2 3 5 8……………….
Source Code:
f=0
s=1
a=int(input("Enter the no of terms :"))
print(f,s,end=' ')
for i in range(0,a-1):
t=f+s
print(t,end=' ')
f=s
s=t
Output:
Enter the no of terms: 8
0 1 1 2 3 5 8 13 21
11) Write a Program in python to read an integer and reverse the number.
Source code:
n = int(input("Enter any number: "))
a=0
t=n
print('Reverse Number:')
while(n>0):
r=n%10
print(r,end='')
n=n//10
Output is:
Enter any number: 654
Reverse Number:
456
12) Write a program to print Binary equivalent of a given integer
Source Code:
q=int(input("Enter an integer"))
r=[]
cnt=0
while(q>=1):
if((q%2)==0):
x=0
r.append(x)
else:
x=1
r.append(x)
q=q//2
cnt=cnt+1
for i in range(cnt-1,-1,-1):
print(r[i],end=' ')
Output:
Enter an integer78
1001110
13) Write a Program in python to input two numbers and print their LCM(Least
Common Multiplier).
Source code:
a= int(input("enter a:"))
b= int(input("enter b:"))
ta=a
tb=b
while a!=b:
if a<b:
a=ta+a
else:
b=tb+b
print('LCM is:',a)
Output is:
enter a:12
enter b:15
LCM is: 60
14) Write a Program in python to input two numbers and print their HCF(Greatest
Common Factor).
Source code:
a=int(input('Enter no.1:'))
b=int(input('Enter no.2:'))
while a!=b:
if a>b:
a=a-b
else:
b=b-a
print('Gcd is:',a)
Output is:
enter a:12
enter b:15
GCD is: 3
15) Write a Program in python to check whether number is perfect or not.
Source code:
n=int(input('Enter no:'))
s=0
t=n
for i in range (1,n-1):
if n%i==0:
s=s+i
if s==t:
print('number is perfect')
else:
print('number is not perfect')
Output is:
Enter no:6
number is perfect
16) Write a Program in python to check whether number is amstrong or not.
Source code:
num = int(input("enter a number: "))
length = len(str(num))
sum = 0
temp = num
while(temp != 0):
sum = sum + ((temp % 10) ** length)
temp = temp // 10
if sum == num:
print("armstrong number")
else:
print("not armstrong number")
Output is:
enter a number: 371
amstrong number
17) Write a Program in python that reads a string and check whether it is a
palindrome string or not.
Source code:
s=input('Enter string:')
s1=s[::-1]
if s==s1:
print('string is palindrome')
else:
print('string is not palindrome')
Output is:
Enter string:madam
string is palindrome
18) Write a Python program to sum all the items in a list.
Source code:
L=[]
def sum_list(items):
sum_numbers = 0
for x in L:
sum_numbers += x
return sum_numbers
n=int(input("How many items to be entered:"))
i=1
while(i<=n):
a=int(input("Enter element:"))
L.append(a)
i=i+1
print(sum_list([L]))
Output is:
How many items to be enetered:3
Enter element:1
Enter element:2
Enter element:3
6
19) Write a Program in python to find minimum and maximum element from a list
of element.
NumList=[ ]
n=int(input("How many items to be entered:"))
i=1
while(i<=n):
a=int(input("Enter element:"))
NumList.append(a)
i=i+1
Number=len(NumList)
print("Length of this List is : ", Number)
smallest = largest = NumList[0]
for i in range(1, Number):
if(smallest > NumList[i]):
smallest = NumList[i]
if(largest < NumList[i]):
largest = NumList[i]
print("The List is:",NumList)
print("The Smallest Element in this List is : ", smallest)
print("The Largest Element in this List is : ", largest)
Output is:
Length of this List is : 6
The List is: [1, 33, 22, 88, 4, 44]
The Smallest Element in this List is : 1
The Largest Element in this List is : 88
20) Write a program to input ‘n’ employee number and name and to display all
employee’s information in ascending order based upon their number.
Source code:
empinfo=dict()
n=int(input("Enter total number of employees"))
i=1
while(i<=n):
a=int(input("enter number"))
b=input("enter name")
empinfo[a]=b
i=i+1
k=list(empinfo.keys())
k.sort()
print("Employee Information")
print("Emp No",'\t',"Employee Name")
for i in k:
print(i,'\t',empinfo[i])
Output is:
Enter total number of employees2
enter number100
enter nameaparna
enter number101
enter namearchana
Employee Information
Emp No Employee Name
100 aparna
101 archana
21) Write a Python program to concatenate one string to another string.
Source code:
nm=input("enter the string:")
nm2=input("enter the second string:")
tmp=""
k=len(nm)
x=0
while x<k:
tmp=tmp+nm[x]
x=x+1
x=0
tmp=tmp+" "
k=len(nm2)
while x<k:
tmp=tmp+nm2[x]
x=x+1
nm=tmp
print("String:",nm)
Output is:
enter the string:hello
enter the second string:world
String: hello world
22) Write a Python program to count total number of words in a string.
Source code:
nm=input("Enter the string")
k=0
for x in nm.split():
k=k+1
print("No of words: ",k)
Output is:
Enter the string : the name of book is python
No of words: 6
23) Write a Program in python to create telephone directory.
phonebook={}
n=int(input("Enter total number of friends:"))
i=1
while(i<=n):
a=input("enter name")
b=int(input("enter phone number"))
phonebook[a]=b
i=i+1
print(phonebook)
Output is:
Enter total number of friends:2
enter nameaparna
enter phone number7083190775
{'aparna': 7083190775}
enter namearchana
enter phone number4643332
{'aparna': 7083190775, 'archana': 4643332}
24) Write a program to read a list of n integers (positive as well as negative). Create
two new lists, one having all positive numbers and the other having all negative
numbers from the given list. Print all three lists.
Source code:
list1 = []
poslist=[]
nglist=[]
n=int(input("How many items to be entered:"))
i=1
while(i<=n):
a=int(input("Enter element:"))
list1.append(a)
i=i+1
for num in list1:
if num >= 0:
poslist.append(num)
else:
nglist.append(num)
print("Original list: ",list1)
print("Positive list: ", poslist)
print("Negative list: ", nglist)
Output is:
How many items to be enetered:4
Enter element:20
Enter element:-3
Enter element:-4
Enter element:29
Original list: [20, -3, -4, 29]
Positive list: [20, 29]
Negative list: [-3, -4]
25) Write program to accept the string and convert each word first
character upper.
Source code:
nm=input("Enter the string: ")
x=0
vword=""
while x<len(nm):
if x==0:
vword=vword+nm[x].upper()
elif nm[x]==" " or nm[x]==".":
vword=vword+" "+nm[x+1].upper()
x=x+1
else:
vword=vword+nm[x]
x=x+1
print(vword)
Output is:
Enter the string: i like python
I Like Python
26) Write a program to search the position of a number in a given list
Source Code:
def linearSearch(num,list1):
for i in range(0,len(list1)):
if list1[i] == num:
return i
return None
list1 = [ ]
print("How many numbers do you want to enter in the list: ")
maximum = int(input())
print("Enter a list of numbers: ")
for i in range(0,maximum):
n = int(input())
list1.append(n) #append numbers to the list
num = int(input("Enter the number to be searched: "))
result = linearSearch(num,list1)
if result is None:
print("Number",num,"is not present in the list")
else:
print("Number",num,"is present at",result + 1, "position")
Output:
How many numbers do you want to enter in the list:
5
Enter a list of numbers:
23
567
12
89
324
Enter the number to be searched:12
Number 12 is present at 3 position
27) Write a program to count the number of times a character appears in a given
string.
Source Code:
st = input("Enter a string: ")
dic = {}
for ch in st:
if ch in dic:
dic[ch] += 1
else: dic[ch] = 1
for key in dic:
print(key,':',dic[key])
Output is:
Enter a string: aparna
a:3
p:1
r:1
n:1
28) Write a python program to accept the string and count the number of upper
case character.
Source code:
nm=input("Enter the string:")
vow=0
for ch in nm:
if ch.isupper():
vow=vow+1
print("No of uppercase in a string:",vow,end=" ")
Output is:
Enter the string:HEllo
No of uppercase in a string: 2
29) Write a program to accept the string count number of digit, alphabets and other
character.
Source code:
nm=input("Enter the string: ")
x=0
cdigit=0
cnum=0
coth=0
while x<len(nm):
if nm[x].isdigit():
cdigit=cdigit+1
elif nm[x].isalpha():
cnum=cnum+1
else:
coth=coth+1
x=x+1
print("digit: ",cdigit)
print("numbers: ",cnum)
print("other character: ",coth)
Output is:
Enter the string: hello 123
digit: 3
30) Write program to accept the string and count the number of vowels in a string.
Source code:
nm=input("Enter the string")
vow=0
for ch in nm:
if ch in ("aeiouAEIOU"):
vow=vow+1
print("No of vowels in a string:",vow,end=" ")
Output is:
Enter the string:hello
No of vowels in a string: 2