dridhOn

Python Interview Questions and Answers

Python Interview Questions and Answers

Q.1. In Python, is indentation required? Why?

In Python, indentation is required. Because it doesn't utilize curly braces, you'll need to indent code blocks evenly so that it knows what belongs in a block and what doesn't. This is a mistake:

 

Q.2 What does PEP 8 stand for?

The Python Enhancement Proposal 8 (PEP) is a Python-style guide. It includes coding rules and recommendations to assist make code more readable and consistent. Indentation, line length, blank lines, source file encoding, imports, string quotes, whitespace, trailing commas, comments, and docstrings, naming standards, and some programming advice are all included.

 

Q.3 What is the purpose of the zip() function?

This function combines objects by mapping related indices of several objects. After that, it returns a zip object. This is how it works:

 

Q.4. What is the purpose of the swap case() function?

The str class has a swap case() function that transforms uppercase to lowercase and vice versa.

 

Q.5. Can you shuffle 10 cards in your hands ([2,4,7,8,9, J, Q, K, A, A]) with Python?

Here, we'll use the random module's shuffle() method. This method shuffles a list while it is still in place.

 

Q.6. Which Python module will you use if you wish to work with files?

Python includes the following options for dealing with text and binary files:

shutil os (and os.path)
You can use them to create a file, duplicate its contents, and remove it.

 

Q.7. Does Python offer a variety of file processing modes?

You can open files in four different modes with Python: read-only, write-only, read-write, and add.

Only read mode is available. ('r') – Open a file in default mode for reading.
Open a file for writing, replace any contents, or create a new file in write-only mode ('w').
'rw' stands for read-write mode. - Open a file that needs to be updated.
Append mode ('a') – adds a new line to the end of the file.

 

Q.8. What is the difference between Python 2.x and Python 3.x?

Python 2 and 3 are similar yet differ slightly:

The print() function in Python 3 replaces the print statement in Python 2.
Strings in Python 3 are Unicode by default. Strings in Python 2 are ASCII.
Some Python 3 libraries are incompatible with Python 2.
For input, Python 2 had a raw input() method. The xrange() function was also included.
Python 2 will be deprecated on January 1, 2020; legacy Python 2 programs can be converted to Python 3 by then.
In Python 2, division returns an integer, whereas in Python 3, it returns a float.

 

Q.9. What is the purpose of enumerating ()?

This function iterates through a sequence and gets the index position and value for each item.

 

Q.10: What does PYTHONPATH stand for?

This is a comparable environment variable to PATH. The interpreter looks for a module at the address provided by this variable whenever you import it. It extends the usual module file search path. The value of this variable is a string containing a directory list for the sys.path directory list. One good application of this is to import our code that isn't yet available as an installable package.

 

Q.11 What is the difference between PYTHONSTARTUP, PYTHONCASEOK, and PYTHONHOME?

PYTHONSTARTUP is a variable that stores the path of an initialization file containing Python code. Before the first prompt appears, this executes every time you start the interpreter.

Python ignores cases in import statements if PYTHONCASEOK is set. This only applies to Windows and Mac OS X. To

After you've activated it, you can change the value to whatever you like.

PYTHONHOME, on the other hand, modifies the location of the standard Python libraries.

On Windows, go to System Properties and then to Environment Variables to do this. Add a new Environment Variable and its value after that. 

 

Q.12 What distinguishes lists from tuples?

In Python, there are two data types: lists and tuples, both of which are collections of objects. But they're not the same, and here's why:

Tuples are immutable, whereas lists are mutable. A list can be changed, but not a tuple.

Tuples are faster than lists.
Because tuples are immutable, they can be used as dictionary keys; lists cannot because they are mutable.
A tuple cannot be sorted, but we can sort a list with sort ().

 

Q.13. How do you get rid of the last item in a list?

Consider the following list of three items: [1,2,3]. This is how we can get rid of the last object: 

 

Q.14. Describe the Python map() function.

This function creates an iterator that computes the function using arguments from each iterable. This, like zipping (), exhausts the shortest iterable.

 

Q.15. In Python, what is __init__?

In C++, __init__ is a magic method that works similarly to a function Object() { [native code] }. It's used to set up new items. This function is called whenever a new object is created, and it allocates memory for the new object. Python will use its default version if you don't provide a __init__ for a class.

 

Q.16.Is Python entirely object-oriented? 

In Python, everything is an object. It isn't fully object-oriented, though, because it doesn't enable strong encapsulation, which is the packaging of data with methods and limiting access to some of them.

 

Q.17.What is loop interruption, and how does it work?

Interrupting a loop involves ending it before it has completed all of its iterations. There are three loop interruption statements in Python:

break — This exits the loop and executes the next statement in the sequence.

continue — Skips to the next iteration without performing the previous statements.

pass – When you don't know what to put in a loop, use this.

 

Q.18.Is Python a pass-by-value or pass-by-reference language?

Python does not support both pass-by-value and pass-by-reference. It's a pass-by-object-reference pass-through. This means that modifications to a mutable object supplied to a function will impact the original, whereas changes to an immutable object passed to a function would not.

 

Q.19.What is a namespace, exactly?

A name is a unique identifier (a name we use to refer to someone giving to things). A collection of names is referred to as a namespace. This is a list of all the names and the objects they identify. Multiple namespaces can be used to avoid name collisions. Even when we call a function, it creates a local namespace that contains all of the names.

 

Q.20. In Python, what is percent s?

We can embed values in strings with Python. F-strings, the format() method, and the percent operator are the three options. You can use it in the following way:

Follow Us on!

How can we help you?

To request a quote or want to meet up for a course discussion, contact us directly or fill out the form and we will get back to you promptly.