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

Python Questions With Solutions

The document contains 10 programming problems with solutions in Python. The problems cover a range of concepts including: merging two lists into a dictionary with one as keys and the other as values, defining a class to perform basic math operations, using list comprehension to print a list, calculating age from a birthdate, converting a string to a datetime object, printing even and odd numbers within a range, finding prime numbers within a range, checking if a number is an Armstrong number, and counting the number of pairs that can be made from numbers whose addition is less than or equal to a given number.

Uploaded by

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

Python Questions With Solutions

The document contains 10 programming problems with solutions in Python. The problems cover a range of concepts including: merging two lists into a dictionary with one as keys and the other as values, defining a class to perform basic math operations, using list comprehension to print a list, calculating age from a birthdate, converting a string to a datetime object, printing even and odd numbers within a range, finding prime numbers within a range, checking if a number is an Armstrong number, and counting the number of pairs that can be made from numbers whose addition is less than or equal to a given number.

Uploaded by

Ritik
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Write a program to merge two list in a dictionary, where content of one list is
key and content of another list is value.

d={}
l1=[1,2,3]
l2=['TCS', 'S2', 'Pune']
len1=len(l1)
len2=len(l2)
if len1==len2:
for i in range(len1):
d[l1[i]]=l2[i] #OR d.update({l1[i]:l2[i]})

print(d)

2. Write a class in python with methods which performs addition,


subtraction,multiplication and division.(take user input,use exception handling if
required)

num1 = int(input("Enter First Number: "))


num2 = int(input("Enter Second Number: "))

print("Enter which operation would you like to perform?")


ch = input("Enter any of these char for specific operation +,-,*,/: ")

result = 0
if ch == '+':
result = num1 + num2
elif ch == '-':
result = num1 - num2
elif ch == '*':
result = num1 * num2
elif ch == '/':
result = num1 / num2
else:
print("Input character is not recognized!")

print(num1, ch , num2, ":", result)

3. Print content of list using list comprehension.

l=[]
size=int(input("Enter the size of the list: "))
for i in range(size):
n=input(f"Enter whatever you want at position {i} : ")
l.append(n)

ans=[print(i) for i in l]

4. Calculate age when birth date is given.

from datetime import date

def calculateAge(birthDate):
days_in_year = 365.2425
age = int((date.today() - birthDate).days / days_in_year)
return age
d=int(input("Enter your day number of birth: "))
m=int(input("Enter your month number of your birth(1-12): "))
y=int(input("Enter your year number of your birth: "))
print(calculateAge(date(y, m, d)), "years")

5. Convert a '20190422' to date time with format 2019-04-22.

import datetime

input = '20190422'
format = '%Y%m%d'
datetime = datetime.datetime.strptime(input, format)
print(datetime.date())

7. Write a function called showNumbers that takes a parameter called limit. It


should print all the numbers between 0 and limit with a label to identify the even
and odd numbers. For example, if the limit is 3, it should print:

i=1
def showNumbers(n):
for i in range(1,n+1):
if i%2 == 0:
print(i,"Even")
else:
print(i,"Odd")

limit=int(input("Enter the limit in number: "))


showNumbers(limit)

8. Write a function that prints all the prime numbers between 0 and limit where
limit is a parameter.

def prime(n):
for Number in range (1,n+1):
count = 0
for i in range(2, (Number//2 + 1)):
if(Number % i == 0):
count = count + 1
break

if (count == 0 and Number != 1):


print(" %d" %Number, end = ' ')

limit=int(input("Enter the limit in number to print prime number: "))


prime(limit)

9. "Check whether the given number is an Armstrong number or not. It will take
integer input and return String output
Sample input : 370
Sample output :The given number is an Armstrong number."

sum=0
count=0
n=int(input("Enter the number to check i.e. Armstrong or not: "))
t=n
while t>0:
t=t//10
count=count+1

t=n
while t>0:
r=t%10
sum=sum+(r*r*r)
t=t//10

if(sum==n):
print(n," it is a Armstrong Number.")
else:
print(n," it is not a Armstrong Number.")

10. "We are given a number, suppose 3. Then we have to find the number of pairs
which can be made using numbers whose addition is less than equal to 3.
Sample input : 3
Then , the pairs which can be made
(0,0),(0,1), (1,0), (1,1), (1,2), (2,1)
Sample output : 6"

count=0
n=int(input("Enter the number to find the count of the pairs: "))
for i in range(n):
for j in range(n):
count=count+1
print(count)

You might also like