Python Programming
Python Programming
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]
Ans: The easiest and the most efficient way you can calculate percentiles in Python is to
make use of NumPy arrays and its functions.
Output: 4.0
Ans: A palindrome is a word, phrase, or sequence that reads the same backward as forward,
e.g., madam, nurses run, etc.
print(fun("madam"))
Output: True
print(sum([2, 4, 5, 6, 7]))
Output: 24
Output:
11,21,22,25,34,37,47
6. Write a program in Python to produce Star triangle?
Star_triangle(9)
Output:
*
***
*****
*******
*********
***********
*************
***************
*****************
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:
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?
my_list.sort()
print (my_list)
Output:
2,3,4,6,8
Output: 65