CS018 - Python Lab Manual
CS018 - Python Lab Manual
celsius = 37.5
# calculate fahrenheit
OUTPUT:
(b)Area of a circle:
Radius=int(input("enter the value R"))
circle=3.14*radius*radius print("Area of
circle", circle)
OUTPUT:
C) Simple Interest for the given principal amount
PROGRAM:
P=int(input("enter the value P"))
N=int(input("enter the value N"))
R=int(input("enter the value R"))
SI=P*N*R/100 print("Simple Interest is", SI)
OUTPUT:
2. PROGRAMS USING CONDITIONAL STATEMENTS
OUTPUT:
Enter 'x' for exit.
Enter marks obtained in 5 subjects:
67
68
69
54
74
Your Grade is B
b) Greatest of three numbers.
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
if (num1 > num2) and (num1 > num3):
largest = num1
elif (num2 > num1) and (num2 > num3):
largest = num2
else:
largest = num3
print("The largest number is",largest)
OUTPUT:
Output:
The solution are (-3+0j) and (-2+0j)
3. PROGRAMS USING CONTROL STATEMENTS
if num < 0:
print("Enter a positive number")
else:
sum = 0
# use while loop to iterate un till zero
while(num > 0):
sum += num
num -= 1
print("The sum is",sum)
OUTPUT:
n=int(input("Enter number:"))
fact=1
while(n>0):
fact=fact*n
n=n-1
print("Factorial of the number is: ")
print(fact)
OUTPUT:
Case 1:
Enter number:5
Factorial of the number is:
120
Case 2:
Enter number:4
Factorial of the number is:
24
num = 12
OUTPUT:
12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
12 x 6 = 72
12 x 7 = 84
12 x 8 = 96
12 x 9 = 108
12 x 10 = 120
OUTPUT:
Case 1:
Enter the first number of the series 0
Enter the second number of the series 1
Enter the number of terms needed 4
0112
Case 2:
Enter the first number of the series 2
Enter the second number of the series 4
Enter the number of terms needed 5
2 4 6 10 16
OUTPUT:
Select operation.
1.Add
2.Subtract
3.Multiply
4.Divide
Enter choice(1/2/3/4): 3
Enter first number: 15
Enter second number: 14
15 * 14 = 210
if(num%2==0):
print(num," Is an even")
else:
print(num," is an odd")
find_Evenodd(num);//function call
OUTPUT;
Enter a number for check odd or even: 349
349 is an odd
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
num = 7
OUTPUT:
The factorial of 7 is 5040
OUTPUT:
Fibonacci sequence:
0
1
1
2
3
5
8
13
21
34
5. PROGRAMS USING STRINGS
string=input("Enter string:")
if(string==string[::-1]):
print("The string is a palindrome")
else:
print("The string isn't a palindrome")
OUTPUT:
Case 1:
Enter string :malayalam
The string is a palindrome
Case 2:
Enter string: hello
The string isn't a palindrome
if(sorted(s1)==sorted(s2)):
print("The strings are anagrams.")
else:
print("The strings aren't anagrams.")
OUTPUT:
Case 1:
Case 2:
OUTPUT:
Case 1:
Enter string:Hello world
Number of words in the string:
2
Number of characters in the string:
11
Case 2:
Enter string:I love python
Number of words in the string:
3
Number of characters in the string:
13
OUTPUT:
The sorted words are:
Example
Hello
Is
With
an
cased
letters
this
string=raw_input("Enter string:")
vowels=0
for i in string:
if(i=='a' or i=='e' or i=='i' or i=='o' or i=='u' or i=='A' or i=='E' or i=='I' or i=='O' or
i=='U'):
vowels=vowels+1
OUTPUT:
Case 1:
Enter string:Hello world
Number of vowels are:
3
Case 2:
Enter string:WELCOME
Number of vowels are:
3
6. PROGRAMS USING LIST
# driver code
a = [3, 4, 1, 3, 4, 5]
minimum(a, len(a))
OUTPUT:
for i in range(1,n1+1):
b=int(input("Enter element:"))
a.append(b)
for i in range(1,n2+1):
d=int(input("Enter element:"))
c.append(d)
new=a+c
new.sort()
OUTPUT:
Case 1:
Enter number of elements:4
Enter element:56
Enter element:43
Enter element:78
Enter element:12
('Second largest number is:', 56)
Case 2:
Enter the number of elements in list 1 : 0
Enter element 1 : 12
Enter element 2 : 12
Enter element 3 : 12
The union is :
[12]
a=[]
n=int(input("Enter number of elements:"))
for i in range(1,n+1):
b=input("Enter element:")
a.append(b)
a.sort(key=len)
print(a)
OUTPUT:
Case 1:
Enter number of elements:4
Enter element:"Apple"
Enter element:"Ball"
Enter element:"Cat"
Enter element:"Dog"
['Cat', 'Dog', 'Ball', 'Apple']
Case 2:
Enter number of elements:4
Enter element:"White"
Enter element:"Red"
Enter element:"Purple"
Enter element:"Orange"
['Red', 'White', 'Purple', 'Orange']
c) Perform linear search in a given list
def search(list,n):
for i in range(len(list)):
if list[i] == n:
return True
return False
# Driver Code
n = 'Geeks'
if search(list, n):
print("Found")
else:
print("Not Found")
OUTPUT:
Found
lower = 900
upper = 1000
OUTPUT:
d1={'A':1,'B':2}
d2={'C':3}
d1.update(d2)
OUTPUT:
Case 1:
d={'A':1,'B':2,'C':3}
if key in d.keys():
print("Key is present and value of the key is:")
print(d[key])
else:
print("Key isn't present!")
OUTPUT:
Case 1:
Case 2:
d={'A':10,'B':10,'C':239}
tot=1
for i in d:
tot=tot*d[i]
print(tot)
OUTPUT:
Case 1:
23900
d = {'a':1,'b':2,'c':3,'d':4}
print("Initial dictionary")
print(d)
if key in d:
del d[key]
else:
print("Key not found!")
exit(0)
print("Updated dictionary")
print(d)
OUTPUT:
Case 1:
Initial dictionary
{'a': 1, 'c': 3, 'b': 2, 'd': 4}
Enter the key to delete(a-d):c
Updated dictionary
{'a': 1, 'b': 2, 'd': 4}
Case 2:
Initial dictionary
{'a': 1, 'c': 3, 'b': 2, 'd': 4}
Enter the key to delete(a-d):g
Key not found!
test_string=input("Enter string:")
l=[]
l=test_string.split()
wordfreq=[l.count(p) for p in l]
print(dict(zip(l,wordfreq)))
OUTPUT:
Case 1:
Case 2:
f=open("sample.txt","r")
word=input (" Enter a word: ")
c=0
for x in f:
a=x.split()
print(a)
for i in range (0, len(a)):
if (word == a[i]):
c=c+1
print (“The word ","\"“, word,"\"“, "appears “, c," times ")
OUTPUT:
Contents of file:
hello world hello
hello
Output:
f=open("sample.txt","r")
l=input (" Enter a letter: ")
c=0
for line in f:
a=line.split()
print(a)
for word in a:
for j in range (0, len(word)):
if(l==word[j]):
c=c+1
print (“The letter ","\"“, l,"\"“, "appears “, c," times ")
OUTPUT:
Contents of file:
Enter a letter: l
f=open("sample.txt","r")
c=0
for x in f:
a=x.split()
print(a)
c=c + len(a)
print (“Total number of words = “, c)
OUTPUT:
Contents of file:
Hello world
f=open("sample.txt","r")
l=0
for line in f:
a=line.split()
for i in a:
for letter in i:
l=l+1
print (" The total number of letters is = “, l)
OUTPUT:
Contents of file:
hello world hello
hello
f=open("sample.txt","r")
c=0
for x in f:
print(x)
c=c+1
print (" The number of lines: “, c)
OUTPUT:
Contents of file:
Hello world
Hello world
a) Bar Graph
# plot title
plt.title('My bar chart!')
# function to show the plot
plt.show()
Output:
b) Scatter Plot
# y-axis values
y = [2,4,5,7,6,8,9,11,12,12]
# plotting points as a scatter plot
plt.scatter(x, y, label= "stars", color= "green", marker= "*", s=30)
# x-axis label
plt.xlabel('x - axis')
# frequency label
plt.ylabel('y - axis')
# plot title
plt.title('My scatter plot!')
# showing legend
plt.legend()
# function to show the plot
plt.show()
Output:
c) Pie Chart
Output:
d) Histogram
# frequency label
plt.ylabel('No. of people')
# plot title
plt.title('My histogram')
# function to show the plot
plt.show()
Output: