Python Interview Questions and Answers With Code # Python

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

axkrhgue6

February 18, 2023

1 Name:- PUSHPRAJ SINGH RATHORE


TOPIC:- Python program beginner Level Interview Question With Solutions

2 add two number


[1]: num1=35
num2=45
print("the sum of two number is",num1+num2)

the sum of two number is 80

[9]: # user input


l1=int(input("enter l1 number"))
l2=int(input("enter l2 number"))
print("the sum of l1 and l2=",l1+l2)

enter l1 number45
enter l2 number35
the sum of l1 and l2= 80

[10]: l1=float(input("enter l1 number"))


l2=float(input("enter l2 number"))
print("the sum of l1 and l2=",l1+l2)

enter l1 number35.6
enter l2 number45.6
the sum of l1 and l2= 81.2

3 find and calculate SquareRoot program


[18]: num=64
square=num**(1/2)# formula of square
print("the square of num is :",square)

the square of num is : 8.0

1
[19]: l1=int(input("enter a number"))
sr=l1**(1/2)## ** use for power of number
print("the square of number",sr)

enter a number49
the square of number 7.0

[23]: ### using maths module


import math
l1=float(input("enter a number"))
sr=math.sqrt(l1) ## inbulit formula in python module
print("square root of l1:",sr)

enter a number64.5
square root of l1: 8.031189202104505

4 claculate the area of triangle


[2]: height=float(input("Enter the area of height"))
base=float(input("Enter the area of base "))
area=(1/2)*height*base
print("area of triangle:",area)

Enter the area of height12


Enter the area of base 12
area of triangle: 72.0

5 swap two vairable


[19]: x=12
y=13
x,y=y,x
print(x,y)

13 12

[3]: # using third variable


x=13
y=12
temp=13
print(temp)
x=y
print(x)
y=temp
print(y)

2
13
12
13

6 kilometer to miles
[1]: # 1km = 0.621371 miles
l1=int(input("enter a km:"))
miles=l1*0.621371
print("km to miles",miles)

enter a km:10
km to miles 6.21371

7 chek number is postive, negative ,zero


[10]: l1=int(input("enter a number"))
if l1>0:
print("postive")
elif l1<0:
print("negative")
else:
print("zero")

enter a number0
zero

[15]: l1=int(input("enter a number"))


if l1>0:
print("postive")
elif l1== 0:
print("zero")
else:
print("negative")

enter a number-6
negative

8 chek year leap year


year=365 leap year=366 – once in 4 year condication of leap year only for centure ex-2000,3000,4000,
year%400==0 and year%100==0 non centure year ex= 1865,1996,1940,1996,2008,2020
year%4==0 and year%100!=0
[43]: year = int(input("enter a year"))

3
if(year%400 == 0) and (year%100==0):
print(year, "is leap year")

elif(year%4 == 0) and (year%100!=0):## non centure year


print(year, "is leap year")

else:
print(year,"is not leap year")

enter a year1957
1957 is not leap year

9 Find the largest among three number


requerment 1= variable 2=condication if,else elif etc 3=opratoroes and , or ,not
[6]: num1=34
num2=25
num3=47
if num1>num2 and num1>num3:
print("num1 grater than both num2 and num3")
elif num2>num1 and num2>num3:
print("num largest number both")
else:
print("num 3 largest")

num 3 largest

[2]: num1=(int(input("Enter a number 1::")))


num2=(int(input("Enter a number 2::")))
num3=(int(input("Enter a number 3::")))
if num1>num2 and num1>num3:
print(num1, "grater than both num2 and num3")
elif num2>num1 and num2>num3:
print(num2,"largest number both")
else:
print(num3, "largest")

Enter a number 1::12


Enter a number 2::134
Enter a number 3::56
134 largest number both

10 chek number is prime or not


prime number 1 3 5 7 11 37 13 etc

4
[7]: num=int(input("Enter the number"))
if num==1:
print("it is prime number")
if num>1:
for i in range(2,num):
if num % i==0:
print("it is not prime number")
break
else:
print("prime number")

Enter the number7


prime number

[10]: n=int(input("enter anumber"))


if n%1==0:
print("not prime number")
elif n>1:
for i in range(2,num):
if i%2==0:
print(" not prime number")
else:
print("Prime number")

enter anumber9
not prime number

11 Print all prime number in interval


[11]: lower=int(input("Enter a lower number"))
upper=int(input("Enter a upper number"))

for num in range(lower,upper+1):


if num >1:
for i in range(2,num):
if num%i==0:
break
else:
print(num,"prime number")

Enter a lower number7


Enter a upper number37
7 prime number
11 prime number
13 prime number
17 prime number
19 prime number

5
23 prime number
29 prime number
31 prime number
37 prime number

12 Genrate random Number


random module use to genrate random number
[9]: import random
num=random.randint(0,10)
print(num)

[22]: # like dice game


import random
dice=random.randint(1,6)
dice

[22]: 6

13 convert to celcius to fahrenheit


[77]: temp=int(input("Enter the temprature"))
fehrenheit=temp*1.8+32
print(fehrenheit,"convert value in Fehrenheit")

Enter the temprature55


131.0 convert value in Fehrenheit

14 fahrenheit to celcius
[16]: fer=int(input("Enter the fahernheit="))
celcius=((fer-32)*5)/9
print(celcius," celcius")
# celcius=((fr-32)*5)/9

Enter the fahernheit=55


12.777777777777779 celcius

[1]: # QR code ganrate


import qrcode as qr
img=qr.make("https://www.youtube.com/")
img.save("youtube")

6
15 Find the factorial of number
like 5!=54321=120 4!=432*1=24 hint factorial me negative number nahi hote negative number ka
factorial nahi nikala jata 0!=1
[1]: num=int(input("Enter a number"))

fact=1
# condicatons we use for factorial
if num < 0:
print("factorial of less than 0 doest not exist")

if num==0:
print("factorail of 0 is ",1)
if num>0:
for i in range(1,num+1):
fact=fact*i
print("the factorial of hiven number is",fact)

Enter a number5
the factorial of hiven number is 120

[12]:

enter a number5
1

[3]: # using Recursion find factorial


#with help of call the fucation itself

def fact(a):
if a==0:
return 1
else:
return((a)*fact(a-1))

num=int(input("enter a factorial here "))


result=fact(num)
print("the factorial of given number",result)

enter a factorial here 5


the factorial of given number 120

7
16 python program to multipication of table
[2]: n=int(input("Enter a number"))
for i in range(1,11):
print(n,"x",i,"=",n*i)

Enter a number5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

[1]: n=7
i=1
while i<=10:
print(n,"x",i,"=",n*i)
i=i+1

7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70

17 print Fibonacci sequence


fibonacci seaquence is like 0,1,2,3,4 so 0+1=1,1+1=2,2+2=4,4+3=7,7+4=11 this type of seaquace
pattern is fibonacci seaquace
[59]: # a and b are alredy defind
a=0
b=1
num=int(input("enter a number to obatain fibonacci sequence:"))

if num==1:
print(a)

8
else:
print(a)
print(b)
for i in range(1,num):
c=a+b
a=b
b=c
print(c)

enter a number to obatain fibonacci sequence:7


0
1
1
2
3
5
8
13

18 program to chek armstrongs number


like 153 (111)+(555)+(333) 1+125+27=153 this amstrong number like 53 55+33 25+9=34 this is
not amstrong number
[1]: num = int(input("Enter a number: "))

# initialize sum
sum = 0

# find the sum of the cube of each digit


temp = num # temp=tempraorey
while temp > 0:
digit = temp % 10 # list digit store kr na he digit ke andr lik 153 so 3␣
↪store krna he

cube = digit ** 3
sum=sum+cube

temp //= 10

# display the result


if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")

Enter a number: 153


153 is an Armstrong number

9
[13]:

Enter a number: 153


num

19 nth number to chek amstong


[8]: num = int(input("Enter a number: "))
order=len(str(num))

# initialize sum
sum = 0

# find the sum of the cube of each digit


temp = num
while temp > 0:
digit = temp % 10 # list digit store kr na he digit ke andr lik 153 so 3␣
↪store krna he

sum += digit ** order


temp //= 10

# display the result


if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")

Enter a number: 1569


1569 is not an Armstrong number

20 find the sum of natural naumber


[ ]: # natural number all postive number

[8]: n=int(input("Enter a number"))


if n<0:
print("plse enter a postive number ")
else:
sum=0 #5 +4 +3+2+1=15
while n>0:# 5 4 3 2 1
sum+=n
n-=1
print(sum)

Enter a number5
15

10
Python Program to Display Powers of 2 Using Anonymous Function
** Anonymous Funaction** lamda funcation is Anonymous Funcation Becacuse its temporary
funaction means Isko alag se difine kr ne ki jarurat nahi padti hamre satament ke sath he work
karta he kam memory ka use karta he
Map fuanction particualr ” srting ke lenth count find kr tah he aur usko list formate print kr deta
he”
[9]: n=int(input("enter a number"))
result=list(map(lambda x:2**x,range(n+1))) # n=5 # 5 4 3 2 1
print(result)

enter a number5
[1, 2, 4, 8, 16, 32]

[25]: n=2
for i in range(0,5+1):
k=(n**i)
print([k],end="")

[1][2][4][8][16][32]

[28]: n=int(input("enter a number"))


result=list(map(lambda x:2**x,range(n+1))) # n+1=5 # 5 4 3 2 1
print(result)

for i in range(n+1):

print("the value of 2 raised to powe",i,"is",result[i])

enter a number5
[1, 2, 4, 8, 16, 32]
the value of 2 raised to powe 0 is 1
the value of 2 raised to powe 1 is 2
the value of 2 raised to powe 2 is 4
the value of 2 raised to powe 3 is 8
the value of 2 raised to powe 4 is 16
the value of 2 raised to powe 5 is 32
Python Program to Find Numbers Divisible by Another Number
[45]: for i in range(1,100):
if i%13==0:
k=i
print(k,end=" ")
print(list[k],end=" ")

13 list[13] 26 list[26] 39 list[39] 52 list[52] 65 list[65] 78 list[78] 91


list[91]

11
[49]: l=[39,79,48,49,88]
r=list(filter(lambda x : x%13==0,l)) # l=x for lambda
print(r)

[39]
Convert Decimal to Binary, Octal, and Hexadecimal
[51]: d= int(input("enter a number"))
print(bin(d))
print(oct(d))
print(hex(d))
# decimal 0 to 9
# bin ,oct ,hex is python inbulit function

enter a number5
0b101
0o5
0x5
Python Program to Find ASCII Value of Character
ord() inbulit Funaction python

[56]: char= input("Enter a charcater")


print("Ascii of value of",char,"is",ord(char))

Enter a charcatera
Ascii of value of a is 97

[6]: for i in range(1,5):


for j in range(1,10):
if j>=5-i and j<=3+i:
print("*",end="")
else:
print("",end=" ")
print()

*
***
*****
*******

[4]: for i in range(1,6):


print(" * "*i)
for y in range(5,0,-1):
print(" * "*y)

*
* *

12
* * *
* * * *
* * * * *
* * * * *
* * * *
* * *
* *
*

[17]: for i in range(5,0,-1):


print("*"*i)

*****
****
***
**
*

[19]: for i in range(1,6):


k=6-i
for j in range(1,6):
if j<=6-i:
print(k,end=" ")
k-=1
else:
print(" ",end="")
print()

5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
Python Program to Check Prime Number - Complete Guide | Python Tutorial

[35]:

enter a number1
prime Number

[ ]: n= int( input("enter a number"))


if n==1:
print("prime Number")
elif n%2==0:
print("not prime number")
else:
print(" prime number")

13
[ ]: num=int(input("Enter the number"))
if num==1:
print("it is prime number")
if num>1:
for i in range(2,num):
if num % i==0:
print("it is not prime number")
break
else:
print("prime number")

[48]: for i in range(1,12):


for j in range(2,i):
if i %2==0:
break
else:
l.append(i)
print(l)

[3, 5, 5, 5, 7, 7, 7, 7, 7, 9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11, 11, 11, 11,


11]

[4]: ## revesre string


a=input("enter word")
while len(a):
print(a[::-1])
break

enter wordI love YOU


UOY evol I

[7]: l=[1,2,3]
l.reverse()
print(l)

[3, 2, 1]

[3]: a=input("enetr a word ")[::-1]


print(a)

enetr a word Rohit


tihoR

[2]: ###Program to find reverse of a string


a=input("Enter String:")
for i in range((len(a)-1),-1,-1):
print(a[i],end="")

14
Enter String:hello
olleh

[ ]:

15

You might also like