Screenshot 2023-08-27 at 3.53.16 PM
Screenshot 2023-08-27 at 3.53.16 PM
Screenshot 2023-08-27 at 3.53.16 PM
CODING:
OUTPUT:
2. Write a program to generate prime
numbers up to N. Where N is a user
input more than 3. A wrong input
should display proper error message.
CODING:
num = 29
num = int(input("Enter a number: "))
flag = False
if num == 1:
print(num, "is not a prime number")
elif num > 1:
flag = True
break
if flag:
print(num, "is not a prime number")
else:
print(num, "is a prime number")
OUTPUT:
3.Write a python program to count the
number of characters (character
frequency) in a string.
CODING:
def character_frequency(string):
for i in string:
count = 0
print(i,":", count, end = ", ")
count += 1
return count
print(character_frequency(“helloworld“))
OUTPUT:
4.Write a python program to count and
display the vowels of a given text.
CODING:
string=raw_input("Enter string:")
vowels=0
for i in string:
if(i=='a' or i=='e' or i=='i' or i=='o' or
i=='u' or i=='A' or i=='E' or i=='I' or i=='O'
or i=='U'):
vowels=vowels+1
print("Number of vowels are:")
print(vowels)
OUTPUT:
5.Write a python program to swap each
character of a given string from lowercase
to uppercase and vice versa.
CODING:
name = “hArshIta hiRa"
print(name.swapcase())
OUTPUT:
6. Write a python program to capitalize
first and last letters of each word of a
given string .
CODING:
st = input("Enter a sentence or string : ")
for word in st.split():
print(word[0].upper()+word[1:-
1]+word[-1].upper(), sep=' ', end=' ')
OUTPUT:
7.Write a python function that takes a
list of words and returns the length of
the longest one.
CODING:
def find_longest_word(words_list):
word_len = []
for n in words_list:
word_len.append((len(n), n))
word_len.sort()
return word_len[-1][0], word_len[-1][1]
result = find_longest_word(["PHP",
"Exercises","Backend"])
print("\nLongest word: ",result[1])
print("Length of the longest word: ",result[0])
OUTPUT:
8.WAP for bubble sort.
CODING:
def bubbleSort(arr):
n = len(arr)
swapped = False
for i in range(n-1):
for j in range(0, n-i-1):
if arr[j] > arr[j + 1]:
swapped = True
arr[j], arr[j + 1] = arr[j + 1], arr[j]
if not swapped:
return
OUTPUT:
9.WAP for insertion
sort.
CODING:
def insertionSort(arr):
if (n := len(arr)) <= 1:
return
for i in range(1, n):
key = arr[i]
j = i-1
while j >=0 and key < arr[j] :
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
Rahul Description
Age 32
Height 175 cm
Weight 75kg
IQ 100
Aryan Description
Age 25
Height 165 cm
Weight 75kg
IQ 50
def mul_Num(a,b):
multiply=a*b;
return multiply;
num1=100
num2=50
print("multiplication of IQ
is",mul_Num(num1,num2))
def div(a,b):
return a/b
print("Division of IQ:",div(100,50))
def subtraction(x,y):
sub=x-y
return sub
num1=int(input("please enter first
number: "))
num2=int(input("please enter
second number: "))
print("Subtraction of heights is :
",subtraction(num1,num2))
OUTPUT:
11.Write definition of a method ZeroEnding(scores)
to add all those values in the list of scores, which are
ending with zero(0) and display the sum.
For example,
If the scores contain [200, 456,300,100,234,678]
The sum should e displayed as 600
CODING:
def zero_ending(scores):
total = 0
for number in scores:
if number%10 == 0:
total += number
return total
CODING:
def sumDigit(string):
sum = 0
for a in string:
#Checking if a character is digit and adding it
if(a.isdigit()):
sum += int(a)
return sum
userInput =( input("Enter any string with digits: "))