Harjot 8 - 18
Harjot 8 - 18
Practical No 8
Aim: Write a program to print the elements of a list using for loop.
Solution:
Output:
Harjot Singh 09121102023 BCA III E2
Practical No 9
Aim: Write a program to search for a number in a tuple using for loop.
Solution:
A = (1, 2, 3, 4, 6)
isFound = False
elem = 5
for item in A:
if item == elem:
isFound = True
break
if isFound:
print("Element found")
else:
Output:
Harjot Singh 09121102023 BCA III E2
Practical No 10
Aim: Write a program to find the sum of numbers in a given range.
Solution:
num1, num2 = 3, 6
sum = 0
for i in range(num1,num2+1):
sum+=i
print(sum)
Output:
Harjot Singh 09121102023 BCA III E2
Practical No 11
Aim: Write a program to swap two strings.
Solution:
str1 = "Good";
str2 = "morning";
str1 = str1[len(str2):];
Output:
Harjot Singh 09121102023 BCA III E2
Practical No 12
Aim: Write a program to demonstrate all the string functions using suitable examples.
Solution:
text = 'Hellow World'
print("\nConverted String:")
print(text.upper())
print("\nConverted String:")
print(text.lower())
print("\nConverted String:")
print(text.title())
print("\nConverted String:")
print(text.swapcase())
print("\nConverted String:")
print(text.capitalize())
print("\nOriginal String")
print(text)
Harjot Singh 09121102023 BCA III E2
Output:
Harjot Singh 09121102023 BCA III E2
Practical No 13
Aim: Write a program to create, concatenate and print a string and accessing substring from a given
string.
Solution:
str1 = "Hello "
str2 = "World"
Output:
Harjot Singh 09121102023 BCA III E2
Practical No 14
Aim: Write a program to print square of positive numbers.
Solution:
n=4
square = n * n
print("Square is:",square)
Output:
Harjot Singh 09121102023 BCA III E2
Practical No 15
Aim: Write a program to print Fibonacci series for first 15 numbers using recursive approach.
Solution:
def fib(n):
if n <= 1:
return n
else:
return(fib(n-1) + fib(n-2))
nterms = 15
print("Fibonacci sequence:")
for i in range(nterms):
print(fib(i))
Output:
Harjot Singh 09121102023 BCA III E2
Practical No 16
Aim: Write a program that creates a list from numbers 1 to 20 that are either divisible by 2 or 4.
Solution:
my_list=[]
for i in range(21):
if (i % 2 == 0) or (i % 4 == 0):
my_list.append(i)
print(my_list)
Output:
my_list=[]
for i in range(21):
if (i % 2 == 0) or (i % 4 == 0):
my_list.append(i)
print(my_list)
Output:
Harjot Singh 09121102023 BCA III E2
Practical No 17
Aim: Write a program that defines a list of countries that are a member of BRICS. Check whether a
country is member of BRICS or not.
Solution:
countries = ["Brazil", "Russia", "India", "China", "South Africa", "Pakistan", "Japan"]
brics = ["Brazil", "Russia", "India", "China", "South Africa"]
for country in brics:
print(country)
Output:
Harjot Singh 09121102023 BCA III E2
Practical No 18
Aim: Write a program to print index at which a particular value exists if the value exists at multiple
locations in the list, then print all the indices also count the number of times that value is repeated in
the list.
Solution:
list1=[1,2,5,7,3,6,23,1,4,43,6,56,1]
n=int(input("Enter the value to find in the list :"))
index=0
count=0
while index<len(list1):
if list1[index]==n:
print(n," found at index",index)
count+=1
index+=1
print("Value is repeated ",count," times in the list")
Output: