0% found this document useful (0 votes)
30 views

Python File

The document contains 15 programming problems and their solutions in Python. It provides sample code for tasks like arithmetic operations on integers and floats, checking Armstrong numbers, checking if a triangle is right angled, finding factorials using recursion, finding prime numbers in a range, printing patterns using loops, string operations like concatenation and accessing substrings, and more.

Uploaded by

Karan Wadhwa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Python File

The document contains 15 programming problems and their solutions in Python. It provides sample code for tasks like arithmetic operations on integers and floats, checking Armstrong numbers, checking if a triangle is right angled, finding factorials using recursion, finding prime numbers in a range, printing patterns using loops, string operations like concatenation and accessing substrings, and more.

Uploaded by

Karan Wadhwa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Program -1 Write a program to enter two integers, two floating numbers and then perform all

arithmetic operations on them.

num1 = int(input("Enter first number: "))

num2 = int(input("Enter second number: "))

print("Addition: ",num1 + num2)

print("Subtraction: ",num1 - num2)

print("Multiplication: ",num1 * num2)

print("Division: ",num1 / num2)

print("Floor Division: ",num1 // num2)

print("Modulus: ", num1 % num2)

print("Exponentiation: ",num1 ** num2)

output
Program -2 Write a program to check whether a number is an Armstrong number or not

Code- num = int(input("Enter a number: "))

sum = 0

temp = num

while temp > 0:

digit = temp % 10

sum += digit ** 3

temp //= 10

if num == sum:

print(num,"is an Armstrong number")

else:

print(num,"is not an Armstrong number")

output
Program-3 Write a python program to that accepts length of three sides of a triangle as inputs.
The program should indicate whether or not the triangle is a right angled triangle (use
Pythagorean theorem).

Code- a = int(input("Enter first side : "))

b = int(input("Enter second side : "))

c = int(input("Enter third side : "))

if a + b > c and b + c > a and a + c > b :

print("Triangle Possible")

else :

print("Triangle Not Possible")


output

program 4 Write a python program to find factorial of a number using recursion

code- def recur_factorial(n):

if n == 1:

return n

else:

return n*recur_factorial(n-1)

num = 7

if num < 0:

print("Sorry, factorial does not exist for negative numbers")

elif num == 0:

print("The factorial of 0 is 1")


else:

print("The factorial of", num, "is", recur_factorial(num))

output

program-5 Write a program to print the sum of all the primes between two ranges

code - lower_value = int(input ("Please, Enter the Lowest Range Value: "))

upper_value = int(input ("Please, Enter the Upper Range Value: "))

print ("The Prime Numbers in the range are: ")

for number in range (lower_value, upper_value + 1):

if number > 1:

for i in range (2, number):

if (number % i) == 0:

break

else:

print (number)
output-

program 6- Write a python program to construct the following pattern using nested for loop:

**

***

****

*****

Code- for i in range(10):

print(str(i) * i)
output

program-7 Write a program to swap two strings.

code- print("Enter the First String: ", end="")

string1 = input()

print("Enter the Second String: ", end="")

string2 = input()

print("\nString before swap:")

print("string1 =", string1)

print("string2 =", string2)

temp = string1

string1 = string2

string2 = temp
print("\nString after swap:")

print("string1 =", string1)

print("string2 =", string2)

output--

program-7 Write a Program to Count the Occurrences of Each Word in a Given String Sentence

code- string = "I am programmer. I am student."

word = "am"

words = string.split()

count = 0

for w in words:

if w == word:
count += 1

print(count)

output

program -8 Write a program to create, concatenate and print a string and accessing substring
from a given string.

code - print("Enter the First String: ")

strOne = input()

print("Enter the Second String: ")

strTwo = input()

strThree = strOne + strTwo

print("\nConcatenated String: ", strThree)


output -

program-9 Write a menu driven program to accept two strings from the user and perform the
various functions using user defined functions

program 10- Write a program to find smallest and largest number in a list

code lst = []

num = int(input('How many numbers: '))

for n in range(num):

numbers = int(input('Enter number '))

lst.append(numbers)

print("Maximum element in the list is :", max(lst), "\nMinimum element in the list is :",
min(lst))

output
Program 11 Write a Program to Remove the ith Occurrence of the Given Word in a List

Code a=[]

n= int(input("Enter the number of elements in list:"))

for x in range(0,n):

element=input("Enter element" + str(x+1) + ":")

a.append(element)

print(a)

c=[]

count=0

b=input("Enter word to remove: ")

n=int(input("Enter the occurrence to remove: "))

for i in a:

if(i==b):

count=count+1
if(count!=n):

c.append(i)

else:

c.append(i)

if(count==0):

print("Item not found ")

else:

print("The number of repetitions is: ",count)

print("Updated list is: ",c)

print("The distinct elements are: ",set(a))

output

program 12 Create a dictionary whose keys are month names and whose values are the number
of days in the corresponding months.

Ask the user to enter a month name and use the dictionary to tell them how many

days are in the month.


Print out all keys in the alphabetically order

 Print out all the months with 31 days

 Print out the key value pairs sorted by number of days in each month

Code month = { "jan" : 31 , "feb" : 28 , "march" : 31 , "april" : 30 , "may" : 31 , "june" : 30 ,


"july" : 31 , "aug" : 31 , "sept" : 30 , "oct" : 31 , "nov" : 30 , "dec" : 31}

mon = input("Enter the month name in short form :- ")

print("Number of days in ",mon,"=",month [ mon ])

(ii)

month = { "jan" : 31 , "feb" : 28 , "march" : 31 , "april" : 30 , "may" : 31 , "june" : 30 , "july" : 31


, "aug" : 31 , "sept" : 30 , "oct" : 31 , "nov" : 30 , "dec" : 31}

lst = list ( month . keys() )

lst.sort()

print( lst )

(iii)

month = { "jan" : 31 , "feb" : 28 , "march" : 31 , "april" : 30 , "may" : 31 , "june" : 30 , "july" : 31


, "aug" : 31 , "sept" : 30 , "oct" : 31 , "nov" : 30 , "dec" : 31}

print( "Month which have 31 days !!-- ")

for i in month :

if month [ i ] == 31 :

print( i )

(iv)
month = { "jan" : 31 , "feb" : 28 , "march" : 31 , "april" : 30 , "may" : 31 , "june" : 30 , "july" : 31
, "aug" : 31 , "sept" : 30 , "oct" : 31 , "nov" : 30 , "dec" : 31}

print("Month according to number of days ---")

print("feb")

for i in month :

if month [ i ] == 30 :

print(i)

for i in month :

if month [ i ] == 31 :

print( i)

output

program 13 Make a list of first 10 letters of the alphabet, then use the slicing to do the following
operations:

 Print the first 3 letters of the list


 Print any 3 letters from the middle

 Print the letter from any particular index to the end of the list

Code import string

# initializing empty list

test_list = []

# using string for filling alphabets

test_list = list(string.ascii_uppercase)

# printing resultant list

print ("List after insertion : " + str(test_list))

output
program 14 – Write a program that scans an email address and forms a tuple of user name and
domain.

code - test_str = '[email protected]'

# printing original string

print("The original string is : " + str(test_str))

# slicing domain name using slicing

res = test_str[test_str.index('@') + 1 : ]

# printing result

print("The extracted domain name : " + str(res))

output

program -15 Write a program that uses a user defined function that accepts name and gender (as
M for Male, F for Female) and prefixes Mr./Ms. on the basis of the gender.

Code
def prefix(name,gender):

if (gender == 'M' or gender == 'm'):

print("Hello, Mr.",name)

elif (gender == 'F' or gender == 'f'):

print("Hello, Ms.",name)

else:

print("Please enter only M or F in gender")

name = input("Enter your name: "

gender = input("Enter your gender: M for Male, and F for Female: ")

prefix(name,gender)

output

You might also like