14 Simple Python Programming Language Interview Questions

Below are 14 simple Python Programming Language questions which can be asked in a Python Related job interview.

Q. 1 – Create a Simple number List?

a_list = [19, 39, 48, 49]

Q. 2 – Create some Python Nested Lists?

list1 = [19, 28, [“Computer”, “Science”], “Python”, [“Coding”, “Programming”]]
list2 = [73, 281, 38, [“Coding”, “What is Programming?”], 73, 291, [“C++”]]

Q. 3 – Explain Create, Update, Delete List Operations?

Creation
lst = [11,22,33, 44]
Above statement creates a list with the name lst.

Empty list creation
lst =[]
The above statement creates an empty list.

Adding single element in list
lst.append(55)

Adding elements of another list
lst.extend([44,44,33])

Concatenate list
Concatenate is to join two list.
Following code adds two list and creates a new list c containing elements from both list.
a =[1,2,3,4]
b=[5,6,7,8,9,10]
c = a + b
print(c) #Outputs 1,2,3,4,5,6,7,8,9,10

Remove element using value
a =[1,2,3,4]
a.remove(3)
print(a) #prints out [1,2,4]
In above code, element with value 3 is removed.

Delete elements by using index
a =[1,2,3,4]
del a[2] #removes element at index 2
print(a) #prints out [1,2,4]

Clear or Empty a list
a=[1,2,3,4]
a.clear()
print(a) # prints out []

Length of list
To print size of list len, function is used.
a=[1,2,3,4]
print(len(a)) #Prints out 4

Q. 4 – Explain Create, Update, Delete Dictionary Operations?

Create
Dictionaries can be created by listing key-value pairs inside curly brackets like dict1 = {1: “Python”, 2: “Java”, 3:”C++”}

Update
Key-value pairs can be added to a dictionary using indexing. Like for adding a new key-value pair to dict1 just do dict[4] = “Swift”. Now dict1 will be {1: “Python”, 2: “Java”, 3:”C++”, 4:”Swift”}

Delete
For deleting any key-value pair from Dictionary just use pop() function passing it, key as a parameter. So for removing 1: “Python” pair from dict1 just do dict.pop(1). Now dict1 will be {2: “Java”, 3:”C++”, 4:”Swift”}

Q. 5 – Explain Some Tuple Operations.

Creation
Python Tuples can be created by simply listing comma separated values in round brackets like (18, 83, 27, 8) or by listing values inside tuple() function like tuple(10, 3, 38, 28).

Length of Tuple
Length of a Python Tuple can be determined by using len() function and passing tuple name to it as a parameter.

Q. 6 – Explain del Python Operator

del operator is used for removing variables from memory. For example – a = (1, 2, 3) is a Python Tuple, to remove it just do del a

Q. 7 – How to delete last element in a List

Last element in Python List can be removed using del list_name[-1] code statement.

Q. 8 – Loop through Python List using enumerate function

a_list = [10, 38, 73, 82]
for i, j in enumerate(a_list):
	print(i, j)

Q. 9 – Reverse a Python Dictionary.

Reversing here means that for any key-value pair turn value as key and key as value. So 1 : “Python” now becomes “Python” : 1.

a_dict = {1:"Computer", 2:"Science", 3:"Hub"}

reversed_a_dict = {value:key for key, value in a_dict.items()}

print(reversed_a_dict)
# Prints out {'Computer': 1, 'Science': 2, 'Hub': 3}

Q. 10 – How to sort Python Dictionary by value?

import operator

a_dict = {1: "Python", 2: "Programming", 3: "Language", 4: "Coding"}

print(sorted(a_dict.items(), key=operator.itemgetter(1)))
# Prints out [(4, 'Coding'), (3, 'Language'), (2, 'Programming'), (1, 'Python')]

Q. 11 – How to randomly shuffle values in a Python List

Elements of a Python List can be shuffled randomly using shuffle() function from random module. So passing list as a parameter to random.shuffle(parameter) would return a shuffled Python List.

Q. 12 – Best way to get a key’s value from Python Dictionary?

a_dict = {1 : "Computer", 2 : "Programming", 3 : "Coding"}

key = 10                      # Defining Key whose value to be accessed from Dictionary

print(a_dict.get(key, "Key " + str(key) + " is not in dictionary"))

Q. 13 – Python Code for Merging Two Dictionaries?

dict1 = {1: "Computer", 2:"Science"}

dict2 = {3: "Python", 4:"Java"}

dict3 = {**dict1, **dict2}

print(dict3)
# Print out {1: "Computer", 2:"Science", 3: "Python", 4:"Java"}

Q. 14 – Preferred Way for fetching last element in a Python List

For fetching last, second last, third last and so on elements from a Python List use list_name[-1], list_name[-2], list_name[-3] ………………..

list1 = [19, 28, 37, 12, 93, 4, 5, 6]

print(list1[-1])
print(list1[-2])
print(list1[-3])
print(list1[-4])

More about Python Programming Language

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts