Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
36 views

Python Codin

The document contains Python coding interview questions and solutions. It includes questions on finding the number of days in a month, calculating roots of a quadratic equation, counting digits in an integer, calculating factorials of a number, generating the Fibonacci series, and finding the sum of natural numbers. For each question, it provides the code to solve the problem and example runs of the code with input/output.

Uploaded by

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

Python Codin

The document contains Python coding interview questions and solutions. It includes questions on finding the number of days in a month, calculating roots of a quadratic equation, counting digits in an integer, calculating factorials of a number, generating the Fibonacci series, and finding the sum of natural numbers. For each question, it provides the code to solve the problem and example runs of the code with input/output.

Uploaded by

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

1/30/23, 12:36 PM Python Coding Interview Questions - Jupyter Notebook

Dharavath Ramdas

Python Coding Interview Questions and Answers link


github link : https://lnkd.in/gNZs5b9N (https://lnkd.in/gNZs5b9N)

Python Coding Interview Questions

Q.1.write a program to find the number of days in a month of a given year in python ?

In [23]:

def no_of_days_month(year,month):
leap = 0
if year%400 == 0:
leap = 1
elif year%100 == 0:
leap = 0
elif year%4 == 0:
leap = 1
if month == 2:
return "Total Days in Month :",28 + leap
odd_months = [1,3,5,7,8,10,12]
if month in odd_months:
return "Total Days in Month :",31
else:
return "Total Days in Month :",30
no_of_days_month(int(input("Enter Year :")),int(input("Enter Month :")))

Enter Year :1999


Enter Month :5

Out[23]:

('Total Days in Month :', 31)

In [24]:

no_of_days_month(int(input("Enter Year :")),int(input("Enter Month :")))

Enter Year :2000


Enter Month :2

Out[24]:

('Total Days in Month :', 29)

In [25]:

no_of_days_month(int(input("Enter Year :")),int(input("Enter Month :")))

Enter Year :2001


Enter Month :2

Out[25]:

('Total Days in Month :', 28)

In [26]:

no_of_days_month(int(input("Enter Year :")),int(input("Enter Month :")))

Enter Year :2002


Enter Month :2

Out[26]:

('Total Days in Month :', 28)

localhost:8888/notebooks/Python Coding Interview Questions.ipynb 1/4


1/30/23, 12:36 PM Python Coding Interview Questions - Jupyter Notebook

Q.2.write a program to find roots of a quadratic equation in python ?

In [27]:

# import complex math module


import cmath
a = float(input('Enter a: '))
b = float(input('Enter b: '))
c = float(input('Enter c: '))

# calculate the discriminant


d = (b**2) - (4*a*c)

# find two solutions


sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)
print('The solution are {0} and {1}'.format(sol1,sol2))

Enter a: 2
Enter b: 2
Enter c: 2
The solution are (-0.5-0.8660254037844386j) and (-0.5+0.8660254037844386j)

Q 3.program to find the number of digits in a given integer count digits in given number in python ?

In [28]:

def count_digits(num):

print(len(str(num)))
count_digits(int(input()))

3456
4

In [29]:

count_digits(int(input()))

12345
5

In [30]:

count_digits(int(input()))

12345678
8

In [7]:

n=int(input("Enter number:"))
count=0
while(n>0):
count=count+1
n=n//10
print("The number of digits in the number are:",count)

Enter number:345
The number of digits in the number are: 3

Q 4.program to find factorial of a number in python ?

In [3]:

n = 7
fac = 1
c = 1
while c<= n:
fac = fac * c
c = c + 1
print(fac)

5040

localhost:8888/notebooks/Python Coding Interview Questions.ipynb 2/4


1/30/23, 12:36 PM Python Coding Interview Questions - Jupyter Notebook

In [19]:

def factorial(n):
num = 1
while n >= 1:
num = num * n
n = n - 1
return num

In [20]:

factorial(int(input("Enter Number :")))

Out[20]:

720

In [31]:

factorial(int(input("Enter Number :")))

Enter Number :5

Out[31]:

120

In [21]:

factorial(int(input("Enter Number :")))

Enter Number :8

Out[21]:

40320

Q.5 program to print fibonacci series in python ?

In [10]:

def fibo(num):

if num == 0:
return 0
elif num == 1:
return 1
else:
return fibo(num-1) + fibo(num-2)
for i in range(10):
print(fibo(i))

0
1
1
2
3
5
8
13
21
34

Q.6 program to find the sum of n natural numbers in python ?

In [14]:

def sum_of_natu(n):
su = 0
if n < 0:
return "Enter Positive Number"
else:
while 1 <= n:
su = su + n
n = n-1
return su
sum_of_natu(16)

Out[14]:

136

localhost:8888/notebooks/Python Coding Interview Questions.ipynb 3/4


1/30/23, 12:36 PM Python Coding Interview Questions - Jupyter Notebook

In [15]:

sum_of_natu(3)

Out[15]:

In [16]:

sum_of_natu(4)

Out[16]:

10

In [17]:

sum_of_natu(10)

Out[17]:

55

In [ ]:

localhost:8888/notebooks/Python Coding Interview Questions.ipynb 4/4

You might also like