Python Lab File

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

SUNDERDEEP ENGINEERING COLLEGE , GHAZIABAD

LAB FILE
OF
PYTHON PROGRAMMING
SUBJECT CODE : KC453

Department of Computer Science & Engineering


B.Tech CSE-2nd Year
Session:2022-23

Submitted To: Submitted By:


Mr.Ashif Ali Student’s Name:
(Assistant Professor,CSE) Section:A
Roll.No:

[1]
INDEX

S.No Name of the Programmes Page.No. Date Signature


1. Write a program to
generate a random number. 3-4 24/04/2023
2. Write a program to check
whether the given number 5-6 15/05/2023
is even or odd.
3. Write a program of
Fibonacci series using 7-8 15/05/2023
recursion.
4. Write a program to create a
list of tuples from given list
having number and its cube 9-10 22/05/2023
in each tuple using list
comprehension.
5. Write a program of 11-12 22/05/2023
factorial using recursion.
6. Write a program to
compute LCM and GCD of 13-14 29/05/2023
two numbers.
7. Write a program to 15-16 29/05/2023
implement Selection Sort.
8. Write a program to 17-18 05/06/2023
implement Linear Search
9. Write a program to 19-20 05/06/2023
implement binary Search
10. Write a program to find the
sum of all primes. 21-22 12/06/2023
11. Write a program to count
frequency of characters in 23-24 19/06/2023
string using dictionary.
12. Write a program to print 25-26 26/06/2023
each line of a file in reverse
order.

[2]
Program 1:- Write a program to generate a random number.

import random
number=random.randint(1,100)
guess_number=int(input('Enter the guess number :'))
if guess_number==number:
print('Your guess number is correct.You win!!')
else:
if guess_number>number:
print('Your guess number is too high!!')
else:
print('Your guess number is too low!!')
print(f'Machine generated number is {number}')

[3]
Output

[4]
Program 2:- Write a program to check whether the given number
is even or odd.

num = int(input("Enter a number: "))


if(num % 2) == 0:
print("{0} is Even".format(num))
else:
print("{0} is Odd".format(num))

[5]
Output

[6]
Program 3:-Write a program of Fibonacci series using recursion.

def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
num=int(input("Enter the number:"))
if num==0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(num):
print(recur_fibo(i))

[7]
Output

[8]
Program 4:-Write a program to create a list of tuples from given
list having number and its cube in each tuple using list.
comprehension.

list1 = [1,2,5,6]
#using list comprehension
res = [(val, pow(val, 3)) for val in list1]

#printing output
print(res)

[9]
Output

[10]
Program 5:- Write a program of factorial using recursion.

def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
num = int(input("Enter a number: "))
if num < 0:
print("Factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of",num,"is",recur_factorial(num))

[11]
Output

[12]
Program 6:- Write a program to compute LCM and GCD of two
numbers.

num1 = int(input("Enter first number: "))


num2 = int(input("Enter second number: "))

a = num1
b = num2
while b != 0:
temp = b
b = a%b
a = temp

gcd = a
lcm = int((num1*num2)/gcd)

print("The GCD and LCM of the numbers are {} and {}".format(gcd,


lcm))

[13]
Output

[14]
Program 7:-Write a program to implement Selection Sort.

Arr = [10,2,4,15,3,7]
print("Unsorted Array")
print(Arr)
for i in range(len(Arr)):
min_idx = i
for j in range(i+1, len(Arr)):
if Arr[min_idx] >Arr[j]:
min_idx = j

Arr[i], Arr[min_idx] = Arr[min_idx], Arr[i]

print("Sorted Array")
print(Arr)

[15]
Output

[16]
Program 8:-Write a program to implement linear search.

def linear_Search(list1, n, key):


for i in range(0, n):
if (list1[i] == key):
return i+1
return -1
list1 = [1 ,3, 5, 4, 7, 9,17,21,34]
key =17
n = len(list1)
result = linear_Search(list1, n, key)
if(result == -1):
print("Element not found")
else:
print("Element found at index: ", result)

[17]
Output

[18]
Program 9:-Write a program to count implement binary search.

def binary_search(list1, n):


low = 0
high = len(list1) - 1
mid = 0
while low <= high:
mid = (high + low) // 2
if list1[mid] < n:
low = mid + 1
elif list1[mid] > n:
high = mid -1
else:
return mid
return -1
list1 = [12, 24, 32, 39, 45, 50, 54]
n = 45
result = binary_search(list1, n)
if result != -1:
print("Element is present at index", str(result))
else:
print("Element is not present in list1")

[19]
Output

[20]
Program 10:-Write a program to find sum of all the primes .

N=2000000
s=0 # variable s will be used to find the sum of all prime.
Primes=[True for k in range(N+1)]
p=2
Primes[0]=False # zero is not a prime number.
Primes[1]=False #one is also not a prime number.
while(p*p<=N):
if Primes[p]==True:
for j in range(p*p,N+1,p):
Primes[j]=False
p+=1
for i in range(2,N):
if Primes[i]:
s+=i
print('The sum of prime numbers:',s)

[21]
Output

[22]
Program 11:-Write a program to count frequency of characters in
string using dictionary.

str = input("Enter the string: ")


f = {}

for i in str:
if i in f:
f[i] = f[i] + 1
else:
f[i] = 1
print(f)

[23]
Output

[24]
Program 12:-Write a program to print each line of a file in
reverse order.

f1 = open("output8.txt", "w")
withopen("sample.txt", "r") asmyfile:
data = myfile.read()

data_1 = data[::-1]

f1.write(data_1)
f1.close()

[25]
Sample Text

Output

[26]

You might also like