0% found this document useful (0 votes)
2 views5 pages

Python Programming

The document contains a series of Python programming interview questions and their respective answers. Topics covered include finding indices of maximum values in a NumPy array, calculating percentiles, checking for palindromes, summing a list, implementing bubble sort, producing star triangles, generating Fibonacci series, checking for prime numbers, sorting a numerical dataset, and printing ASCII values of characters. Each question is accompanied by sample code and output.

Uploaded by

mamatha.pragada
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Python Programming

The document contains a series of Python programming interview questions and their respective answers. Topics covered include finding indices of maximum values in a NumPy array, calculating percentiles, checking for palindromes, summing a list, implementing bubble sort, producing star triangles, generating Fibonacci series, checking for prime numbers, sorting a numerical dataset, and printing ASCII values of characters. Each question is accompanied by sample code and output.

Uploaded by

mamatha.pragada
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Python Programming Interview Questions

1. Write a code to get the indices of N maximum values from a NumPy array?

Ans: We can get the indices of N maximum values from a NumPy array using the below
code:
import numpy as np
ar = np.array([1, 3, 2, 4, 5, 6])
print(ar.argsort()[-3:][::-1])

Output: [5 4 3]

2. What is the easiest way to calculate percentiles when using Python?

Ans: The easiest and the most efficient way you can calculate percentiles in Python is to
make use of NumPy arrays and its functions.

Consider the following example:


import numpy as np
a = np.array([1,2,3,4,5,6,7])
p = np.percentile(a, 50) #Returns the 50th percentile which is also the median
print(p)

Output: 4.0

3. Write a Python program to check whether a given string is a palindrome or not,

without using an iterative method?

Ans: A palindrome is a word, phrase, or sequence that reads the same backward as forward,
e.g., madam, nurses run, etc.

Consider the below code:


def fun(string):
s1 = string
s = string[::-1]
if s1 == s:
return True
else:
return False

print(fun("madam"))

Output: True

4. Write a Python program to calculate the sum of a list of numbers?


def sum(num):
if len(num) == 1:
return num[0] # With only one element in the list, the sum result will be equal to the
element.
else:
return num[0] + sum(num[1:])

print(sum([2, 4, 5, 6, 7]))

Output: 24

5. Write a program in Python to execute the Bubble sort algorithm?

Check out the code below to execute bubble sort-


def bubbleSort(x):
n = len(x)
# Traverse through all array elements
for i in range(n-1):
for j in range(0, n-i-1):
if x[j] > x[j+1]:
x[j], x[j+1] = x[j+1], x[j]

# Driver code to test above


arr = [25, 34, 47, 21, 22, 11, 37]
bubbleSort(arr)

print("Sorted array is:")


for i in range(len(arr)):
print(arr[i])

Output:

11,21,22,25,34,37,47
6. Write a program in Python to produce Star triangle?

Ans: The below code produces a star triangle-


def Star_triangle(n):
for x in range(n):
print(' '*(n-x-1)+'*'*(2*x+1))

Star_triangle(9)

Output:

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

7. Write a program to produce Fibonacci series in Python?

The Fibonacci series refers to a series where an element is the sum of two elements prior to it.
nterms = int(input("How many terms? "))
# first two terms
n1, n2 = 0, 1
count = 0
# check if the number of terms is valid
if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1

Output:

How many terms? 7


Fibonacci sequence:
0
1
1
2
3
5
8

8. Write a program in Python to check if a number is prime?

Ans: The below code is used to check if a number is prime or not


num = 13

if num > 1:
for i in range(2, int(num/2)+1):
if (num % i) == 0:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")
else:
print(num, "is not a prime number")

Output:

13 is a prime number
9. Write a sorting algorithm for a numerical dataset in Python?

code to sort a list in Python:


my_list = ["8", "4", "3", "6", "2"]

my_list = [int(i) for i in list]

my_list.sort()

print (my_list)

Output:

2,3,4,6,8

10. Write a Program to print ASCII Value of a character in python?

Ans: Check the below code to print ASCII value:


x= 'a'

# print the ASCII value of assigned character stored in x

print(" ASCII value of '" + x + "' is", ord(x))

Output: 65

You might also like