Python Interview Questions and Answers

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3
At a glance
Powered by AI
Python is a programming language with objects, modules, threads, exceptions and automatic memory management.

Python is a programming language with objects, modules, threads, exceptions and automatic memory management. The benefits of pythons are that it is simple and easy, portable, extensible, build-in data structure and it is an open source.

PEP 8 is a coding convention, a set of recommendation, about how to write your Python code more readable.

1. What is Python? What are the benefits of using Python?

Ans: Python is a programming language with objects, modules, threads, exceptions and automatic memory
management.
The benefits of pythons are that it is simple and easy, portable, extensible, build-in data structure and it is an open
source.
2. What is PEP 8?
Ans: PEP 8 is a coding convention, a set of recommendation, about how to write your Python code more readable.
3. What is pickling and unpickling?
Ans: The process of writing state of object to the file by using dump function is called pickling and the process of
reading state of an object from the file is called unpickling.
4. How Python is interpreted?
Ans: PVM reads Line by Line of Byte Code and converted into Machine Understandable Code(Binary Code) and It is
read By OS and Processer and Gives Result.
5. How memory is managed in Python?
Ans: Python memory is managed by Python private heap space. All Python objects and data structures are located in
a private heap. The programmer does not have an access to this private heap and interpreter takes care of this
Python private heap. The allocation of Python heap space for Python objects is done by Python memory manager.
Python also have an inbuilt garbage collector makes it available to the heap space.
6. What are the tools that help to find bugs or perform static analysis?
Ans: PyChecker is a static analysis tool that detects the bugs in Python source code and warns about the style and
complexity of the bug. Pylint is another tool that verifies whether the module meets the coding standard.
7. What are Python decorators?
Ans: A Python decorator is a specific change that we make in Python syntax to alter functions easily.
8. What is the difference between list and tuple?
Ans: The difference between list and tuple is that list is mutable while tuple is not. Tuple can be hashed for e.g as a
key for dictionaries.
9. How are arguments passed by value or by reference?
Ans: Everything in Python is an object and all variables hold references to the objects. The references values are
according to the functions; as a result you cannot change the value of the references. However, you can change the
objects if it is mutable.
10. What is Dict and List comprehensions are?
Ans: They are syntax constructions to ease the creation of a Dictionary or List based on existing iterable.
11. What are the built in type does python provides?
Ans: There are mutable and Immutable types of Pythons built in types Mutable built-in types
List Sets
Dictionaries Immutable built-in types
Strings Tuples Numbers
12. What is namespace in Python?
Ans: A collection of currently defined symbolic names along with information about the object that each name
reference
13. What is lambda in Python?
Ans: It is a single expression anonymous function often used as In-line function.
14. Why lambda forms in python does not have statements?
Ans: A lambda form in python does not have statements as it is used to make new function object and then return
them at runtime.
15. What is pass in Python?
Ans: Pass means, no-operation Python statement, or in other words it is a place holder in compound statement,
where there should be a blank left and nothing has to be written there.
16. In Python what are iterators?
Ans: In Python, iterators are used to iterate a group of elements, containers like list.
17. What is unit test in Python?
Ans: A unit testing framework in Python is known as unit test. It supports sharing of setups, automation testing,
shutdown code for tests, aggregation of tests into collections etc.

18. In Python what is slicing?


Ans: A mechanism to select a range of items from sequence types like list, tuple, strings etc. is known as slicing.
19. What are generators in Python?
Ans: The way of implementing iterators are known as generators. It is a normal function except that it yields
expression in the function.
20. What is docstring in Python?
Ans: A Python documentation string is known as docstring, it is a way of documenting Python functions, modules
and classes.
21. How can you copy an object in Python?
Ans: To copy an object in Python, you can try copy.copy () or copy.deepcopy() for the general case. You cannot copy
all objects but most of them.
22. What is negative index in Python?
Ans: Python sequences can be index in positive and negative numbers. For positive index, 0 is the first index, 1 is the
second index and so forth. For negative index, (-1) is the last index and (-2) is the second last index and so forth.
23. How you can convert a number to a string?
Ans: In order to convert a number into a string, use the inbuilt function str(). If you want a octal or hexadecimal
representation, use the inbuilt function oct() or hex().
24. What is the difference between Xrange and range?
Ans: Xrange returns the xrange object while range returns the list, and uses the same memory and no matter what
the range size is.
25. What is module and package in Python?
Ans: In Python, module is the way to structure program. Each Python program file is a module, which imports other
modules like objects and attributes.
The folder of Python program is a package of modules. A package can have modules or subfolders.
26. Mention what are the rules for local and global variables in Python?
Ans:Global Variables: Variables declared outside a function or in global space are called global variables.
Local Variables: Any variable declared inside a function is known as a local variable.
27. How can you share global variables across modules?
Ans: To share global variables across modules within a single program, create a special module. Import the config
module in all modules of your application. The module will be available as a global variable across modules.
28. Explain how can you make a Python Script executable on Unix?To make a Python Script executable on Unix,
you need to do two things
Ans: Script file’s mode must be executable and the first line must begin with # ( #!/usr/local/bin/python)
29. Explain how to delete a file in Python?
Ans: By using a command os.remove (filename) or os.unlink(filename)
30. Explain how can you generate random numbers in Python?
Ans: To generate random numbers in Python, you need to import command as import random random.random()
This returns a random floating point number in the range [0,1)
31. Explain how can you access a module written in Python from C?
Ans: You can access a module written in Python from C by following method, Module =
=PyImport_ImportModule(“”);
32. Mention the use of // operator in Python?
Ans: It is a Floor Divisionoperator , which is used for dividing two operands with the result as quotient showing only
digits before the decimal point. For instance, 10//5 = 2 and 10.0//5.0 = 2.0.
33. Mention the use of the split function in Python?
Ans: The use of the split function in Python is that it breaks a string into shorter strings using the defined separator.
It gives a list of all words present in the string.
34. What is python’s standard way of identifying a block of code?
Ans: Indentation.
35. Please provide an example implementation of a function called “my_func” that returns the square of a given
variable “x”. (Continues from previous question)
Ans:
defmy_func(x):
returnx**2

36. Is python statically typed or dynamically typed?


Ans: Dynamic.
37. Is python strongly typed or weakly typed language?
Ans: Strong.
38. Create a unicode string in python with the string “This is a test string”?
Ans: some_variable=u’Thisisateststring’ (Or) some_variable=u”Thisisateststring”
39. What is the python syntax for switch case statements?
Ans: Python doesn’t support switch­case statements. You can use if-else statements for this purpose.
40. What is a lambda statement? Provide an example.
Ans: A lambda statement is used to create new function objects and then return them at runtime.
41.What are some mutable and immutable data-types?
Ans: Mutable Types: byte array, set, list, dict
Immutable Types: int, float, bool, complex, str, byte, range, tuple, Frozen set
42.What can you use Python generator functions for?
Ans: One of the reasons to use generator is to make the solution clearer for some kind of solutions. The other is to
treat results one at a time, avoiding building huge lists of results that you would process separated anyway.
43.What Are Closures In Python?
Ans: Python closures are function objects returned by another function. We use them to eliminate code redundancy.
44.What’s your preferred text editor?
Ans: Emacs. Any alternate answer leads to instant disqualification of the applicant
45.When should you use generator expressions vs list comprehensions in Python and vice versa?
Ans: Iterating over the generator expression or the list comprehension will do the same thing. However, the list
comp will create the entire list in memory first while the generator expression will create the items on the fly, so you
are able to use it for very large (and also infinite!) sequences.
46. What is a negative index in Python?
Ans: Python arrays and list items can be accessed with positive or negative numbers. A negative Index accesses the
elements from the end of the list counting backwards.
47. How to convert a string to lowercase in Python?
Ans: use lower() function.
48. How to convert a string to uppercase in Python?
Ans: Similar to the above question. use upper() function instead.
49. How to check if string A is substring of string B?
Ans: The easiest way is to use the in operator.
>>> ‘abc’ in ‘abcdefg’
True
50. What is __init__.py?
Ans: It is used to import a module in a directory
51. How append to a string
Ans: The easiest way is to use the += operator. If the string is a list of character, join() function can also be used.

You might also like