Python Questions With Solutions
Python Questions With Solutions
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)
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!")
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]
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")
import datetime
input = '20190422'
format = '%Y%m%d'
datetime = datetime.datetime.strptime(input, format)
print(datetime.date())
i=1
def showNumbers(n):
for i in range(1,n+1):
if i%2 == 0:
print(i,"Even")
else:
print(i,"Odd")
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
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)