File of Pyhton Removed
File of Pyhton Removed
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
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:
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.2 Repetition
10
4.2 List
list2 = [4, 5, 6]
combined list = list1 + list2
print(combined list)
Output:
4.2.2 Repetition
My list = [1, 2, 3]
11
4.3 Tuple
4.3.2 Repetition
my_tuple = (1, 2, 3)
result = my_tuple * 3
print(result)
Output:
Tuple1 = (0, 1, 2, 3, 2, 3, 1, 3, 2)
12
Output:
4.4 Dictionary
= {'c': 4} result =
dict1.copy()
result.update(dict2)
print(result)
Output:
[my_dict] * 3 print(repeated_dicts)
Output:
13
4.4.3 Get method in dictionary
value = my_dict.get('b')
print(value)
Output:
14
Program No .5
Aim :- Write a program to perform logical and mathematical operations.
return num1
else:
return num2
Output:
15
5.2 Write a program to find factorial of a number.
Sol:-
factorial = 1
if num < 0:
else:
factorial *= i
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:
if len(user_input) == 1:
result = check_vowel_or_consonant(user_input)
else:
17
Output:
rev = 0
total_sum = 0
while n > 0:
a = n % 10
rev = rev * 10 + a
n = n // 10
total_sum += rev % 10
rev = rev // 10
n = int(input("enter n = "))
18
rev_sum(n)
Output:
19
Program No. 6
6.1 Factorial without using recursion function
for i in range(1,n+1):
fact=fact*i
print(fact)
cal_fact(5)
def fact(n):
if n==0 or n==1:
return 1
else:
return n*fact(n-1)
print(fact(4))
20
Output:
return n**p
result = calculate_power(2, 3)
print(result)
Output:
def fibonacci(n):
if n <= 0:
return []
21
elif n == 1:
return [0]
elif n == 2:
return [0, 1]
sequence = [0, 1]
sequence.append(next_term)
return sequence
fib_sequence = fibonacci(num_terms)
if fib_sequence:
Output:
22
Program No.7
Aim :- Write a program to demonstrate linear search in python.
arr=np.array([1,2,3,4,5])
for i in range(len(arr)):
if(arr[i]==s):
break;
if(arr[i]!=s):
Output:
23
Program No.8
Aim :- Write a program to demonstrate binay search in python.
arr_1 = np.array([23,34,55,66,78])
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):
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.
a=[]
for i in range(0,5):
b=int(input(""))
a.append(b)
if a[i]<a[j]:
temp=a[i]
a[i]=a[j]
a[j]=temp
Output:
a=[]
for i in range(0,5):
b=int(input(""))
a.append(b)
if a[i]>a[j]:
27
temp=a[i]
a[i]=a[j]
a[j]=temp
Output:
a=[]
for i in range(0,5):
b=int(input(""))
a.append(b)
28
for i in range (0,5):
if a[j]>a[j+1]:
temp=a[j]
a[j]=a[j+1]
a[j+1]=temp
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_list = birthday_string.split(":")
birthday = birthday_list[1].strip()
print(birthday_dict)
trace_birthday()
Output:
30
31