Python Interview Questions & Answers

Showing 10 of 69 questions | Page 2

Python technical interview questions and answers are widely asked in software development, data science, automation, and machine learning roles. Python is one of the most popular languages due to its simplicity and power, so companies expect candidates to understand core topics like variables, loops, functions, OOP, modules, file handling, exceptions, and libraries. Companies like TCS, Infosys, Cognizant, Capgemini, and Wipro frequently include Python questions in interviews and online tests. This guide provides clear explanations for the most important Python questions, making it useful for freshers preparing for placement interviews as well as experienced developers. You can also download Python interview PDFs and practice coding exercises to strengthen your preparation.

Python developers can enhance their capabilities by exploring data science applications and machine learning  implementations 

Showing 10 of 69 questions

11. How do I delete a file? (And other file questions...)

Use os.remove(filename) or os.unlink(filename);

12. How do I copy a file?

The shutil module contains a copyfile() function.

13. Can I create my own functions in C?

Yes, you can create built-in modules containing functions, variables, exceptions and even new types in C.

14. How do I generate random numbers in Python?

The standard module random implements a random number generator. Usage is simple: import random random.random() This returns a random floating point number in the range [0, 1).

15. Can I create my own functions in C++?

Yes, using the C compatibility features found in C++. Place extern "C" { ... } around the Python include files and put extern "C" before each function that is going to be called by the Python interpreter. Global or static C++ objects with constructors are probably not a good idea.

16. How can I execute arbitrary Python statements from C?

The highest-level function to do this is PyRun_SimpleString() which takes a single string argument to be executed in the context of the module __main__ and returns 0 for success and -1 when an exception occurred (including SyntaxError). If you want more control, use PyRun_String(); see the source for PyRun_SimpleString() in Python/pythonrun.c.

17. How can I evaluate an arbitrary Python expression from C?

Call the function PyRun_String() from the previous question with the start symbol Py_eval_input; it parses an expression, evaluates it and returns its value.

18. How do I interface to C++ objects from Python?

Depending on your requirements, there are many approaches. To do this manually, begin by reading the "Extending and Embedding" document. Realize that for the Python run-time system, there isn't a whole lot of difference between C and C++ -- so the strategy of building a new Python type around a C structure (pointer) type will also work for C++ objects.

19. Where is Freeze for Windows?

"Freeze" is a program that allows you to ship a Python program as a single stand-alone executable file. It is not a compiler; your programs don't run any faster, but they are more easily distributable, at least to platforms with the same OS and CPU.

20. What is metaprogramming in Python and how is it implemented?

Metaprogramming allows for writing programs that manipulate other programs, typically through metaclasses and decorators, which can dynamically alter class behavior.
Questions and Answers for Competitive Exams Various Entrance Test