0% found this document useful (0 votes)
6 views12 pages

Harjot 8 - 18

Uploaded by

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

Harjot 8 - 18

Uploaded by

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

Harjot Singh 09121102023 BCA III E2

Practical No 8
Aim: Write a program to print the elements of a list using for loop.
Solution:

thislist = ["apple", "banana", "cherry"]


for x in thislist:
print(x)

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:

print("Element not found")

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";

print("Strings before swapping: " + str1 + " " + str2);

str1 = str1 + str2;

str2 = str1[0 : (len(str1) - len(str2))];

str1 = str1[len(str2):];

print("Strings after swapping: " + str1 + " " + 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())

# lower() function to convert

# string to lower case

print("\nConverted String:")

print(text.lower())

# converts the first character to

# upper case and rest to lower case

print("\nConverted String:")

print(text.title())

# swaps the case of all characters in the string

# upper case character to lowercase and viceversa

print("\nConverted String:")

print(text.swapcase())

# convert the first character of a string to uppercase

print("\nConverted String:")

print(text.capitalize())

# original string never changes

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"

str3 = str1 + str2

print("The new combined string is:",str3)

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:

You might also like