Python Interview Questions – Part 2

Hello Friends,

Today in this article we are going to continue our Python Interview Questions series

Questions:

1) What is difference between int() and eval() function?

2) Different editors in Python?

3) Explain Commenting in Python?

4) How to a read a file in Python?

5) Difference between for loop and while loop?

6) Difference between list and tuples?

7) Explain the concept of indenting and slicing?

8) What is xlrd?

9) How to find any word from the string?

10) Write a program to store seven games in the list entered by the user?

Solutions :

Q1) solution

eval() functionint() function
The eval() function
evaluates the expression x + 1
The int () function do not
evaluates the expression it gives an
error
Syntax : eval(expression, globals=None, locals=None)int(value, base)
Table 1 : Difference between eval() and int() function

eval() function

x = 1
print(eval("x + 1"))
-----------------------------
Output
2

int() function

x = 1
print(int("x + 1")
----------------------------------
Output
Traceback (most recent call last):
  File "c:\Users\USER\TEST.py", line 2, in <module>
    print(int("x + 1"))
ValueError: invalid literal for int() with base 10: 'x + 1'
PS C:\Users\USER> 

Q2) solution

There are many editors in Python but in the list are some popular editors

  1. Visual Studio Code – https://code.visualstudio.com/download
  2. PyCharm – https://www.jetbrains.com/pycharm/download/#section=windows
  3. Jupiter notebook – https://jupyter.org/install
  4. Anaconda – https://www.anaconda.com/?modal=commercial

Q3) solution

Please refer my article on comments – https://knowledge-junction.com/2021/10/23/python-exploring-comments/

Q4) solution

To read the txt file in the Python we will use read() function

# We will give the location
read_file=open("C:\\Users\\USER\\Desktop\\file_handling_demo_1.txt", "r")

# Now we will print
print(read_file.read()) 

# Full code

read_file=open("C:\\Users\\USER\\Desktop\\file_handling_demo_1.txt", "r")
print(f.read())

Q5) solution

for loop while loop
for loop can be used only in case of
a known number of iterations
while loop executes the code till
the statement is not false
Syntax : for val in sequence:
loop body
Syntax : while test_expression:
Body of while
Table-2 Difference between for loop and while loop

Q6) solution

List Tuples
List are mutable. We can change the
list
Tuples are immutable. We cannot
change the tuples
Table-3 Difference between List and Tuples

As a result, tuples are more memory efficient than the lists

import sys
a_list = list()
a_tuple = tuple()
a_list = [1,2,3,4,5]
a_tuple = (1,2,3,4,5)
print(sys.getsizeof(a_list))
print(sys.getsizeof(a_tuple))
----------------------------------

Output
104 (bytes for the list object)
88 (bytes for the tuple object)

Q7) solution

With the help of slice() we can slice any object. For example –

# Create a tuple
numbers = ("3", "4", "5", "1", "6", "8", "0")
slicing = slice(0, 2)
print(numbers[slicing])
------------------------------------
Output
('3', '4')

Most of the programming languages like C, C++, and Java use braces {} to define a block of code. However, Python uses indentation.

A code block (body of a function, loop, etc.) starts with indentation and ends with the first unindented line.

Generally, four whitespaces are used for indentation and are preferred over tabs. Here is an example.

for icounter in range(1,11):
    print(icounter)
    if icounter == 10:
        break

Q8) solution

Please refer my article on xlrd – https://knowledge-junction.com/2021/07/30/python-reading-an-excel-file-using-xlrd/

Q9) solution

To find any word from string we use string.find()

names = ("james sayyam, harry, jack")
index = names.find("sayyam")
print(index)
------------------------------------------
Output
6

Q10) solution

game1 = input("Enter the first games: ")
game2 = input("Enter the second games: ")
game3= input("Enter the third games: ")
game4= input("Enter the fourth games: ")
game5= input("Enter the fifth games: ")
game6= input("Enter the sixth games: ")
game7= input("Enter the seventh games: ")

gamelist = [game1,game2,game3,game4,game5,game6,game7]
print(gamelist)
---------------------------------------------------------
Output
Enter the first games: football
Enter the second games: cricket
Enter the third games: chess
Enter the fourth games: carrom
Enter the fifth games: hockey
Enter the sixth games: dog and bone
Enter the seventh games: skipping
['football', 'cricket', 'chess', 'carrom', 'hockey', 'dog and bone', 'skipping']

Python Interview questions – Part 1 – https://knowledge-junction.com/2022/03/05/python-basic-questions/

I will be continuing this series 🙂

Thank you

Have a nice day

You may also like...

1 Response

  1. Yogesh Meher says:

    Thanks for sharing Sayyam, good one!! Keep it up

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: