0% found this document useful (0 votes)
15 views31 pages

File of Pyhton Removed

The document is a practical file for a Python lab course in a Bachelor of Technology program, detailing various programming exercises. It includes programs demonstrating basic data types, arithmetic operations, string manipulation, data structures like lists and dictionaries, and algorithms like sorting and searching. Each program is accompanied by a description of its aim, code solutions, and expected outputs.

Uploaded by

Mukul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views31 pages

File of Pyhton Removed

The document is a practical file for a Python lab course in a Bachelor of Technology program, detailing various programming exercises. It includes programs demonstrating basic data types, arithmetic operations, string manipulation, data structures like lists and dictionaries, and algorithms like sorting and searching. Each program is accompanied by a description of its aim, code solutions, and expected outputs.

Uploaded by

Mukul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

PRACTICAL FILE

OF
PYTHON LAB
(PC- CS-AIML-217LA)
Bachelor of Technology
In
Computer Science and Engineering
(AI&ML)

Submitted To - Submitted By –
Dr. Rama Chawla Mahesh
Assistant Professor 82545503
Computer Engineering 3rd Semester

DEPARTMENT OF COMPUTER ENGINEERING


STATE INSTITUTE OF ENGINEERING & TECHNOLOGIES,
NILOKHERI
(AFFILIATED TOKURUKSHETRA UNIVERSITY
KURUKSHETRA)
(2023 - 2027)
Program No.1
Aim :- Write a program to demonstrate basic data types in python.
1.1 Write a program to find sum of two numbers
Sol:- a=int(input("Enter First Number:"))
b=int(input("Enter Second Number:"))
print("Sum of ",a,"and ",b,"is: ",a+b)
Output:

1.2 Write a program to check weather a given number is greater or


smaller
Sol: a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
print(a>=b)
Output:

1.3 Write a program to find Average of Two numbers


Sol: a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
3
print("Average of two numbers: ",(a+b)/2)
Output:

1.4 WAP to find the Area of Rectangle


Sol: l=int(input("Enter length of rectangle:"))
b=int(input("Enter breadth of rectangle:"))
print("Area of rectangle: ",l*b)
Output:

1.5 WAP to find the difference between the ASCII code of any letter
Sol: ch1=input("Enter first character: ")
ch2=input("Enter second character: ")
print(ch1,ord(ch1))
print(ch2,ord(ch2))
print("The difference between the ASCII value is:")
print(ord(ch1),'-',ord(ch2),'=',end=' ')
print(ord(ch1)-ord(ch2))

4
Output:

5
Program No. 2
Aim :- Write a program to implement
2.1 Write a program to implement addition
Sol:- sum1=int(input(“Enter First number: “))
sum2=int(input(“Enter Second number: “))
print(“Addition of two nos.=”,sum1+sum2)
Output:

2.2 Write a program to implement subtraction


Sol:- sub1=int(input(“Enter First number: “))
sub2=int(input(“Enter Second number: “))
print(“Subtraction of two nos.=”,sub1-sub2)
Output:

2.3 Write a program to implement multiplication


Sol:- a=int(input(“Enter First number: “))
b=int(input(“Enter Second number: “))
print(“Multiplication of two nos.=”,a*b)
6
Output:

2.4 Write a program to implement division


Sol:- a=int(input(“Enter First number: “))
b=int(input(“Enter Second number:“))
print(“Division of two nos.=”,a/b)
Output:

2.5 Write a program to implement AND operator


Sol:- a=int(input("Enter First number: "))
b=int(input("Enter Second number: "))
c=int(input("Enter Third number: ")) print(a<b and b>c)
Output:

2.6 Write a program to implement OR operator


Sol:- a=int(input("Enter First number: "))
7
b=int(input("Enter Second number: "))
c=int(input("Enter Third number: ")) print(a<b or b>c)
Output:

2.7 Write a program to implement NOT operator


Sol:- a=int(input("Enter First number: "))
b=int(input("Enter Second number: "))
c=int(input("Enter Third number: ")) print(not a<b and b>c)
Output:

8
Program No. 3
AIM: Write a program for checking whether the given number is an
even number or not.

number = int(input("Enter a
number:")) if number % 2 == 0:
print("The number is even.")
else:
print("The number is not
even.")
Output:

9
Program No. 4
Aim: Write a program to demonsatrate strings,tuple and
dictionaries in python.

4.1 String

4.1.1 String concatenation


string1 = “Hello,”
string2=“world!” string3“How
are you?”
Result=string1+string2+string3
Print(result)
Output:

4.1.2 Repetition

String to repeat = input("Enter the string t-repeat: ")


repetitions = int(input("Enter the number of
repetitions: "))
result = ""
for _ in range(repetitions):
result += string to repeat
print(result)
Output:

10
4.2 List

4.2.1 Combine two list list1 = [1, 2, 3]

list2 = [4, 5, 6]
combined list = list1 + list2
print(combined list)
Output:

4.2.2 Repetition

My list = [1, 2, 3]

Repeated list = my list * 3 print(repeated list)


Output:

4.2.3 Insert an element at last position in list


list:List=[1,2,3]
print(List) list.append(5)
print(List)
Output:

11
4.3 Tuple

4.3.1 Adding two tuple


tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
result = tuple1 + tuple2
print(result)
Output:

4.3.2 Repetition
my_tuple = (1, 2, 3)
result = my_tuple * 3
print(result)
Output:

4.3.3 Tuple count method

Tuple1 = (0, 1, 2, 3, 2, 3, 1, 3, 2)

Tuple2 = ('python', 'geek', 'python',

'for', 'java', 'python')

res = Tuple1.count(3) print('Count of 3


in Tuple1 is:', res) res =
Tuple2.count('python') print('Count of
Python in Tuple2 is:',res)

12
Output:

4.4 Dictionary

4.4.1 Update method in dictionary

dict1 = {'a': 1, 'b': 2} dict2

= {'c': 4} result =

dict1.copy()

result.update(dict2)

print(result)

Output:

4.4.2 Repetition method in dictionary

my_dict = {'a': 1, 'b': 2} repeated_dicts =

[my_dict] * 3 print(repeated_dicts)

Output:

13
4.4.3 Get method in dictionary

my_dict = {'a': 1, 'b': 2, 'c': 3}

value = my_dict.get('b')

print(value)

Output:

14
Program No .5
Aim :- Write a program to perform logical and mathematical operations.

5.1 Write a program to find maximum of two numbers.

Sol:- def max of two numbers(num1, num2):

if num1 > num2:

return num1

else:

return num2

number1 = float(input("Enter the first number: "))

number2 = float(input("Enter the second number: "))

max number = max of two numbers (number1, number2)

print("The maximum number is:", max number)

Output:

15
5.2 Write a program to find factorial of a number.

Sol:-

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

factorial = 1

if num < 0:

print("Factorial is not defined for negative numbers.")

elif num == 0 or num == 1:

print(f"The factorial of {num} is 1.")

else:

for i in range(2, num + 1):

factorial *= i

print(f"The factorial of {num} is {factorial}.")

Output:

16
5.3 Write a program to find whether an alphabet is vowel or consonant.

Sol:-

def check_vowel_or_consonant(letter):

letter = letter.lower()

if letter in 'aeiou':

return "Vowel"

elif letter.isalpha():

return "Consonant"

else:

return "Invalid input"

user_input = input("Enter a letter: ")

if len(user_input) == 1:

result = check_vowel_or_consonant(user_input)

print(f"The letter '{user_input}' is a {result}.")

else:

print("Please enter only one letter.")

17
Output:

5.4 Write a program to find the sum of the reversed number .

Sol:- def rev_sum(n):

rev = 0

total_sum = 0

while n > 0:

a = n % 10

rev = rev * 10 + a

n = n // 10

print("rev =", rev)

while rev > 0:

total_sum += rev % 10

rev = rev // 10

print("sum =", total_sum)

n = int(input("enter n = "))

18
rev_sum(n)

Output:

19
Program No. 6
6.1 Factorial without using recursion function

def cal_fact(n): fact=1

for i in range(1,n+1):

fact=fact*i

print(fact)

cal_fact(5)

6.2 Factorial with recursion function

def fact(n):

if n==0 or n==1:

return 1

else:

return n*fact(n-1)

print(fact(4))
20
Output:

6.3 Using function define the power

def calculate_power(n, p):

return n**p

result = calculate_power(2, 3)

print(result)

Output:

6.4 Fibanocci with recursion function

def fibonacci(n):

if n <= 0:

print("Please enter a positive integer.")

return []

21
elif n == 1:

return [0]

elif n == 2:

return [0, 1]

sequence = [0, 1]

for i in range(2, n):

next_term = sequence[-1] + sequence[-2]

sequence.append(next_term)

return sequence

num_terms = int(input("Enter the number of terms: "))

fib_sequence = fibonacci(num_terms)

if fib_sequence:

print(f"Fibonacci sequence with {num_terms} terms: {fib_sequence}")

Output:

22
Program No.7
Aim :- Write a program to demonstrate linear search in python.

Sol: import numpy as np

arr=np.array([1,2,3,4,5])

s=int(input("Enter the element: "))

print("searched element =",s)

for i in range(len(arr)):

if(arr[i]==s):

print(f"element found at location {i}")

break;

if(arr[i]!=s):

print("element not found")

Output:

23
Program No.8
Aim :- Write a program to demonstrate binay search in python.

Sol: import numpy as np

arr_1 = np.array([23,34,55,66,78])

s = int(input("Enter the element = "))

print("Searched element = ",s)

First = 0

last = len(arr_1)-1

while(first<=last):

mid = (first+last)//2

if(arr_1[mid]<s):

first = mid+1

elif(arr_1[mid]==s):

print("element found at location = ",mid+1)

break;

else:

last = mid-1

if (first>last):
24
print("not found ")

Output:

25
Program No.9
Aim: Write a program to perform insertion sort, selection sort, bubble sort.

9.1 Write a program to perform insertion sort.

a=[]

for i in range(0,5):

b=int(input(""))

a.append(b)

print("Array before sorting:")

for i in range (0,5):

print(a[i] , end=" ")

for i in range (0,5):

for j in range (0,i):

if a[i]<a[j]:

temp=a[i]

a[i]=a[j]

a[j]=temp

print("\narray after soting:")

for i in range (0,5):


26
print(a[i] , end=" ")

Output:

9.2 Write a program to perform selection sort.

a=[]

for i in range(0,5):

b=int(input(""))

a.append(b)

print("Array before sorting:")

for i in range (0,5):

print(a[i] , end=" ")

for i in range (0,5):

for j in range (i,5):

if a[i]>a[j]:

27
temp=a[i]

a[i]=a[j]

a[j]=temp

print("\narray after soting:")

for i in range (0,5):

print(a[i] , end=" ")

Output:

9.3 Write a program to perform bubble sort.

a=[]

for i in range(0,5):

b=int(input(""))

a.append(b)

print("Array before sorting:")

28
for i in range (0,5):

print(a[i] , end=" ")

for i in range (0,5):

for j in range (0,4):

if a[j]>a[j+1]:

temp=a[j]

a[j]=a[j+1]

a[j+1]=temp

print("\narray after soting:")

for i in range (0,5):

print(a[i] , end=" ")

Output:

29
Program No. 10
Aim: write a python program to use split and join methods in the string
and trace a birthday of a person with dictionary data structure .

Source code:

def trace_birthday():

birthday_string = "John: 01/01/2000"

birthday_list = birthday_string.split(":")

name = birthday_list[0].strip() # Remove any extra whitespace

birthday = birthday_list[1].strip()

birthday_dict = {name: birthday}

print(birthday_dict)

trace_birthday()

Output:

30
31

You might also like