Screenshot 2023-08-27 at 3.53.16 PM

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

1.

Write a python program to print the


following pattern.
1
3 2
6 5 4
10 9 8 7

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:

for i in range(2, num):


if (num % i) == 0:

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

arr = [12, 11, 13, 5, 6]


insertionSort(arr)
print(arr)
OUTPUT:
10. Rahul and Aryan both are friends they want to
display addition of age subtraction of fights
multiplier and division of IQ .Description as follows :

Rahul Description

Age 32

Height 175 cm

Weight 75kg

IQ 100

Aryan Description

Age 25

Height 165 cm

Weight 75kg

IQ 50

Firstly, Define the function add(), sub(), mul() and div()


CODING:
def add_num(a,b):
sum=a+b;
return sum;
num1=32
num2=25
print("The addition of age
is",add_num(num1,num2))

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

scores = [200, 456, 300, 100, 234, 678]


s = zero_ending(scores)
print(s)
OUTPUT:
12. Write a user – defined function which take
string as a parameter and check whether the string
contains digit or not if it contains digit then
calculate the sum of digits and print the digit and
print the original string. If it is not contained digit
then it give original string with appropriate
message, “No digit is present “.

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: "))

#Calling the sumDigit function


result = sumDigit(userInput)

#Printing the sum of all digits


print("The sum of all digits in the string '",userInput,"'
is:",result)
OUTPUT:

You might also like