Top 10 Most Asked Python Interview Questions With Answers Part 13

By Muhammad Umair

Muhammad Umair
4 min readNov 18, 2022
Top 10 Most Asked Python Interview Questions With Answers Part 13 By Muhammad Umair

Q.1 How will you find, in a string, the first word that rhymes with ‘cake’?

For our purpose, we will use the function search(), and then use group() to get the output.

import re rhyme=re.search('.ake','I would make a cake, but I hate to bake') rhyme.group()

‘make’

And as we know, the function search() stops at the first match. Hence, we have our first rhyme to ‘cake’.

Q.2 What is Tkinter?

Tkinter is a famous Python library with which you can craft a GUI. It provides support for different GUI tools and widgets like buttons, labels, text boxes, radio buttons, and more. These tools and widgets have attributes like dimensions, colors, fonts, colors, and more.

You can also import the tkinter module.

import tkinter top=tkinter.Tk()

This will create a new window for you:

This creates a window with the title ‘My Game’. You can position your widgets on this.

Follow this link to know more about Python Libraries

Q.3 How is a .pyc file different from a .py file?

While both files hold bytecode, .pyc is the compiled version of a Python file. It has platform-independent bytecode. Hence, we can execute it on any platform that supports the .pyc format. Python automatically generates it to improve performance(in terms of load time, not speed).

Q.4 How do you calculate the length of a string?

This is simple. We call the function len() on the string we want to calculate the length of.

len('Adi Shakara')

Q.5 What does the following code output?

def extendList(val, list=[]):      list.append(val)      return list list1 = extendList(10) list2 = extendList(123,[]) list3 = extendList('a') list1,list2,list3

Ans. ([10, ‘a’], [123], [10, ‘a’])

You’d expect the output to be something like this:

([10],[123],[‘a’])

Well, this is because the list argument does not initialize to its default value ([]) every time we make a call to the function. Once we define the function, it creates a new list. Then, whenever we call it again without a list argument, it uses the same list. This is because it calculates the expressions in the default arguments when we define the function, not when we call it.

Let’s revise the Basis of Python Programming

Q.6 What is a decorator? How do I define my own?

Ans. A decorator is a function that adds functionality to another function without modifying it. It wraps another function to add functionality to it. A Python decorator is a specific change that we make in Python syntax to alter functions easily.

def decor(func):    def wrap():        print("$$$$$$$$$$$$$$$$$")        func()            print("$$$$$$$$$$$$$$$$$")    return wrap@decordef sayhi():    print("Hi")sayhi()

$$Hi$$

Decorators are an example of metaprogramming, where one part of the code tries to change another. For more on decorators, read Python Decorators.

Q.7 Why use function decorators? Give an example.

A decorator is essentially a callable Python object that is used to modify or extend a function or class definition.

One of the beauties of decorators is that a single decorator definition can be applied to multiple functions (or classes). Much can thereby be accomplished with decorators that would otherwise require lots of boilerplate (or even worse redundant!) code.

Flask, for example, uses decorators as the mechanism for adding new endpoints to a web application. Examples of some of the more common uses of decorators include adding synchronization, type enforcement,logging, or pre/post conditions to a class or function.

Q.8 How many arguments can the range() function take?

Ans. The range() function in Python can take up to 3 arguments. Let’s see this one by one.

a. One argument

When we pass only one argument, it takes it as the stop value. Here, the start value is 0, and the step value is +1.

list(range(5))

[0, 1, 2, 3, 4]

list(range(-5))

[]

list(range(0))

[]

b. Two arguments

When we pass two arguments, the first one is the start value, and the second is the stop value.

list(range(2,7))

[2, 3, 4, 5, 6]

list(range(7,2))

[]

list(range(-3,4))

[-3, -2, -1, 0, 1, 2, 3]

c. Three arguments

Here, the first argument is the start value, the second is the stop value, and the third is the step value.

list(range(2,9,2))

[2, 4, 6, 8]

list(range(9,2,-1))

[9, 8, 7, 6, 5, 4, 3]

Q.9 How do you debug a program in Python? Answer in brief.

Ans. To debug a Python program, we use themodule. This is the Python debugger; we will discuss it in a tutorial soon. If we start a program using pdb, it will let us step through the code.

Q.10 List some pdb commands.

Some pdb commands include-

<b> — Add breakpoint<c> — Resume execution<s> — Debug step by step<n> — Move to next line<l> — List source code<p> — Print an expression

--

--

Muhammad Umair
Muhammad Umair

Written by Muhammad Umair

MERN Stack Developer | Software Engineer| Frontend & Backend Developer | Javascript, React JS, Express JS, Node JS, MongoDB, SQL, and Python