Python Interview Questions


Q6. Differentiate between deep and shallow copy?

Ans: Shallow copy stores the values that are copied in the new instance. It copies the reference pointers as well as the values. These references indicate the original objects. That’s why, if any changes are made to any member of the class, the original copy will be affected. Depending on the size of the data, it helps in faster execution.

Deep copy stores the values that are copied already. In addition to that, it doesn’t copy the reference pointers. If the original copy is altered, it won’t affect any other copy with the objects. It slows down the execution as multiple copies of the object are stored.

Q7: What can you do to achieve Multithreading in Python?

Ans: Python offers a package for multi-threading. However, for those who want to multi-thread to make your code run faster, then use it is not really a great idea.

  • It consists of a construct known as the Global Interpreter Lock (GIL). Due to the GIL, at a time, only one of the threads can be executed. A thread obtains the GIL, after some changes, it shifts the GIL onto the next thread.
  • This occurs very fast. They take turns using the CPU core and appears like they are moving in a parallel direction.
  • As a consequence, overhead is added to execution. Hence, using the threading package to speed up the code is not favorable.

Q8: How can you compile and link in python?

Ans: Basically, because of compiling and linking new extensions can be compiled without an error. After the compiling process is done, linking can take place.

  • Firstly, create a file. Give it a name in a language that a compiler of your system supports. For instance, file.c, etc.
  • In the Modules/ directory of the distribution which you are going to use, put this file.
  • In the directory that has the file, add a line in the file Setup.local.
  • Use spam file.o to run the file.
  • Rebuild the interpreter with help of the make command present on the upper-level directory.
  • In case the file is altered, run rebuildMakefile. You can use ‘make Makefile’ command to do it.

Q9: Explain the differences that separate Django, Pyramid, and Flask from each other?

Ans: Originally, Flask is a microframework. The purpose of Flask was to help with the small applications that have simple needs. It is ready to use and helps the programmer to use external libraries.

In contrast to Flask, Pyramid is created for bigger applications. It ensures flexibility so that the developer gets to use the correct tools according to the nature of their work. The developer has the liberty to opt for the database, URL structure, templating style, etc. we can say that Pyramid is heavily configurable.

Similarly, Django is used for larger applications. On top of that, it has an ORM.

Q10: How do multiple lines comments appear in python?

Ans: As the name suggests, Multi-line comments are present in multiple lines. Each and every line that is commented must be prefixed by a #. Most importantly, there is a method that does this directly in less time. If you are looking for a shortcut method to comment in multiple lines, hold the ctrl key and left-click in the spots where you want to add a # character. Finally, type a # only once. You can comment on all the lines wherever you clicked the cursor with this method.