{"id":75540,"date":"2020-09-02T16:08:30","date_gmt":"2020-09-02T10:38:30","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=75540"},"modified":"2020-09-02T16:08:30","modified_gmt":"2020-09-02T10:38:30","slug":"python-interview-questions-and-answers","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-interview-questions-and-answers\/","title":{"rendered":"Python Interview Questions and Answers to Make you Job-Ready"},"content":{"rendered":"<p>Since you have crossed the beginners&#8217; level now its time to move one step ahead and level-up your skills and knowledge with the help of these advanced Python interview questions and answers for intermediates. As we all know that Python is the most preferred programming language in today&#8217;s world and has great career opportunities, this is the right time to step ahead of others and get ready for the technical round.<\/p>\n<p>Here TechVidvan is providing you a series of 75+ Python Interview Questions and Answers in three parts:<\/p>\n<ul>\n<li><a href=\"https:\/\/techvidvan.com\/tutorials\/python-interview-questions\/\">Python Interview Questions and Answers for Beginners<\/a><\/li>\n<li>Python Interview Questions and Answers for Intermediates<\/li>\n<li><a href=\"https:\/\/techvidvan.com\/tutorials\/python-programming-interview-questions\/\">Python Interview Questions and Answers for Experts<\/a><\/li>\n<\/ul>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/Python-interview-questions1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-79762\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/Python-interview-questions1.jpg\" alt=\"Python interview questions and answers\" width=\"1200\" height=\"628\" \/><\/a><\/p>\n<h2>Python Interview Questions and Answers for Intermediates<\/h2>\n<p>These Python interview questions and answers mainly focus on intermediates. Here we provide you top Python interview questions including some advanced and technical questions also with their answers which will definitely help you to crack your next interview. So, let&#8217;s get started.<\/p>\n<h3>Top Python Interview Questions and Answers<\/h3>\n<p><strong>Q.1. How do you define data types in Python? How much memory does an integer hold?<\/strong><\/p>\n<p>You do not define data types in Python. It determines this at runtime based on the values of the underlying expressions.<\/p>\n<p><strong>Q.2. What are *args and **kwargs? Why do we call them so?<\/strong><\/p>\n<p>What if until runtime, you don\u2019t know how many arguments your function will take? That could depend on the user, and hence, you must wait until runtime. You can define your function with *args or **kwargs as the list of parameters. *args can then take any number of arguments, and **kwargs will take any number of keyword arguments. Let\u2019s take an example.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def add(*args):\n  return sum([arg for arg in args])<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; add(1,2,3)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">6<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; add(1,2,3,4)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">10<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; add(1,2,3.0)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">6.0<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">def sayhello(**kwargs):\n  for key,value in kwargs.items():\n    print(value)\n\n&gt;&gt;&gt; sayhello(fname=\"Ayushi\",lname=\"Sharma\")<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Ayushi<br \/>\nSharma<\/div>\n<p>We call them *args and **kwargs for arguments and keyword arguments respectively, but we can call them anything else we want.<\/p>\n<p><strong>Q.3. How is shallow copying different from deep copying?<\/strong><\/p>\n<p>Shallow copying creates a new object &#8211; this holds the same objects as the original. So, any changes we make to this copy are visible in the original object as well. In deep copying, the new object holds copies of all the objects in the original, and so, any changes to these do not affect values in the original object.<\/p>\n<p>To perform a shallow\/deep copy:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; import copy\n&gt;&gt;&gt; a=[1,2,3]\n&gt;&gt;&gt; b=copy.copy(a)\n&gt;&gt;&gt; c=copy.deepcopy(a)<\/pre>\n<p><strong>Q.4. What are match() and search()?<\/strong><\/p>\n<p>match() and search() are functions of the re module (for regular expressions) in Python. But they are slightly different. match() checks for a pattern match at the beginning of a string and search() checks for it in the entire string. Both return a Match object if found, else None.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; import re\n&gt;&gt;&gt; re.match('location','relocation')\n&gt;&gt;&gt; re.search('location','relocation')\\<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&lt;re.Match object; span=(2, 10), match=&#8217;location&#8217;&gt;<\/div>\n<p><strong>Q.5. What does the following code output?<\/strong><\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">'I like to watch movies'.isalnum()<\/pre>\n<p>In the interpreter, this prints False. It looks alphanumeric but isn\u2019t. If it had a digit as well, it would return True.<\/p>\n<p><strong>Q.6. What is fabs()?<\/strong><\/p>\n<p>Let\u2019s first talk about abs(). This is a <a href=\"https:\/\/techvidvan.com\/tutorials\/python-built-in-functions\/\"><em><strong>built-in function<\/strong> <\/em><\/a>that returns the absolute value of the argument.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; abs(2-3)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">1<\/div>\n<p>fabs() is a built-in function in the math module. It gives us the absolute value of a float argument.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; fabs(2.1-3.7)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">1.6<\/div>\n<p><strong>Q.7. What is a suite in Python?<\/strong><\/p>\n<p>The suite is another name for a group of statements controlled by a clause. And the clause header starts with a uniquely identifying keyword and ends with a colon. In the following code, the part under <em>if<\/em> is the suite:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; if 2&gt;1:\n  print(\"Ok\")\n  print(\"Bye\")<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Ok<br \/>\nBye<\/div>\n<p>That applies for this one too:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; if 2&gt;1: print(\"Ok\"); print(\"Bye\")<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Ok<br \/>\nBye<\/div>\n<p>But this is invalid:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; if 2&gt;1: if 2&gt;-1: print(\"Ok\")<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">SyntaxError: invalid syntax<\/div>\n<p>Nesting is not possible for suites on the same lines as the clauses.<\/p>\n<p><strong>Q.8. How will you get the current time with Python?<\/strong><\/p>\n<p>I will use the time module for this:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; import time\n&gt;&gt;&gt; time.localtime(time.time())<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">time.struct_time(tm_year=2019, tm_mon=9, tm_mday=30, tm_hour=11, tm_min=43, tm_sec=51, tm_wday=0, tm_yday=273, tm_isdst=0)<\/div>\n<p>Here, the time() function returns the current time in seconds since the Epoch, and localtime() converts it into a time <a href=\"https:\/\/techvidvan.com\/tutorials\/python-tuples\/\"><em><strong>tuple<\/strong><\/em><\/a> representing the local time. But a more efficient way to do this is:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; time.localtime()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">time.struct_time(tm_year=2019, tm_mon=9, tm_mday=30, tm_hour=11, tm_min=45, tm_sec=41, tm_wday=0, tm_yday=273, tm_isdst=0)<\/div>\n<h3>Advanced Python Interview Questions and Answers<\/h3>\n<p>Now, let&#8217;s discuss some advanced Python Interview Questions and answers.<\/p>\n<p><strong>Q.9. Do you prefer Tkinter or PyQt?<\/strong><\/p>\n<p>Both Tkinter and PyQt are good GUI development libraries for Python. There are some reasons why you should go for PyQt over Tkinter:<\/p>\n<ul>\n<li>It will give you some experience with Qt. If you then shift to some other technology, you will still be able to use it.<\/li>\n<li>It is one of the best cross-platform interface toolkits.<\/li>\n<\/ul>\n<p>However, sometimes, you may want to go for Tkinter:<\/p>\n<ul>\n<li>PyQT is under the GPL license. So if you release your code, it should be under a compatible license. Go for Tkinter if you don&#8217;t want to spend money on this.<\/li>\n<li>Also, Tkinter ships with Python. You will have to install <a href=\"https:\/\/wiki.python.org\/moin\/PyQt\">PyQt<\/a> if you want it.<\/li>\n<\/ul>\n<p><strong>Q.10. How will you destroy allocated memory in Python?<\/strong><\/p>\n<p>We do not need to explicitly deallocate memory in Python. Like in Java, the garbage collector takes care of allocating and deallocating memory. Memory leaks don&#8217;t happen often.<\/p>\n<p>Python implements reference counting &#8211; it counts how often an object is referenced by other objects in the system. Each time a reference to an object is removed, its reference count is decremented. And when this becomes 0, the object is deallocated. Python runs its garbage collector when the difference between the number of allocations and the number of deallocations is greater than the threshold number.<\/p>\n<p><strong>Q.11. What is the self parameter?<\/strong><\/p>\n<p>Every method in a class takes the self parameter. This tells it to work on the current object of the class. When we talk about the properties of the current object of a class, we prefix the self parameter to it. We can call this anything else we want.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; class Fruit:\n  def __init__(self,shape):\n    self.shape='round'\n  def show(self):\n    print(self.shape)<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; oj=Fruit('round')\n&gt;&gt;&gt; oj.show()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">round<\/div>\n<p><strong>Q.12. Print the following star pattern:<\/strong><\/p>\n<p><strong> \u00a0 \u00a0*<\/strong><br \/>\n<strong>\u00a0\u00a0* *<\/strong><br \/>\n<strong>\u00a0* * *<\/strong><br \/>\n<strong>* * * *<\/strong><br \/>\n<strong>\u00a0* * *<\/strong><br \/>\n<strong>\u00a0\u00a0* *<\/strong><br \/>\n<strong>\u00a0\u00a0\u00a0*<\/strong><\/p>\n<p><strong>Code: To print the star pattern:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">def star():\n  for row in range(4):\n    for space in range(3-row,0,-1):\n      print(' ',end='')\n    for star in range(row+1):\n      print('* ',end='')\n    print()\n  for row in range(3):\n    for space in range(row+1):\n      print(' ',end='')\n    for star in range(3-row):\n      print('* ',end='')\n    print()\n\nstar()<\/pre>\n<p><strong>Q.13. Tell me one interesting thing about the Python interpreter.<\/strong><\/p>\n<p>In the interpreter, suppose we add the numbers 2 and 3.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 2+3<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">5<\/div>\n<p>Then, if we want to use this result and operate on that, we can use the _ (underscore) in its place.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; _*3<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">15<\/div>\n<p><strong>Q.14. Write a function to check whether a number is an Armstrong number.<\/strong><\/p>\n<p>Considering Armstrong numbers are those whose digits\u2019 cubes sum up to the original number, let\u2019s take an example.<\/p>\n<p>153 = 1<sup>3<\/sup> + 5<sup>3<\/sup> + 3<sup>3<\/sup><\/p>\n<p><strong>Q.15. What is enumerate?<\/strong><\/p>\n<p>enumerate() is a built-in function. The enumerate object from the enumerate class gives us pairs with a count starting from 0, along with a value yielded by the iterable. Let\u2019s take an example.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">for index, value in enumerate(['Milk','eggs','cheese']):\n  print(index, value)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">0 Milk<br \/>\n1 eggs<br \/>\n2 cheese<\/div>\n<p><strong>Q.16. How is remove() different from del?<\/strong><\/p>\n<p>remove() is a built-in method on lists in Python. It lets us delete the first occurrence of an object in a <a href=\"https:\/\/techvidvan.com\/tutorials\/python-lists\/\"><em><strong>list<\/strong><\/em><\/a>. To delete an object at a certain index, we can use the del keyword or the pop() method.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list=[4,9,27,34,12,9,34]\n&gt;&gt;&gt; list.remove(34)\n&gt;&gt;&gt; list<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[4, 9, 27, 12, 9, 34]<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; del list[2]\n&gt;&gt;&gt; list<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[4, 9, 12, 9, 34]<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list.pop(2)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">12<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[4, 9, 9, 34]<\/div>\n<p><strong>Q.17. Can you overload constructors in Python?<\/strong><\/p>\n<p>If we overload the __init__ method of the Fruit class, creating an object of the first kind raises an error.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; class Fruit:\n  def __init__(self,shape):\n    self.shape=shape\n  def __init__(self,shape,color):\n    self.shape=shape\n    self.color=color<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; orange=Fruit('round','orange')\n&gt;&gt;&gt; apple=Fruit('round')<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\n<span style=\"font-weight: 400\">\u00a0\u00a0<\/span>File &#8220;&lt;pyshell#200&gt;&#8221;, line 1, in &lt;module&gt;<br \/>\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span>apple=Fruit(&#8217;round&#8217;)<br \/>\nTypeError: __init__() missing 1 required positional argument: &#8216;color&#8217;<\/div>\n<p>This is because there are not 2 __init__ methods- the second one exists, but the first one doesn\u2019t.<\/p>\n<p><strong>Q.18. Can you count each item\u2019s occurrences in a list without mentioning it?<\/strong><\/p>\n<p>We can turn the list into a set to avoid duplicates, then for each number in it, create a tuple from the number and its count. Put this in a <a href=\"https:\/\/techvidvan.com\/tutorials\/python-list-comprehension\/\"><em><strong>list comprehension<\/strong><\/em><\/a> and return the list.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def count_in_list(list):\n  return [(num,list.count(num)) for num in set(list)]<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; count_in_list([1,2,4,1,4,1,7,9,5])<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[(1, 3), (2, 1), (4, 2), (5, 1), (7, 1), (9, 1)]<\/div>\n<p><strong>Q.19. List some common commands of the Python debugger.<\/strong><\/p>\n<p>The Python debugger (pdb) has many commands. Some are:<\/p>\n<ul>\n<li>b &#8211; Add breakpoint<\/li>\n<li>c &#8211; Continue execution<\/li>\n<li>l &#8211; List entire source code<\/li>\n<li>n &#8211; Move to next line<\/li>\n<li>p &#8211; Print an expression<\/li>\n<li>s &#8211; Execute next step<\/li>\n<\/ul>\n<h3>Technical Python Interview Questions and Answers<\/h3>\n<p>These are some frequently asked technical Python interview questions and answers. Let&#8217;s have a look:<\/p>\n<p><strong>Q.20. What are static variables?<\/strong><\/p>\n<p>A static variable is a class variable &#8211; it belongs to a class.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; class Fruit:\n  name='fruit'\n  def __init__(self, color):\n    self.color=color<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; orange=Fruit('orange')\n&gt;&gt;&gt; orange.name<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&#8216;fruit&#8217;<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; Fruit.name<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&#8216;fruit&#8217;<\/div>\n<p><strong>Q.21. How will you assign values to class attributes?<\/strong><\/p>\n<p>We can do this in two ways &#8211; either while declaring the class or at runtime. This is how we do it at runtime:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; class Fruit:\n  pass<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; Fruit.name='fruit'\n&gt;&gt;&gt; Fruit.name<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&#8216;fruit\u2019<\/div>\n<p>This is called declaring attributes on the fly.<\/p>\n<p><strong>Q.22. Can you write a function to convert a string of text in title case?<\/strong><\/p>\n<p>This can easily be done using the title() built-in function.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def to_title_case(text):\n  return text.title()<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; to_title_case('planet of the apes')<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&#8216;Planet Of The Apes&#8217;<\/div>\n<p><strong>Q.23. If you are given a string of text, how will you replace the spaces with periods? Also, add a period at the end.<\/strong><\/p>\n<p>There are two ways to do this:<\/p>\n<p><strong>1. Using join()<\/strong><\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; print('.'.join('How are you?'.split(' '))+'.')<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">How.are.you?.<\/div>\n<p><strong>2. Using end<\/strong><\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; for word in 'How are you?'.split(' '):\n  print(word,end='.')<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">How.are.you?.<\/div>\n<p><strong>Q.24. What can follow a try-except block?<\/strong><\/p>\n<p>After a try-except block, there can be an else-clause or a finally-clause. If the code under the try-block does not raise an exception, the code under the else-clause runs. And the code under finally runs regardless of whether an exception occurred.<\/p>\n<p><strong>Q.25. What does the following code output?<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list=[1,2,3]\n&gt;&gt;&gt; list[4]\n&gt;&gt;&gt; list[4:]<\/pre>\n<p>The second line raises an IndexError because there is no index 4 on this list. But the slice does not raise an IndexError &#8211; it returns an empty list.<\/p>\n<p><strong>Q.26. How will you check the memory usage of an object?<\/strong><\/p>\n<p>The getsizeof() function gives us the sizes of an object in bytes.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; sys.getsizeof(7)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">28<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; sys.getsizeof(2)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">28<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; sys.getsizeof(2.5)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">24<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; sys.getsizeof(True)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">28<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; class Fruit:\n  pass<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; orange=Fruit()\n&gt;&gt;&gt; sys.getsizeof(orange)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">56<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def sayhi():\n  print(\"Hi\")<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; sys.getsizeof(sayhi)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">136<\/div>\n<p><strong>Q.27. How will you specify path to a file in the parent of the current working directory?<\/strong><\/p>\n<p>If the current working directory is:<\/p>\n<p>\u2018C:\\\\Users\\\\DataFlair\\\\Desktop\\\\one\u2019<\/p>\n<p>Suppose there are two folders on the Desktop- \u2018one\u2019 and \u2018two. Each have a file text.txt. Then, the path to text.txt in two is:<\/p>\n<p>\u2018..\\two\\\\text.txt\u2019<\/p>\n<h3>Open-Ended Python Interview Questions<\/h3>\n<p><strong>Q.28. Why we should hire you?<\/strong><\/p>\n<p><strong>Q.29. What do you expect from us?<\/strong><\/p>\n<p><strong>Q.30. Do you have any previous work experience?<\/strong><\/p>\n<p><strong>Q.31. What do you think about the future of Python programming language?<\/strong><\/p>\n<p><strong>Q.32. What motivates you to do things better than others?<\/strong><\/p>\n<p><strong>Q.33. Why are you interested in our company?<\/strong><\/p>\n<p><strong>Q.34. Describe what is your ideal job?<\/strong><\/p>\n<p><strong>Q.35. Tell me about the projects you have worked on? What approaches you have taken to complete that project?<\/strong><\/p>\n<h2>Summary<\/h2>\n<p>This was all about TechVidvan&#8217;s Python interview questions and answers for intermediates. In this, we have covered top Python interview questions and some open-ended, advanced, and technical Python interview questions and answers with the help of examples which will easily help you to crack your next interview.<\/p>\n<p>If you find this tutorial helpful, do share it on social media with others also.<\/p>\n<p>All the best for your next interview!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since you have crossed the beginners&#8217; level now its time to move one step ahead and level-up your skills and knowledge with the help of these advanced Python interview questions and answers for intermediates.&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":79762,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[3217,1435,3218,1436,3219,3220,1441],"class_list":["post-75540","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-advanced-python-interview-questions-and-answers","tag-python-interview-questions","tag-python-interview-questions-advanced","tag-python-interview-questions-and-answers","tag-python-technical-interview-questions-and-answers","tag-technical-python-interview-questions-and-answers","tag-top-python-interview-questions-and-answers"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Interview Questions and Answers to Make you Job-Ready - TechVidvan<\/title>\n<meta name=\"description\" content=\"Practice these Python interview questions and answers for your next interview to step ahead of others in this race and crack your next interview.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/python-interview-questions-and-answers\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Interview Questions and Answers to Make you Job-Ready - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Practice these Python interview questions and answers for your next interview to step ahead of others in this race and crack your next interview.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-interview-questions-and-answers\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-09-02T10:38:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/02\/Python-interview-questions1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Interview Questions and Answers to Make you Job-Ready - TechVidvan","description":"Practice these Python interview questions and answers for your next interview to step ahead of others in this race and crack your next interview.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/python-interview-questions-and-answers\/","og_locale":"en_US","og_type":"article","og_title":"Python Interview Questions and Answers to Make you Job-Ready - TechVidvan","og_description":"Practice these Python interview questions and answers for your next interview to step ahead of others in this race and crack your next interview.","og_url":"https:\/\/techvidvan.com\/tutorials\/python-interview-questions-and-answers\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-09-02T10:38:30+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/02\/Python-interview-questions1.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-interview-questions-and-answers\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-interview-questions-and-answers\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Python Interview Questions and Answers to Make you Job-Ready","datePublished":"2020-09-02T10:38:30+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-interview-questions-and-answers\/"},"wordCount":1871,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-interview-questions-and-answers\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/02\/Python-interview-questions1.jpg","keywords":["Advanced Python Interview Questions and Answers","python interview questions","python interview questions advanced","python interview questions and answers","python technical interview questions and answers","Technical Python Interview Questions and Answers","top python interview questions and answers"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-interview-questions-and-answers\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-interview-questions-and-answers\/","url":"https:\/\/techvidvan.com\/tutorials\/python-interview-questions-and-answers\/","name":"Python Interview Questions and Answers to Make you Job-Ready - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-interview-questions-and-answers\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-interview-questions-and-answers\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/02\/Python-interview-questions1.jpg","datePublished":"2020-09-02T10:38:30+00:00","description":"Practice these Python interview questions and answers for your next interview to step ahead of others in this race and crack your next interview.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-interview-questions-and-answers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-interview-questions-and-answers\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-interview-questions-and-answers\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/02\/Python-interview-questions1.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/02\/Python-interview-questions1.jpg","width":1200,"height":628,"caption":"Python interview questions and answers"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-interview-questions-and-answers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Python Interview Questions and Answers to Make you Job-Ready"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75540","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=75540"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75540\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/79762"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=75540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=75540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=75540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}