K L UNIVERSITY
DEPARTMENT OF ECE
COMPUTER NETWORKS LAB MANUAL (15CS2208)
Python Basic Programs:
1.Write a python program to print the Welcome message
print(‘Welcome to CN Lab’)
2.Write a python program to Addition of two numbers
a=15
b=10
print(a+b)
3.Write a python program to declare the variables in Dynamic and static
declaration
a=5
b,c=2.0,"CN"
d=int(input('enter the number'))
print(a)
print(b)
print(c)
print(d)
print("the values of variables are",a,b,c,d)
4.Write a python program to printing string in different formats:
str="welcome to CN Lab"
print(str)
print(str[0])
print(str[3:7])
print(str[11:])
print(str+" Manual")
5.Write a python program to find the maximum of three numbers:
a=int(input('Enter a value:'))
b=int(input('Enter b value:'))
c=int(input('Enter c value:'))
if(a>b and a>c):
print('a is maximum')
elif(b>c):
print('b is maximum')
else:
print('c is maximum')
or
print(max(a,b,c))
6.Write a python program to find out the given number is prime or not
num = int(input("Enter a number: "))
if num > 1:
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
print(i,"times",num//i,"is",num)
break
else:
print(num,"is a prime number")
else:
print(num,"is not a prime number")
7.Write a python program to find out the factorial of a given number
num = int(input("Enter a number: "))
factorial = 1
if num < 0:
print("Sorry, 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)
8.Write a python program to print the prime numbers in the given range
lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))
print("Prime numbers between",lower,"and",upper,"are:")
for num in range(lower,upper + 1):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)
9.Write a python program to print the fibonacci sequence upto n
nterms = int(input("How many terms? "))
n1 = 0
n2 = 1
count = 2
if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
print("Fibonacci sequence upto",nterms,":")
print(n1)
print(n2)
while count < nterms:
nth = n1 + n2
print(nth)
n1 = n2
n2 = nth
count += 1
10.Write a python program to find the transpose the matrix
X = [[12,7],
[4 ,5],
[3 ,8]]
result = [[0,0,0],
[0,0,0]]
for i in range(len(X)):
for j in range(len(X[0])):
result[j][i] = X[i][j]
for r in result:
print(r)
11.Write a python program to sort the words in a given sentence
my_str = "Welcome to CN Lab"
words = my_str.split()
words.sort()
print("The sorted words are:")
for word in words:
print(word)
12.Write a python program to count the number of vowels
vowels = 'aeiou'
ip_str = 'Welcome to CN Lab'
ip_str = ip_str.casefold()
count = {}.fromkeys(vowels,0)
for char in ip_str:
if char in count:
count[char] += 1
print(count)
13.Write a python program to Find Numbers Divisible by Another
Number
my_list = [12, 65, 54, 39, 102, 339, 221,]
# use anonymous function to filter
result = list(filter(lambda x: (x % 13 == 0), my_list))
# display the result
print("Numbers divisible by 13 are",result)