Screenshot 2023-08-27 at 4.00.34 PM

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

1.

Write a python program to print the


following pattern.
1

32
654
198 7

CODING:

OUTPUT:
Write a program to generate prime
1.

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:

~IDLEShell3.10.5
FileEditShellDebugOptionsWindowHelp
Python3.10.5(tags/v3.10.5:377153,Jun62022,16:14:13)[MSCv.192964bit(AMD64)]onwin32
Type"help","copyright","credits"or"license()"formoreinformation.
>>>
RESTART:C:/Users/Admin/AppData/Local/Programs/Python/Python310/02.py
Enteranumber:5
5isaprimenumber
>>>

Write a python program to count the


2.

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:

Write a python program to count and


3.

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:

Write a python program to swap each


4.

character of a given string from lowercase to


uppercase and vice versa.
CODING:
name = “nEEtIsha kApiL"
print(name.swapcase())
OUTPUT:
5.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:
6.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:
7. 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:

WAP for insertion


8.

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:

Rahul and Aryan both are friends they want to


9.

display addition of age subtraction of fights multiplier and


division of IQ .Description as follows :

Rahul Descrip9on

Age 32

Height 175 cm

Weight 75kg

IQ 100

Aryan Descrip9on
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:
Write definition of a method ZeroEnding(scores)
10.

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:

Write a user – defined function which take


11.

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