{"id":25,"date":"2023-02-14T05:54:00","date_gmt":"2023-02-14T05:54:00","guid":{"rendered":""},"modified":"2024-01-28T09:05:30","modified_gmt":"2024-01-28T09:05:30","slug":"top-30-python-interview-questions-answers","status":"publish","type":"post","link":"https:\/\/pythonpower.in\/index.php\/2023\/02\/14\/top-30-python-interview-questions-answers\/","title":{"rendered":"Top  30 Python Interview Questions &#038; Answers"},"content":{"rendered":"<p>&nbsp;<\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"http:\/\/pythonpower.in\/wp-content\/uploads\/2023\/02\/python-interview-questions-answers.jpg\" style=\"margin-left: 1em; margin-right: 1em;\"><img loading=\"lazy\" decoding=\"async\" border=\"0\" data-original-height=\"400\" data-original-width=\"800\" height=\"320\" src=\"http:\/\/pythonpower.in\/wp-content\/uploads\/2023\/02\/python-interview-questions-answers-300x150.jpg\" width=\"640\" \/><\/a><\/div>\n<p><\/p>\n<p><span style=\"font-size: large;\"><b>Top 30&nbsp; Python Interview Questions &amp; Answers<\/b><\/span><\/p>\n<p><span style=\"font-size: medium;\"><b>1) What is Python? What are the benefits of using Python?<\/b><\/span><\/p>\n<p>Python is a programming language with objects, modules, threads, exceptions and automatic<br \/>\nmemory management. The benefits of pythons are that it is simple and easy, portable,<br \/>\nextensible, build-in data structure and it is an open source.<\/p>\n<p><span style=\"font-size: medium;\"><b>2) What is PEP 8?<\/b><\/span><\/p>\n<p>PEP 8 is a coding convention, a set of recommendation, about how to write your Python code<br \/>\nmore readable.&nbsp;<\/p>\n<p><span style=\"font-size: medium;\"><b>3) What is pickling and unpickling?<\/b><\/span><\/p>\n<p>Pickle module accepts any Python object and converts it into a string representation and dumps<br \/>\nit into a file by using dump function, this process is called pickling. While the process of<br \/>\nretrieving original Python objects from the stored string representation is called unpickling.<\/p>\n<p><span style=\"font-size: medium;\"><b>4) How Python is interpreted?<\/b><\/span><\/p>\n<p>Python language is an interpreted language. Python program runs directly from the source<br \/>\ncode. It converts the source code that is written by the programmer into an intermediate<br \/>\nlanguage, which is again translated into machine language that has to be executed.<\/p>\n<p><span style=\"font-size: medium;\"><b>5) How memory is managed in Python?<\/b><\/span><\/p>\n<p><span style=\"font-size: medium;\"><b>&nbsp; &nbsp; .&nbsp;<\/b><\/span>Python memory is managed by Python private heap space. All Python objects and data<br \/>\nstructures are&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; located in a private heap. The programmer does not have an access to<br \/>\nthis private heap and interpreter&nbsp; &nbsp; &nbsp; &nbsp; takes care of this Python private heap.<\/p>\n<p>&nbsp; &nbsp;<span style=\"font-size: large;\"><b>.&nbsp;<\/b><\/span>The allocation of Python heap space for Python objects is done by Python memory<br \/>\nmanager. The core&nbsp; &nbsp; &nbsp; &nbsp; API gives access to some tools for the programmer to code.<\/p>\n<p>&nbsp; &nbsp;<span style=\"font-size: large;\"><b>.&nbsp;&nbsp;<\/b><\/span>Python also have an inbuilt garbage collector, which recycle all the unused memory and<br \/>\nfrees the&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; memory and makes it available to the heap space.<\/p>\n<p><span style=\"font-size: medium;\"><b>6) What are the tools that help to find bugs or perform static analysis?<\/b><\/span><\/p>\n<p>PyChecker is a static analysis tool that detects the bugs in Python source code and warns<br \/>\nabout the style and complexity of the bug. Pylint is another tool that verifies whether the module<br \/>\nmeets the coding standard.<\/p>\n<p><span style=\"font-size: medium;\"><b>7) What are Python decorators?<\/b><\/span><\/p>\n<p>A Python decorator is a specific change that we make in Python syntax to alter functions easily<\/p>\n<p><span style=\"font-size: medium;\"><b>8) What is the difference between list and tuple?<\/b><\/span><\/p>\n<p>The difference between list and tuple is that list is mutable while tuple is not. Tuple can be<br \/>\nhashed for e.g as a key for dictionaries.<\/p>\n<p><span style=\"font-size: medium;\"><b>9) How are arguments passed by value or by reference?<\/b><\/span><\/p>\n<p>Everything in Python is an object and all variables hold references to the objects. The<br \/>\nreferences values are according to the functions; as a result you cannot change the value of the<br \/>\nreferences. However, you can change the objects if it is mutable.<\/p>\n<p><span style=\"font-size: medium;\"><b>10) What is Dict and List comprehensions are?<\/b><\/span><\/p>\n<p>They are syntax constructions to ease the creation of a Dictionary or List based on existing<br \/>\niterable.<\/p>\n<p><span style=\"font-size: medium;\"><b>11) What are the built-in type does python provides?<\/b><\/span><\/p>\n<p>There are mutable and Immutable types of Pythons built in types Mutable built-in types<\/p>\n<p><span style=\"font-size: large;\"><b>.<\/b><\/span> List&nbsp;<\/p>\n<p><b style=\"font-size: x-large;\">.&nbsp;<\/b>Sets&nbsp;<\/p>\n<p><b style=\"font-size: x-large;\">.&nbsp;<\/b>Dictionaries<\/p>\n<p>Immutable built-in types<\/p>\n<p><b style=\"font-size: x-large;\">.&nbsp;<\/b>Strings&nbsp;<\/p>\n<p><b style=\"font-size: x-large;\">.&nbsp;<\/b>Tuples&nbsp;<\/p>\n<p><b style=\"font-size: x-large;\">.&nbsp;<\/b>Numbers<\/p>\n<p><span style=\"font-size: medium;\"><b>12) What is namespace in Python?<\/b><\/span><\/p>\n<p>In Python, every name introduced has a place where it lives and can be hooked for. This is<br \/>\nknown as namespace. It is like a box where a variable name is mapped to the object placed.<br \/>\n Whenever the variable is searched out, this box will be searched, to get corresponding object.<\/p>\n<p><span style=\"font-size: medium;\"><b>13) What is lambda in Python?<\/b><\/span><\/p>\n<p>It is a single expression anonymous function often used as inline function.<\/p>\n<p><span style=\"font-size: medium;\"><b>14) Why lambda forms in python does not have statements?<\/b><\/span><\/p>\n<p>A lambda form in python does not have statements as it is used to make new function object<br \/>\nand then return them at runtime.<\/p>\n<p><span style=\"font-size: medium;\"><b>15) What is pass in Python?<\/b><\/span><\/p>\n<p>Pass means, no-operation Python statement, or in other words it is a place holder in compound<br \/>\nstatement, where there should be a blank left and nothing has to be written there.<\/p>\n<p><span style=\"font-size: medium;\"><b>16) In Python what are iterators?<\/b><\/span><\/p>\n<p>In Python, iterators are used to iterate a group of elements, containers like list.<\/p>\n<p><span style=\"font-size: medium;\"><b>17) What is unit test in Python?<\/b><\/span><\/p>\n<p>A unit testing framework in Python is known as unittest. It supports sharing of setups,<br \/>\nautomation testing, shutdown code for tests, aggregation of tests into collections etc.<\/p>\n<p><span style=\"font-size: medium;\"><b>18) In Python what is slicing?<\/b><\/span><\/p>\n<p>A mechanism to select a range of items from sequence types like list, tuple, strings etc. is<br \/>\nknown as slicing.<\/p>\n<p><span style=\"font-size: medium;\"><b>19) What are generators in Python?<\/b><\/span><\/p>\n<p>The way of implementing iterators are known as generators. It is a normal function except that<br \/>\nit yields expression in the function.<\/p>\n<p><span style=\"font-size: medium;\"><b>20) What is docstring in Python?<\/b><\/span><\/p>\n<p>A Python documentation string is known as docstring, it is a way of documenting Python<\/p>\n<p>functions, modules and classes.<\/p>\n<p><span style=\"font-size: medium;\"><b>21) How can you copy an object in Python?<\/b><\/span><\/p>\n<p>To copy an object in Python, you can try copy.copy () or copy.deepcopy() for the general case.<br \/>\nYou cannot copy all objects but most of them.<\/p>\n<p><span style=\"font-size: medium;\"><b>22) What is negative index in Python?<\/b><\/span><\/p>\n<p>Python sequences can be index in positive and negative numbers. For positive index, 0 is the<br \/>\nfirst index, 1 is the second index and so forth. For negative index, (-1) is the last index and (-2)<br \/>\nis the second last index and so forth.<\/p>\n<p><span style=\"font-size: medium;\"><b>23) How you can convert a number to a string?<\/b><\/span><\/p>\n<p>In order to convert a number into a string, use the inbuilt function str(). If you want a octal or<br \/>\nhexadecimal representation, use the inbuilt function oct() or hex().<\/p>\n<p><span style=\"font-size: medium;\"><b>24) What is the difference between Xrange and range?<\/b><\/span><\/p>\n<p>Xrange returns the xrange object while range returns the list, and uses the same memory and<br \/>\nno matter what the range size is.<\/p>\n<p><span style=\"font-size: medium;\"><b>25) What is module and package in Python?<\/b><\/span><\/p>\n<p>In Python, module is the way to structure program. Each Python program file is a module, which<br \/>\nimports other modules like objects and attributes.<\/p>\n<p>The folder of Python program is a package of modules. A package can have modules or<br \/>\nsubfolders.<\/p>\n<p><span style=\"font-size: medium;\"><b>26) Mention what are the rules for local and global variables in Python?<\/b><\/span><\/p>\n<p><b>Local variables:<\/b> If a variable is assigned a new value anywhere within the function&#8217;s body, it&#8217;s<br \/>\nassumed to be local.<\/p>\n<p><b>Global variables:<\/b> Those variables that are only referenced inside a function are implicitly<br \/>\nglobal.<\/p>\n<p><span style=\"font-size: medium;\"><b>27) How can you share global variables across modules?<\/b><\/span><\/p>\n<p>To share global variables across modules within a single program, create a special module.<br \/>\nImport the config module in all modules of your application. The module will be available as a<br \/>\nglobal variable across modules.<\/p>\n<p><span style=\"font-size: medium;\"><b>28) Explain how can you make a Python Script executable on Unix?<\/b><\/span><\/p>\n<p>To make a Python Script executable on Unix, you need to do two things,&nbsp;<\/p>\n<p><span style=\"font-size: large;\"><b>. <\/b><\/span>Script file&#8217;s mode must be executable and&nbsp;<\/p>\n<p><b style=\"font-size: x-large;\">.&nbsp;&nbsp;<\/b>the first line must begin with # ( #!\/usr\/local\/bin\/python)<\/p>\n<p><b><span style=\"font-size: medium;\">29) Explain how to delete a file in Python?<\/span><\/b><\/p>\n<p>By using a command os.remove (filename) or os.unlink(filename)<\/p>\n<p><span style=\"font-size: medium;\"><b>30) Explain how can you generate random numbers in Python?<\/b><\/span><\/p>\n<p>To generate random numbers in Python, you need to import command as<\/p>\n<p>&nbsp;import random&nbsp;<\/p>\n<p>random.random()&nbsp;<\/p>\n<p>This returns a random floating point number in the range [0,1)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Top 30&nbsp; Python Interview Questions &amp; Answers 1) What is Python? What are the&hellip;<\/p>\n","protected":false},"author":2,"featured_media":93,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":"","_joinchat":[]},"categories":[6],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/posts\/25"}],"collection":[{"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/comments?post=25"}],"version-history":[{"count":1,"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/posts\/25\/revisions"}],"predecessor-version":[{"id":94,"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/posts\/25\/revisions\/94"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/media\/93"}],"wp:attachment":[{"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/media?parent=25"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/categories?post=25"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/tags?post=25"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}