Python Data Structure Interview Questions

Displaying 1 - 10 of 98

Python: Count Prime Numbers upto the given input number

09/19/2021 - 12:13 by devraj


n=int(input("Enter the number till you want to check: "))
primes = []
for i in range (2, n+1):
    for j in range(2, i):
        if i%j == 0:
print("Prime Numbers: ",primes)        
print("Count of prime numbers: ", len(primes))


Enter the number till you want to check: 11
Prime Numbers:  [2, 3, 5, 7, 11]
Count of prime numbers:  5

Python: In a List and in a Dictionary, What Are the Typical Characteristics of Elements?

06/24/2021 - 01:33 by devraj

Elements in lists maintain their ordering unless they are explicitly commanded to be re-ordered. They can be of any data type, they can all be the same, or they can be mixed. Elements in lists are always accessed through numeric, zero-based indices.

In a dictionary, each entry will have a key and a value, but the order will not be guaranteed. Elements in the dictionary can be accessed by using their key.

Lists can be used whenever you have a collection of items in an order. A dictionary can be used whenever you have a set of unique keys that map to values.

How will you remove the last object from a list?

06/24/2021 - 01:22 by devraj

 The pop function removes and returns the last object (or obj) from the list. Here -1 represent last element of the list. Below example will return 5.

my_list = [1, 2, 3, 4, 5]
last_element = my_list.pop(-1)

What is the difference between Python Arrays and lists?

06/24/2021 - 01:20 by devraj

 Arrays and lists, in Python, have the same way of storing data. But, arrays can hold only a single data type elements whereas lists can hold any data type elements.

import array as arr

my_array = arr.array('i', [1, 2, 3, 4])
my_list=[1, 'abc', 1.20]


In Python what is slicing?

06/24/2021 - 01:14 by devraj

A mechanism to select a range of items from sequence types like list, tuple, strings etc. is known as slicing. Example:

a = ("a", "b", "c", "d", "e", "f", "g", "h")
x = slice(2)

Output: ('a', 'b')