**Python** Certification is the most sought-after skill in programming domain. In this Python Interview Questions blog, We will introduce you to the most frequently asked questions in Python interviews. Our Python Interview Questions is the one-stop resource from where you can boost your interview preparation. We have questions on Python Programming basics which will help you with different expertise levels to reap the maximum benefit from our blog.

Let us start by taking a look at some of the most frequently asked Python interview questions:-

Also See: **Python Basic Interview Questions**

#### 1Q. How can we debug a Python program?

**Ans:** By using the following command we can debug the Python program

$ python -m pdb python-script.py |

#### 2Q. Write a program to count the number of capital letters in a file?

**Ans:**

with open(SOME_LARGE_FILE) as countletter: count = 0 text = countletter.read() for character in text: if character.isupper(): count += 1 |

#### 3Q. Write a program in Python to produce Star triangle?

**Ans:** The code to produce star triangle is as follows:

def pyfun(r):
for a in range(r): print(‘ ‘*(r-x-1)+’*’*(2*x+1)) pyfun(9) |

**Output:**

* |

*** |

***** |

******* |

********* |

*********** |

************* |

*************** |

***************** |

#### 4Q. Write a program to check whether the given number is prime or not?

**Ans:** The code to check prime number is as follows:

# program to check the number is prime or not |

n1 = 409 |

# num1 = int(input(“Enter any one number: “)) |

# prime number is greater than 1 |

if n1 > 1: |

# check the following factors |

for x is in range of(2,num1): |

if (n1 % x) == 0: |

print(n1,”is not a prime number”) |

print(x,”times”,n1//x,”is”,num) |

Break |

else: |

print(n1,”is a prime number”) |

# if input number is smaller than |

# or equal to the value 1, then it is not prime number |

else: |

print(n1,”is not a prime number”) |

**Output:**

409 is a prime number |

#### 5Q. Write Python code to check the given sequence is a palindrome or not?

**Ans:**

# Python code to check a given sequence
# is palindrome or not my_string1 = ‘MOM’ My_string1 = my_string1.casefold() # reverse the given string rev_string1 = reversed(my_string1) # check whether the string is equal to the reverse of it or not if list(my_string1) == list(rev_string1): print(“It is a palindrome”) else: print(“It is not a palindrome”) |

** **

**Output:**

it is a palindrome |

#### 6Q. Write Python code to sort a numerical dataset?

**Ans:** The code to sort a numerical dataset is as follows:

list = [ “13”, “16”, “1”, “5” , “8”]
list = [int(x) for x in the list] list.sort() print(list) |

**Output:**

1, 5, 8, 13, 16 |

#### 7Q. What is the output of the following code?

x = [‘ab’,’cd’]
print(list(map(list, x))) |

**Ans:** The output of the following code is

[ [‘a’, ‘b’], [‘c’, ‘d’] |

**Data Analysis – Python Interview Questions**

**8Q. What is map function in Python?**

**Ans:** *Map** function executes the function given as the first argument on all the elements of the iterable given as the second argument. If the function given takes in more than 1 arguments, then many iterables are given. #Follow the link to know more similar functions.*

**9Q. Is python numpy better than lists?**

**Ans:** We use python numpy array instead of a list because of the below three reasons:

- Less Memory
- Fast
- Convenient

**10Q. How to get indices of N maximum values in a Numpy array?**

**Ans:** We can get the indices of N maximum values in a Numpy array using the below code:

import numpy as np
arr = np.array([1, 3, 2, 4, 5]) print(arr.argsort()[-3:][::-1]) |

Output

**11Q. How do you calculate percentiles with Python/ NumPy?**

**Ans:** We can calculate percentiles with the following code

import numpy as np
a = np.array([1,2,3,4,5]) p = np.percentile(a, 50) #Returns 50th percentile, e.g. median print(p) |

Output

** ****12Q. What is the difference between NumPy and SciPy?**

**Ans:**

- In an ideal world, NumPy would contain nothing but the array data type and the most basic operations: indexing, sorting, reshaping, basic element wise functions, et cetera.
- All numerical code would reside in SciPy. However, one of NumPy’s important goals is compatibility, so Numpy tries to retain all features supported by either of its predecessors.
- Thus NumPy contains some linear algebra functions, even though these more properly belong in SciPy. In any case, SciPy contains more fully-featured versions of the linear algebra modules, as well as many other numerical algorithms.
- If you are doing scientific computing with python, you should probably install both NumPy and SciPy. Most new features belong in SciPy rather than NumPy.

**13Q. How do you make 3D plots/visualizations using NumPy/SciPy?**

**Ans:** Like 2D plotting, 3D graphics is beyond the scope of NumPy and SciPy, but just as in the 2D case, packages exist that integrate with NumPy. Matplotlib provides basic 3D plotting in the mplot3d sub package, whereas Mayavi provides a wide range of high-quality 3D visualization features, utilizing the powerful VTK engine.