{"id":2711,"date":"2023-12-16T12:39:40","date_gmt":"2023-12-16T12:39:40","guid":{"rendered":"https:\/\/www.kaashivinfotech.com\/blog\/?p=2711"},"modified":"2025-07-19T10:27:38","modified_gmt":"2025-07-19T10:27:38","slug":"intermediate-python-interview-questions-and-answers","status":"publish","type":"post","link":"https:\/\/www.kaashivinfotech.com\/blog\/intermediate-python-interview-questions-and-answers\/","title":{"rendered":"Python intermediate interview questions [Updated]"},"content":{"rendered":"<h2 data-start=\"323\" data-end=\"748\">Deep Dive: Python Intermediate Interview Questions for Career Growth<\/h2>\n<p data-start=\"323\" data-end=\"748\"><strong data-start=\"323\" data-end=\"366\">Python intermediate interview questions<\/strong> are designed to test a candidate\u2019s ability to apply Python concepts beyond the beginner level. If you&#8217;re preparing for technical interviews, it&#8217;s essential to practice not only basic syntax but also focus on <strong data-start=\"575\" data-end=\"608\">python intermediate questions<\/strong> and <a href=\"https:\/\/www.wikitechy.com\/tutorials\/python\/python-interview-question-and-answers\" target=\"_blank\" rel=\"noopener\"><strong data-start=\"613\" data-end=\"653\">python intermediate coding questions<\/strong><\/a> that evaluate your understanding of real-world problem solving and advanced language features.<\/p>\n<p data-start=\"750\" data-end=\"1091\"><a href=\"https:\/\/www.kaashivinfotech.com\/blog\/basic-python-interview-questions-and-answers\/\"><strong data-start=\"750\" data-end=\"760\">Python<\/strong><\/a> is a general-purpose, dynamic, high-level, and interpreted programming language. It supports the object-oriented programming approach, which makes it a popular choice for developing robust applications. Its simple and readable syntax makes it easy to learn, yet its capabilities are powerful enough for enterprise-grade solutions.<\/p>\n<p data-start=\"1093\" data-end=\"1455\">Being an <strong data-start=\"1102\" data-end=\"1126\">interpreted language<\/strong>, Python allows for rapid application development and scripting. Its dynamic typing and clean syntax make it ideal for beginners and professionals alike. However, in <strong data-start=\"1292\" data-end=\"1335\">python intermediate interview questions<\/strong>, you\u2019ll often encounter topics that go deeper\u2014like decorators, list comprehensions, exception handling, and generators.<\/p>\n<p data-start=\"1644\" data-end=\"1906\">This versatility is reflected in the variety of <strong data-start=\"1692\" data-end=\"1725\">python intermediate questions<\/strong> asked during interviews. You may be asked to build classes, implement interfaces, or manipulate data using functional techniques like <code class=\"\" data-line=\"\">map()<\/code>, <code class=\"\" data-line=\"\">filter()<\/code>, and <code class=\"\" data-line=\"\">lambda<\/code> expressions.<\/p>\n<p data-start=\"1908\" data-end=\"2267\">One of the reasons Python stands out is its dynamic nature. You don\u2019t need to specify data types explicitly. For example, simply writing <code class=\"\" data-line=\"\">a = 10<\/code> assigns an integer value to the variable without any type declaration. This flexibility speeds up development but also introduces challenges that are commonly addressed in <strong data-start=\"2226\" data-end=\"2266\">python intermediate coding questions<\/strong>.<\/p>\n<h2>Python Intermediate Coding Questions<\/h2>\n<figure id=\"attachment_8685\" aria-describedby=\"caption-attachment-8685\" style=\"width: 1024px\" class=\"wp-caption aligncenter\"><img fetchpriority=\"high\" decoding=\"async\" class=\"size-full wp-image-8685\" src=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2023\/12\/Python-Intermediate-Interview-Questions.png\" alt=\"Python Intermediate Interview Questions\" width=\"1024\" height=\"1024\" srcset=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2023\/12\/Python-Intermediate-Interview-Questions.png 1024w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2023\/12\/Python-Intermediate-Interview-Questions-300x300.png 300w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2023\/12\/Python-Intermediate-Interview-Questions-150x150.png 150w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2023\/12\/Python-Intermediate-Interview-Questions-768x768.png 768w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2023\/12\/Python-Intermediate-Interview-Questions-72x72.png 72w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2023\/12\/Python-Intermediate-Interview-Questions-144x144.png 144w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2023\/12\/Python-Intermediate-Interview-Questions-332x332.png 332w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2023\/12\/Python-Intermediate-Interview-Questions-664x664.png 664w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2023\/12\/Python-Intermediate-Interview-Questions-688x688.png 688w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2023\/12\/Python-Intermediate-Interview-Questions-24x24.png 24w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2023\/12\/Python-Intermediate-Interview-Questions-48x48.png 48w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2023\/12\/Python-Intermediate-Interview-Questions-96x96.png 96w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption id=\"caption-attachment-8685\" class=\"wp-caption-text\">Python Intermediate Interview Questions<\/figcaption><\/figure>\n<h3 data-start=\"423\" data-end=\"462\"><strong data-start=\"428\" data-end=\"462\">1. What are Python decorators?<\/strong><\/h3>\n<p data-start=\"463\" data-end=\"785\"><strong data-start=\"463\" data-end=\"474\">Answer:<\/strong><br data-start=\"474\" data-end=\"477\" \/>Decorators are a powerful tool in Python used to modify the behavior of a function or class. They are applied using the <code class=\"\" data-line=\"\">@decorator_name<\/code> syntax above the function definition. Internally, a decorator is a higher-order function that takes a function as input and returns a new function with enhanced behavior.<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">def my_decorator(func):<br\/>    def wrapper():<br\/>        print(&quot;Before function execution&quot;)<br\/>        func()<br\/>        print(&quot;After function execution&quot;)<br\/>    return wrapper<br\/><br\/>@my_decorator<br\/>def greet():<br\/>    print(&quot;Hello!&quot;)<br\/><br\/>greet()<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h3 data-start=\"1026\" data-end=\"1084\"><strong data-start=\"1031\" data-end=\"1084\">2. Explain list comprehension and its advantages.<\/strong><\/h3>\n<p data-start=\"1085\" data-end=\"1225\"><strong data-start=\"1085\" data-end=\"1096\">Answer:<\/strong><br data-start=\"1096\" data-end=\"1099\" \/>List comprehension provides a concise way to create lists in Python. It&#8217;s faster and more readable than traditional for-loops.<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">squares = [x*x for x in range(5)]<br\/># Output: [0, 1, 4, 9, 16]<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<p data-start=\"1303\" data-end=\"1314\"><strong>Advantages:<\/strong><\/p>\n<ul data-start=\"1315\" data-end=\"1397\">\n<li data-start=\"1315\" data-end=\"1334\">\n<p data-start=\"1317\" data-end=\"1334\">More compact code<\/p>\n<\/li>\n<li data-start=\"1335\" data-end=\"1355\">\n<p data-start=\"1337\" data-end=\"1355\">Better performance<\/p>\n<\/li>\n<li data-start=\"1356\" data-end=\"1397\">\n<p data-start=\"1358\" data-end=\"1397\">Easier to understand when used properly<\/p>\n<\/li>\n<\/ul>\n<h3 data-start=\"1404\" data-end=\"1471\"><strong data-start=\"1409\" data-end=\"1471\">3. What is the difference between <code class=\"\" data-line=\"\">is<\/code> and <code class=\"\" data-line=\"\">==<\/code> in Python?<\/strong><\/h3>\n<p data-start=\"1472\" data-end=\"1485\"><strong data-start=\"1472\" data-end=\"1483\">Answer:<\/strong><\/p>\n<ul data-start=\"1486\" data-end=\"1645\">\n<li data-start=\"1486\" data-end=\"1553\">\n<p data-start=\"1488\" data-end=\"1553\"><code class=\"\" data-line=\"\">==<\/code> checks <strong data-start=\"1500\" data-end=\"1518\">value equality<\/strong> (whether the values are the same).<\/p>\n<\/li>\n<li data-start=\"1554\" data-end=\"1645\">\n<p data-start=\"1556\" data-end=\"1645\"><code class=\"\" data-line=\"\">is<\/code> checks <strong data-start=\"1568\" data-end=\"1580\">identity<\/strong> (whether the two references point to the same object in memory).<\/p>\n<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">a = [1, 2]<br\/>b = [1, 2]<br\/>print(a == b)  # True<br\/>print(a is b)  # False<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h3 data-start=\"1734\" data-end=\"1773\"><strong data-start=\"1739\" data-end=\"1773\">4. What are Python generators?<\/strong><\/h3>\n<p data-start=\"1774\" data-end=\"1921\"><strong data-start=\"1774\" data-end=\"1785\">Answer:<\/strong><br data-start=\"1785\" data-end=\"1788\" \/>Generators are iterators created using functions and the <code class=\"\" data-line=\"\">yield<\/code> keyword. They are memory-efficient and produce values one at a time.<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">def countdown(n):<br\/>    while n &gt; 0:<br\/>        yield n<br\/>        n -= 1<br\/><br\/>for i in countdown(5):<br\/>    print(i)<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h3 data-start=\"2046\" data-end=\"2094\"><strong data-start=\"2051\" data-end=\"2094\">5. Explain Python\u2019s garbage collection.<\/strong><\/h3>\n<p data-start=\"2095\" data-end=\"2235\"><strong data-start=\"2095\" data-end=\"2106\">Answer:<\/strong><br data-start=\"2106\" data-end=\"2109\" \/>Python has an automated garbage collector that uses reference counting and cyclic garbage collection to free up unused memory.<\/p>\n<ul data-start=\"2237\" data-end=\"2388\">\n<li data-start=\"2237\" data-end=\"2323\">\n<p data-start=\"2239\" data-end=\"2323\">Reference Count: If an object\u2019s reference count drops to 0, it is garbage collected.<\/p>\n<\/li>\n<li data-start=\"2324\" data-end=\"2388\">\n<p data-start=\"2326\" data-end=\"2388\">Cyclic GC: Detects and collects cycles of unreachable objects.<\/p>\n<\/li>\n<\/ul>\n<h3 data-start=\"2395\" data-end=\"2443\"><strong data-start=\"2400\" data-end=\"2443\">6. What is a lambda function in Python?<\/strong><\/h3>\n<p data-start=\"2444\" data-end=\"2553\"><strong data-start=\"2444\" data-end=\"2455\">Answer:<\/strong><br data-start=\"2455\" data-end=\"2458\" \/>Lambda functions are anonymous, single-expression functions defined using the <code class=\"\" data-line=\"\">lambda<\/code> keyword.<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">square = lambda x: x*x<br\/>print(square(5))  # Output: 25<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h3 data-start=\"2629\" data-end=\"2669\">7. What are *args and <strong data-start=\"2658\" data-end=\"2669\">kwargs?<\/strong><\/h3>\n<p data-start=\"2670\" data-end=\"2683\"><strong data-start=\"2670\" data-end=\"2681\">Answer:<\/strong><\/p>\n<ul data-start=\"2684\" data-end=\"2803\">\n<li data-start=\"2684\" data-end=\"2743\">\n<p data-start=\"2686\" data-end=\"2743\"><code class=\"\" data-line=\"\">*args<\/code>: Accepts variable number of positional arguments.<\/p>\n<\/li>\n<li data-start=\"2744\" data-end=\"2803\">\n<p data-start=\"2746\" data-end=\"2803\"><code class=\"\" data-line=\"\">**kwargs<\/code>: Accepts variable number of keyword arguments.<\/p>\n<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">def demo(*args, **kwargs):<br\/>    print(args)<br\/>    print(kwargs)<br\/><br\/>demo(1, 2, 3, a=10, b=20)<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h3 data-start=\"2913\" data-end=\"2987\"><strong data-start=\"2918\" data-end=\"2987\">8. What is the difference between a shallow copy and a deep copy?<\/strong><\/h3>\n<p data-start=\"2988\" data-end=\"3001\"><strong data-start=\"2988\" data-end=\"2999\">Answer:<\/strong><\/p>\n<ul data-start=\"3002\" data-end=\"3150\">\n<li data-start=\"3002\" data-end=\"3081\">\n<p data-start=\"3004\" data-end=\"3081\">Shallow copy creates a new object but inserts references to the same objects.<\/p>\n<\/li>\n<li data-start=\"3082\" data-end=\"3150\">\n<p data-start=\"3084\" data-end=\"3150\">Deep copy creates a new object and recursively copies all objects.<\/p>\n<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">import copy<br\/>a = [[1, 2], [3, 4]]<br\/>shallow = copy.copy(a)<br\/>deep = copy.deepcopy(a)<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<div class=\"contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\">\n<h3 data-start=\"3252\" data-end=\"3305\"><strong data-start=\"3257\" data-end=\"3305\">9. How is exception handling done in Python?<\/strong><\/h3>\n<p data-start=\"3306\" data-end=\"3372\"><strong data-start=\"3306\" data-end=\"3317\">Answer:<\/strong><br data-start=\"3317\" data-end=\"3320\" \/>Using <code class=\"\" data-line=\"\">try<\/code>, <code class=\"\" data-line=\"\">except<\/code>, <code class=\"\" data-line=\"\">else<\/code>, and <code class=\"\" data-line=\"\">finally<\/code> blocks:<\/p>\n<div class=\"contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary\">\n<div class=\"sticky top-9\">\n<div class=\"absolute end-0 bottom-0 flex h-9 items-center pe-2\">\n<div class=\"bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs\">\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">try:<br\/>    x = 1 \/ 0<br\/>except ZeroDivisionError:<br\/>    print(&quot;Cannot divide by zero.&quot;)<br\/>finally:<br\/>    print(&quot;Always runs.&quot;)<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\">\n<h3 data-start=\"3510\" data-end=\"3559\"><strong data-start=\"3515\" data-end=\"3559\">10. What is a Python module and package?<\/strong><\/h3>\n<p data-start=\"3560\" data-end=\"3571\"><strong data-start=\"3560\" data-end=\"3571\">Answer:<\/strong><\/p>\n<ul data-start=\"3572\" data-end=\"3717\">\n<li data-start=\"3572\" data-end=\"3638\">\n<p data-start=\"3574\" data-end=\"3638\"><strong data-start=\"3574\" data-end=\"3584\">Module<\/strong>: A <code class=\"\" data-line=\"\">.py<\/code> file with Python definitions and statements.<\/p>\n<\/li>\n<li data-start=\"3639\" data-end=\"3717\">\n<p data-start=\"3641\" data-end=\"3717\"><strong data-start=\"3641\" data-end=\"3652\">Package<\/strong>: A directory containing <code class=\"\" data-line=\"\">__init__.py<\/code> file and multiple modules.<\/p>\n<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># mymodule.py<br\/>def hello():<br\/>    print(&quot;Hello from module&quot;)<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 data-start=\"3842\" data-end=\"3938\"><strong data-start=\"3847\" data-end=\"3938\">11. What is the difference between <code class=\"\" data-line=\"\">classmethod<\/code>, <code class=\"\" data-line=\"\">staticmethod<\/code>, and instance methods?<\/strong><\/h3>\n<p data-start=\"3939\" data-end=\"3950\"><strong data-start=\"3939\" data-end=\"3950\">Answer:<\/strong><\/p>\n<ul data-start=\"3951\" data-end=\"4182\">\n<li data-start=\"3951\" data-end=\"4033\">\n<p data-start=\"3953\" data-end=\"4033\"><code class=\"\" data-line=\"\">@classmethod<\/code> takes <code class=\"\" data-line=\"\">cls<\/code> as the first argument and can access class variables.<\/p>\n<\/li>\n<li data-start=\"4034\" data-end=\"4116\">\n<p data-start=\"4036\" data-end=\"4116\"><code class=\"\" data-line=\"\">@staticmethod<\/code> doesn&#8217;t take <code class=\"\" data-line=\"\">self<\/code> or <code class=\"\" data-line=\"\">cls<\/code> and behaves like a normal function.<\/p>\n<\/li>\n<li data-start=\"4117\" data-end=\"4182\">\n<p data-start=\"4119\" data-end=\"4182\">Instance methods take <code class=\"\" data-line=\"\">self<\/code> and can access instance variables.<\/p>\n<\/li>\n<\/ul>\n<h3 data-start=\"4189\" data-end=\"4239\"><strong data-start=\"4194\" data-end=\"4239\">12. What is the purpose of <code class=\"\" data-line=\"\">__init__.py<\/code>?<\/strong><\/h3>\n<p data-start=\"4240\" data-end=\"4359\"><strong data-start=\"4240\" data-end=\"4251\">Answer:<\/strong><br data-start=\"4251\" data-end=\"4254\" \/>It marks a directory as a Python package and allows imports from it. It can also run initialization code.<\/p>\n<h3 data-start=\"4366\" data-end=\"4413\"><strong data-start=\"4371\" data-end=\"4413\">13. What is monkey patching in Python?<\/strong><\/h3>\n<p data-start=\"4414\" data-end=\"4497\"><strong data-start=\"4414\" data-end=\"4425\">Answer:<\/strong><br data-start=\"4425\" data-end=\"4428\" \/>Monkey patching is dynamically changing a class or module at runtime.<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">import math<br\/>math.sqrt = lambda x: 42<br\/>print(math.sqrt(9))  # Output: 42<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h3 data-start=\"4590\" data-end=\"4635\"><strong data-start=\"4595\" data-end=\"4635\">14. What are Python\u2019s magic methods?<\/strong><\/h3>\n<p data-start=\"4636\" data-end=\"4796\"><strong data-start=\"4636\" data-end=\"4647\">Answer:<\/strong><br data-start=\"4647\" data-end=\"4650\" \/>Magic methods (dunder methods) begin and end with double underscores, like <code class=\"\" data-line=\"\">__init__<\/code>, <code class=\"\" data-line=\"\">__str__<\/code>, <code class=\"\" data-line=\"\">__len__<\/code>, etc., and define behavior of objects.<\/p>\n<h3 data-start=\"4803\" data-end=\"4850\"><strong data-start=\"4808\" data-end=\"4850\">15. How do you handle files in Python?<\/strong><\/h3>\n<p data-start=\"4851\" data-end=\"4923\"><strong data-start=\"4851\" data-end=\"4862\">Answer:<\/strong><br data-start=\"4862\" data-end=\"4865\" \/>Using <code class=\"\" data-line=\"\">open()<\/code>, <code class=\"\" data-line=\"\">read()<\/code>, <code class=\"\" data-line=\"\">write()<\/code>, and context managers:<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">with open(&#039;file.txt&#039;, &#039;r&#039;) as f:<br\/>    content = f.read()<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h3 data-start=\"5001\" data-end=\"5049\"><strong data-start=\"5006\" data-end=\"5049\">16. How do you manage memory in Python?<\/strong><\/h3>\n<p data-start=\"5050\" data-end=\"5091\"><strong data-start=\"5050\" data-end=\"5061\">Answer:<\/strong><br data-start=\"5061\" data-end=\"5064\" \/>Python handles memory with:<\/p>\n<ul data-start=\"5092\" data-end=\"5173\">\n<li data-start=\"5092\" data-end=\"5112\">\n<p data-start=\"5094\" data-end=\"5112\">Reference counting<\/p>\n<\/li>\n<li data-start=\"5113\" data-end=\"5143\">\n<p data-start=\"5115\" data-end=\"5143\">Automatic garbage collection<\/p>\n<\/li>\n<li data-start=\"5144\" data-end=\"5173\">\n<p data-start=\"5146\" data-end=\"5173\">Memory pools (via PyMalloc)<\/p>\n<\/li>\n<\/ul>\n<h3 data-start=\"5180\" data-end=\"5249\"><strong data-start=\"5185\" data-end=\"5249\">17. What is the difference between <code class=\"\" data-line=\"\">@property<\/code> and a method?<\/strong><\/h3>\n<p data-start=\"5250\" data-end=\"5325\"><strong data-start=\"5250\" data-end=\"5261\">Answer:<\/strong><br data-start=\"5261\" data-end=\"5264\" \/><code class=\"\" data-line=\"\">@property<\/code> allows a method to be accessed like an attribute.<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">class Circle:<br\/>    def __init__(self, radius):<br\/>        self._radius = radius<br\/><br\/>    @property<br\/>    def area(self):<br\/>        return 3.14 * self._radius ** 2<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h3 data-start=\"5498\" data-end=\"5544\"><strong data-start=\"5503\" data-end=\"5544\">18. What is a Python context manager?<\/strong><\/h3>\n<p data-start=\"5545\" data-end=\"5673\"><strong data-start=\"5545\" data-end=\"5556\">Answer:<\/strong><br data-start=\"5556\" data-end=\"5559\" \/>Used to manage resources (files, sockets) with <code class=\"\" data-line=\"\">with<\/code> statement. Automatically handles <code class=\"\" data-line=\"\">__enter__<\/code> and <code class=\"\" data-line=\"\">__exit__<\/code>.<\/p>\n<h3 data-start=\"5680\" data-end=\"5740\"><strong data-start=\"5685\" data-end=\"5740\">19. Explain Python\u2019s GIL (Global Interpreter Lock).<\/strong><\/h3>\n<p data-start=\"5741\" data-end=\"5877\"><strong data-start=\"5741\" data-end=\"5752\">Answer:<\/strong><br data-start=\"5752\" data-end=\"5755\" \/>GIL allows only one thread to execute Python bytecode at a time. It limits multithreading but is needed for memory safety.<\/p>\n<h3 data-start=\"5884\" data-end=\"5932\"><strong data-start=\"5889\" data-end=\"5932\">20. How to use map, filter, and reduce?<\/strong><\/h3>\n<p data-start=\"5933\" data-end=\"5944\"><strong data-start=\"5933\" data-end=\"5944\">Answer:<\/strong><\/p>\n<ul data-start=\"5945\" data-end=\"6095\">\n<li data-start=\"5945\" data-end=\"5987\">\n<p data-start=\"5947\" data-end=\"5987\"><code class=\"\" data-line=\"\">map()<\/code> applies a function to all items.<\/p>\n<\/li>\n<li data-start=\"5988\" data-end=\"6036\">\n<p data-start=\"5990\" data-end=\"6036\"><code class=\"\" data-line=\"\">filter()<\/code> selects items based on a condition.<\/p>\n<\/li>\n<li data-start=\"6037\" data-end=\"6095\">\n<p data-start=\"6039\" data-end=\"6095\"><code class=\"\" data-line=\"\">reduce()<\/code> (from functools) applies cumulative function.<\/p>\n<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">from functools import reduce<br\/>reduce(lambda x, y: x+y, [1,2,3])  # Output: 6<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h3 data-start=\"6193\" data-end=\"6249\"><strong data-start=\"6198\" data-end=\"6249\">21. Difference between <code class=\"\" data-line=\"\">sort()<\/code> and <code class=\"\" data-line=\"\">sorted()<\/code>?<\/strong><\/h3>\n<p data-start=\"6250\" data-end=\"6261\"><strong data-start=\"6250\" data-end=\"6261\">Answer:<\/strong><\/p>\n<ul data-start=\"6262\" data-end=\"6340\">\n<li data-start=\"6262\" data-end=\"6300\">\n<p data-start=\"6264\" data-end=\"6300\"><code class=\"\" data-line=\"\">sort()<\/code> modifies the list in-place.<\/p>\n<\/li>\n<li data-start=\"6301\" data-end=\"6340\">\n<p data-start=\"6303\" data-end=\"6340\"><code class=\"\" data-line=\"\">sorted()<\/code> returns a new sorted list.<\/p>\n<\/li>\n<\/ul>\n<h3 data-start=\"6347\" data-end=\"6397\"><strong data-start=\"6352\" data-end=\"6397\">22. What is the purpose of <code class=\"\" data-line=\"\">enumerate()<\/code>?<\/strong><\/h3>\n<p data-start=\"6398\" data-end=\"6454\"><strong data-start=\"6398\" data-end=\"6409\">Answer:<\/strong><br \/>\nReturns both index and item while iterating:<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">for i, v in enumerate([&#039;a&#039;, &#039;b&#039;]):<br\/>    print(i, v)<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h3 data-start=\"6527\" data-end=\"6571\"><strong data-start=\"6532\" data-end=\"6571\">23. What are Python\u2019s data classes?<\/strong><\/h3>\n<p>Python&#8217;s data classes, introduced in Python 3.7 via PEP 557, are a decorator-based feature (<span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">@dataclass<\/span>) designed to simplify the creation of classes that primarily store data. They automatically generate common methods like <span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">init<\/span>, <span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">repr<\/span>, <span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">eq<\/span>, and others, reducing boilerplate code for classes that act as structured data containers.<\/p>\n<h3><strong>24. What are Python sets and their features?<\/strong><\/h3>\n<p dir=\"auto\">Python sets are unordered collections of unique, immutable elements. They are useful for operations involving membership testing, removing duplicates, and performing mathematical set operations.<\/p>\n<h3 dir=\"auto\">Key Features of Python Sets:<\/h3>\n<ol dir=\"auto\">\n<li><strong>Unordered<\/strong>: Elements have no specific order, so indexing or slicing is not possible.<\/li>\n<li><strong>Unique Elements<\/strong>: Duplicate elements are automatically removed.<\/li>\n<li><strong>Mutable<\/strong>: Sets can be modified (add\/remove elements), but the elements themselves must be immutable (e.g., numbers, strings, tuples).<\/li>\n<li><strong>Dynamic<\/strong>: Sets can grow or shrink as elements are added or removed.<\/li>\n<li><strong>No Indexing<\/strong>: Since sets are unordered, elements cannot be accessed via indices.<\/li>\n<li><strong>Hashable Elements<\/strong>: Elements in a set must be hashable (i.e., they must have a valid __hash__ method).<\/li>\n<\/ol>\n<h3><strong><a tabindex=\"0\">25. Explain Python\u2019s <code class=\"\" data-line=\"\">zip()<\/code> function.<\/a><\/strong><\/h3>\n<div class=\"flex flex-col items-center\">\n<div class=\"relative group flex flex-col justify-center w-full max-w-[var(--content-max-width)] pb-0.5 items-start\">\n<div class=\"message-bubble rounded-3xl text-primary min-h-7 prose dark:prose-invert break-words prose-p:opacity-100 prose-strong:opacity-100 prose-li:opacity-100 prose-ul:opacity-100 prose-ol:opacity-100 prose-ul:my-1 prose-ol:my-1 prose-li:my-2 last:prose-li:mb-3 prose-li:ps-1 prose-li:ms-1 w-full max-w-none\" dir=\"auto\">\n<div class=\"response-content-markdown markdown [&amp;_a:not(.not-prose)]:text-current [&amp;_a:not(.not-prose):hover]:text-primary [&amp;_a:not(.not-prose):hover]:decoration-primary [&amp;_a:not(.not-prose)]:underline [&amp;_a:not(.not-prose)]:decoration-primary\/30 [&amp;_a:not(.not-prose)]:underline-offset-2 [&amp;_h2:not(.not-prose):first-child]:mt-0 [&amp;_h3:not(.not-prose):first-child]:mt-0 [&amp;_h4:not(.not-prose):first-child]:mt-0\">\n<p class=\"break-words\" dir=\"auto\">Python&#8217;s <span class=\"text-sm px-1 rounded-sm !font-mono bg-orange-400\/10 text-orange-500 dark:bg-orange-300\/10 dark:text-orange-200\">zip()<\/span> function takes multiple iterables (like lists, tuples, or strings) and combines their elements into tuples, returning an iterator of these tuples. It pairs elements based on their position, stopping when the shortest iterable is exhausted.<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">a = [1, 2]<br\/>b = [&#039;x&#039;, &#039;y&#039;]<br\/>list(zip(a, b))  # [(1, &#039;x&#039;), (2, &#039;y&#039;)]<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3><strong>26. How to handle JSON in Python?<\/strong><\/h3>\n<p data-start=\"176\" data-end=\"258\"><strong data-start=\"176\" data-end=\"187\">Answer:<\/strong><br data-start=\"187\" data-end=\"190\" \/>Python provides the <code class=\"\" data-line=\"\">json<\/code> module to parse and manipulate JSON data.<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">import json<br\/><br\/># Convert dict to JSON string<br\/>data = {&#039;name&#039;: &#039;John&#039;, &#039;age&#039;: 30}<br\/>json_str = json.dumps(data)<br\/><br\/># Convert JSON string to dict<br\/>parsed = json.loads(json_str)<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<div class=\"contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary\">\n<h3 class=\"overflow-y-auto p-4\" dir=\"ltr\"><strong>27. What are Python assertions?<\/strong><\/h3>\n<p><strong data-start=\"549\" data-end=\"560\">Answer:<\/strong><br data-start=\"560\" data-end=\"563\" \/>Assertions are used for debugging. The <code class=\"\" data-line=\"\">assert<\/code> statement tests a condition and throws <code class=\"\" data-line=\"\">AssertionError<\/code> if the condition is false.<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">x = 5<br\/>assert x &gt; 0, &quot;x must be positive&quot;<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<\/div>\n<h3 data-start=\"756\" data-end=\"834\"><strong data-start=\"762\" data-end=\"832\">28. What is the difference between <code class=\"\" data-line=\"\">del<\/code>, <code class=\"\" data-line=\"\">remove()<\/code>, and <code class=\"\" data-line=\"\">pop()<\/code>?<\/strong><\/h3>\n<p data-start=\"835\" data-end=\"848\"><strong data-start=\"835\" data-end=\"846\">Answer:<\/strong><\/p>\n<ul data-start=\"849\" data-end=\"1010\">\n<li data-start=\"849\" data-end=\"896\">\n<p data-start=\"851\" data-end=\"896\"><code class=\"\" data-line=\"\">del<\/code> removes by index or variable reference.<\/p>\n<\/li>\n<li data-start=\"897\" data-end=\"950\">\n<p data-start=\"899\" data-end=\"950\"><code class=\"\" data-line=\"\">remove()<\/code> deletes the first occurrence of a value.<\/p>\n<\/li>\n<li data-start=\"951\" data-end=\"1010\">\n<p data-start=\"953\" data-end=\"1010\"><code class=\"\" data-line=\"\">pop()<\/code> removes and returns the element at a given index.<\/p>\n<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">lst = [1, 2, 3]<br\/>del lst[0]<br\/>lst.remove(2)<br\/>lst.pop(0)<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h3 data-start=\"1084\" data-end=\"1130\"><strong data-start=\"1090\" data-end=\"1128\">29. Explain slicing with examples.<\/strong><\/h3>\n<p data-start=\"1131\" data-end=\"1225\"><strong data-start=\"1131\" data-end=\"1142\">Answer:<\/strong><br data-start=\"1142\" data-end=\"1145\" \/>Slicing extracts a portion of a list, tuple, or string: <code class=\"\" data-line=\"\">list[start:stop:step]<\/code>.<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">nums = [0, 1, 2, 3, 4, 5]<br\/>print(nums[1:4])  # [1, 2, 3]<br\/>print(nums[::-1]) # Reversed list<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h3 data-start=\"1337\" data-end=\"1413\"><strong data-start=\"1343\" data-end=\"1411\">30. What is the difference between <code class=\"\" data-line=\"\">__str__()<\/code> and <code class=\"\" data-line=\"\">__repr__()<\/code>?<\/strong><\/h3>\n<p data-start=\"1414\" data-end=\"1427\"><strong data-start=\"1414\" data-end=\"1425\">Answer:<\/strong><\/p>\n<ul data-start=\"1428\" data-end=\"1555\">\n<li data-start=\"1428\" data-end=\"1485\">\n<p data-start=\"1430\" data-end=\"1485\"><code class=\"\" data-line=\"\">__str__()<\/code> is for user-friendly string representation.<\/p>\n<\/li>\n<li data-start=\"1486\" data-end=\"1555\">\n<p data-start=\"1488\" data-end=\"1555\"><code class=\"\" data-line=\"\">__repr__()<\/code> is for developers\/debugging and should be unambiguous.<\/p>\n<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">class Person:<br\/>    def __repr__(self):<br\/>        return &quot;Person(&#039;John&#039;)&quot;<br\/>    def __str__(self):<br\/>        return &quot;John&quot;<br\/><br\/>print(repr(Person()))<br\/>print(str(Person()))<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h3 data-start=\"1736\" data-end=\"1782\"><strong data-start=\"1742\" data-end=\"1780\">31. What is a metaclass in Python?<\/strong><\/h3>\n<p data-start=\"1783\" data-end=\"1934\"><strong data-start=\"1783\" data-end=\"1794\">Answer:<\/strong><br data-start=\"1794\" data-end=\"1797\" \/>A metaclass is a class of a class. It defines how classes behave and are constructed. You can customize class creation using metaclasses.<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">class Meta(type):<br\/>    def __new__(cls, name, bases, dct):<br\/>        print(&quot;Creating class&quot;, name)<br\/>        return super().__new__(cls, name, bases, dct)<br\/><br\/>class MyClass(metaclass=Meta):<br\/>    pass<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h3 data-start=\"2147\" data-end=\"2184\"><strong data-start=\"2153\" data-end=\"2182\">32. Explain type hinting.<\/strong><\/h3>\n<p data-start=\"2185\" data-end=\"2298\"><strong data-start=\"2185\" data-end=\"2196\">Answer:<\/strong><br data-start=\"2196\" data-end=\"2199\" \/>Type hinting allows you to specify the expected data types of function arguments and return values.<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">def greet(name: str) -&gt; str:<br\/>    return &quot;Hello, &quot; + name<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<p data-start=\"2372\" data-end=\"2451\">It helps with code readability and tooling (e.g., static analysis with <code class=\"\" data-line=\"\">mypy<\/code>).<\/p>\n<h3 data-start=\"2458\" data-end=\"2494\"><strong data-start=\"2464\" data-end=\"2492\">33. What is duck typing?<\/strong><\/h3>\n<p data-start=\"2495\" data-end=\"2632\"><strong data-start=\"2495\" data-end=\"2506\">Answer:<\/strong><br data-start=\"2506\" data-end=\"2509\" \/>Duck typing focuses on behavior rather than the actual type. \u201cIf it walks like a duck and quacks like a duck, it&#8217;s a duck.\u201d<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">def quack(duck):<br\/>    duck.quack()<br\/><br\/>class Duck:<br\/>    def quack(self):<br\/>        print(&quot;Quack!&quot;)<br\/><br\/>quack(Duck())  # Works as long as the method exists<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h3 data-start=\"2799\" data-end=\"2844\"><strong data-start=\"2805\" data-end=\"2842\">34. Explain the use of <code class=\"\" data-line=\"\">super()<\/code>.<\/strong><\/h3>\n<p data-start=\"2845\" data-end=\"2967\"><strong data-start=\"2845\" data-end=\"2856\">Answer:<\/strong><br data-start=\"2856\" data-end=\"2859\" \/><code class=\"\" data-line=\"\">super()<\/code> is used to call a method from a parent class in child classes, especially in multiple inheritance.<\/p>\n<h3 data-start=\"3137\" data-end=\"3173\"><strong data-start=\"3143\" data-end=\"3171\">35. What are frozensets?<\/strong><\/h3>\n<p data-start=\"3174\" data-end=\"3312\"><strong data-start=\"3174\" data-end=\"3185\">Answer:<\/strong><br data-start=\"3185\" data-end=\"3188\" \/><code class=\"\" data-line=\"\">frozenset<\/code> is an immutable version of a set. It cannot be modified once created and is hashable (can be used as dict keys).<\/p>\n<h3 data-start=\"3360\" data-end=\"3422\"><strong data-start=\"3366\" data-end=\"3420\">36. What is the use of <code class=\"\" data-line=\"\">globals()<\/code> and <code class=\"\" data-line=\"\">locals()<\/code>?<\/strong><\/h3>\n<p data-start=\"3423\" data-end=\"3436\"><strong data-start=\"3423\" data-end=\"3434\">Answer:<\/strong><\/p>\n<ul data-start=\"3437\" data-end=\"3557\">\n<li data-start=\"3437\" data-end=\"3488\">\n<p data-start=\"3439\" data-end=\"3488\"><code class=\"\" data-line=\"\">globals()<\/code> returns a dictionary of global scope.<\/p>\n<\/li>\n<li data-start=\"3489\" data-end=\"3557\">\n<p data-start=\"3491\" data-end=\"3557\"><code class=\"\" data-line=\"\">locals()<\/code> returns a dictionary of the current local symbol table.<\/p>\n<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">x = 10<br\/>print(globals()[&#039;x&#039;])<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h3 data-start=\"3608\" data-end=\"3674\"><strong data-start=\"3614\" data-end=\"3672\">37. How do you use list and dictionary comprehensions?<\/strong><\/h3>\n<p data-start=\"3675\" data-end=\"3768\"><strong data-start=\"3675\" data-end=\"3686\">Answer:<\/strong><br data-start=\"3686\" data-end=\"3689\" \/>List and dictionary comprehensions provide a compact way to create collections.<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">squares = [x*x for x in range(5)]<br\/>even_dict = {x: x%2 == 0 for x in range(5)}<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h3 data-start=\"3868\" data-end=\"3924\"><strong data-start=\"3874\" data-end=\"3922\">38. Explain <code class=\"\" data-line=\"\">try...except...else...finally<\/code>.<\/strong><\/h3>\n<p data-start=\"3925\" data-end=\"3938\"><strong data-start=\"3925\" data-end=\"3936\">Answer:<\/strong><\/p>\n<ul data-start=\"3939\" data-end=\"4050\">\n<li data-start=\"3939\" data-end=\"3961\">\n<p data-start=\"3941\" data-end=\"3961\"><code class=\"\" data-line=\"\">try<\/code>: Run the code.<\/p>\n<\/li>\n<li data-start=\"3962\" data-end=\"3992\">\n<p data-start=\"3964\" data-end=\"3992\"><code class=\"\" data-line=\"\">except<\/code>: Handle exceptions.<\/p>\n<\/li>\n<li data-start=\"3993\" data-end=\"4024\">\n<p data-start=\"3995\" data-end=\"4024\"><code class=\"\" data-line=\"\">else<\/code>: Runs if no exception.<\/p>\n<\/li>\n<li data-start=\"4025\" data-end=\"4050\">\n<p data-start=\"4027\" data-end=\"4050\"><code class=\"\" data-line=\"\">finally<\/code>: Always runs.<\/p>\n<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">try:<br\/>    x = 10 \/ 2<br\/>except ZeroDivisionError:<br\/>    print(&quot;Error&quot;)<br\/>else:<br\/>    print(&quot;Success&quot;)<br\/>finally:<br\/>    print(&quot;Done&quot;)<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h3 data-start=\"4191\" data-end=\"4260\"><strong data-start=\"4197\" data-end=\"4258\">39. Are arguments passed by value or reference in Python?<\/strong><\/h3>\n<p data-start=\"4261\" data-end=\"4406\"><strong data-start=\"4261\" data-end=\"4272\">Answer:<\/strong><br data-start=\"4272\" data-end=\"4275\" \/>Python uses <strong data-start=\"4287\" data-end=\"4315\">pass-by-object-reference<\/strong>. Mutable objects (like lists) can be changed, while immutable (like ints, strings) cannot.<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">def add(lst):<br\/>    lst.append(5)<br\/><br\/>a = [1, 2]<br\/>add(a)<br\/>print(a)  # [1, 2, 5]<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h3 data-start=\"4501\" data-end=\"4558\"><strong data-start=\"4507\" data-end=\"4556\">40. Difference between iterator and iterable?<\/strong><\/h3>\n<p data-start=\"4559\" data-end=\"4572\"><strong data-start=\"4559\" data-end=\"4570\">Answer:<\/strong><\/p>\n<ul data-start=\"4573\" data-end=\"4714\">\n<li data-start=\"4573\" data-end=\"4647\">\n<p data-start=\"4575\" data-end=\"4647\"><strong data-start=\"4575\" data-end=\"4587\">Iterable<\/strong>: An object that can return an iterator (e.g., list, tuple).<\/p>\n<\/li>\n<li data-start=\"4648\" data-end=\"4714\">\n<p data-start=\"4650\" data-end=\"4714\"><strong data-start=\"4650\" data-end=\"4662\">Iterator<\/strong>: Object with <code class=\"\" data-line=\"\">__next__()<\/code> and <code class=\"\" data-line=\"\">__iter__()<\/code> methods.<\/p>\n<\/li>\n<\/ul>\n<div class=\"contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary\">\n<div class=\"sticky top-9\">\n<div class=\"absolute end-0 bottom-0 flex h-9 items-center pe-2\">\n<div class=\"bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs\">\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">nums = [1, 2, 3]<br\/>it = iter(nums)<br\/>print(next(it))  # 1<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<p>&nbsp;<\/p>\n<h3 data-start=\"7010\" data-end=\"7078\"><strong data-start=\"7016\" data-end=\"7076\">41. Explain multithreading vs multiprocessing in Python.<\/strong><\/h3>\n<p data-start=\"7079\" data-end=\"7092\"><strong data-start=\"7079\" data-end=\"7090\">Answer:<\/strong><\/p>\n<ul data-start=\"7093\" data-end=\"7264\">\n<li data-start=\"7093\" data-end=\"7176\">\n<p data-start=\"7095\" data-end=\"7176\"><strong data-start=\"7095\" data-end=\"7113\">Multithreading<\/strong>: Multiple threads within the same process; limited by the GIL.<\/p>\n<\/li>\n<li data-start=\"7177\" data-end=\"7264\">\n<p data-start=\"7179\" data-end=\"7264\"><strong data-start=\"7179\" data-end=\"7198\">Multiprocessing<\/strong>: Multiple processes with separate memory space; bypasses the GIL.<\/p>\n<\/li>\n<\/ul>\n<p data-start=\"7266\" data-end=\"7338\">Use <code class=\"\" data-line=\"\">threading<\/code> for I\/O-bound and <code class=\"\" data-line=\"\">multiprocessing<\/code> for CPU-bound tasks.<\/p>\n<h3 data-start=\"7266\" data-end=\"7338\"><strong>42. Explain Python\u2019s <code class=\"\" data-line=\"\">logging<\/code> module.<\/strong><\/h3>\n<p dir=\"auto\">Python\u2019s logging module provides a flexible framework for generating log messages in applications. It allows developers to track events, errors, and information during program execution, offering more control and configurability than print statements. Below is a comprehensive explanation of the logging module, its components, and how to use it effectively.<\/p>\n<p dir=\"auto\">Key Components of the Logging Module:<\/p>\n<ul>\n<li dir=\"auto\">Logger<\/li>\n<li dir=\"auto\">Log Levels<\/li>\n<li dir=\"auto\">Handler<\/li>\n<li dir=\"auto\">Formatter<\/li>\n<li dir=\"auto\">Filter<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<h3 class=\"overflow-y-auto p-4\" dir=\"ltr\"><strong> 43. What is the <code class=\"\" data-line=\"\">yield from<\/code> expression?<\/strong><\/h3>\n<p dir=\"auto\">The yield from expression in Python is used in generator functions to delegate the generation of values to another iterable, such as a generator, list, or other iterable object. It simplifies the process of yielding values from a sub-iterable without needing to manually iterate over it.<\/p>\n<h3 dir=\"auto\">Key Points about yield from:<\/h3>\n<ol dir=\"auto\">\n<li><strong>Purpose<\/strong>: It allows a generator to yield all values from another iterable (like a generator, list, or tuple) directly, as if they were yielded by the parent generator.<\/li>\n<li><strong>Syntax<\/strong>: yield from &lt;iterable&gt;<\/li>\n<li><strong>Introduced<\/strong>: Python 3.3 (PEP 380).<\/li>\n<li><strong>Use Cases<\/strong>: It\u2019s commonly used to chain generators, simplify code, and improve readability when working with nested iterables or sub-generators.<\/li>\n<\/ol>\n<h2>\u2705 <strong data-start=\"263\" data-end=\"348\">Mastering Python Intermediate Coding Questions: What to Expect and How to Prepare<\/strong><\/h2>\n<figure id=\"attachment_8686\" aria-describedby=\"caption-attachment-8686\" style=\"width: 1024px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"size-full wp-image-8686\" src=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2023\/12\/Mastering-Python-Intermediate-Coding-Questions-with-colorful-background-with-some-tips.png\" alt=\"python intermediate interview questions\" width=\"1024\" height=\"1536\" srcset=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2023\/12\/Mastering-Python-Intermediate-Coding-Questions-with-colorful-background-with-some-tips.png 1024w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2023\/12\/Mastering-Python-Intermediate-Coding-Questions-with-colorful-background-with-some-tips-200x300.png 200w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2023\/12\/Mastering-Python-Intermediate-Coding-Questions-with-colorful-background-with-some-tips-683x1024.png 683w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2023\/12\/Mastering-Python-Intermediate-Coding-Questions-with-colorful-background-with-some-tips-768x1152.png 768w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2023\/12\/Mastering-Python-Intermediate-Coding-Questions-with-colorful-background-with-some-tips-332x498.png 332w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2023\/12\/Mastering-Python-Intermediate-Coding-Questions-with-colorful-background-with-some-tips-664x996.png 664w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2023\/12\/Mastering-Python-Intermediate-Coding-Questions-with-colorful-background-with-some-tips-688x1032.png 688w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption id=\"caption-attachment-8686\" class=\"wp-caption-text\">Mastering Python Intermediate Coding Questions<\/figcaption><\/figure>\n<p data-start=\"350\" data-end=\"574\"><strong data-start=\"350\" data-end=\"390\">Python Intermediate Coding Questions<\/strong> are designed to test not just your familiarity with syntax but also your ability to solve logical, algorithmic, and real-world programming challenges using Python\u2019s powerful features.<\/p>\n<p data-start=\"576\" data-end=\"702\">Unlike basic questions (e.g., reversing a string or printing Fibonacci numbers), <strong data-start=\"657\" data-end=\"689\">intermediate coding problems<\/strong> may involve:<\/p>\n<h3 data-start=\"704\" data-end=\"738\">\ud83d\udd39 <strong data-start=\"712\" data-end=\"738\">Use of Data Structures<\/strong><\/h3>\n<ul>\n<li data-start=\"741\" data-end=\"782\">Manipulating nested lists or dictionaries<\/li>\n<li data-start=\"785\" data-end=\"824\">Using sets for performance optimization<\/li>\n<li data-start=\"827\" data-end=\"896\">Working with collections module (Counter, defaultdict, deque)<\/li>\n<\/ul>\n<h3>\ud83d\udd39 <strong data-start=\"1046\" data-end=\"1085\">Working with Functional Programming<\/strong><\/h3>\n<p data-start=\"1086\" data-end=\"1167\">Python supports functional paradigms that are often tested in interviews through:<\/p>\n<ul>\n<li data-start=\"1170\" data-end=\"1201\">map(), filter(), reduce()<\/li>\n<li data-start=\"1204\" data-end=\"1220\">Lambda functions<\/li>\n<li data-start=\"1223\" data-end=\"1257\">List and dictionary comprehensions<\/li>\n<\/ul>\n<h3>\ud83d\udd39 <strong data-start=\"1396\" data-end=\"1438\">File Handling and Exception Management<\/strong><\/h3>\n<p data-start=\"1439\" data-end=\"1503\">Many Python Intermediate Coding Questions include tasks such as:<\/p>\n<ul>\n<li data-start=\"1506\" data-end=\"1527\">Reading\/writing files<\/li>\n<li data-start=\"1530\" data-end=\"1562\">Error handling with try-except<\/li>\n<li data-start=\"1565\" data-end=\"1616\">Logging or parsing structured data (like CSV, JSON)<\/li>\n<\/ul>\n<h3>\ud83d\udd39 <strong data-start=\"1783\" data-end=\"1809\">Object-Oriented Design<\/strong><\/h3>\n<p data-start=\"1810\" data-end=\"1831\">OOP concepts such as:<\/p>\n<ul>\n<li data-start=\"1834\" data-end=\"1850\">Creating classes<\/li>\n<li data-start=\"1853\" data-end=\"1864\">Inheritance<\/li>\n<li data-start=\"1867\" data-end=\"1879\">Polymorphism<\/li>\n<li data-start=\"1882\" data-end=\"1943\">Use of @classmethod, @staticmethod, __str__, __repr__<\/li>\n<\/ul>\n<p data-start=\"1945\" data-end=\"2062\">These are common in <strong data-start=\"1965\" data-end=\"2005\">python intermediate coding questions<\/strong> when you&#8217;re expected to write modular and reusable code.<\/p>\n<h3>\ud83d\udd39 <strong data-start=\"2077\" data-end=\"2106\">Decorators and Generators<\/strong><\/h3>\n<p data-start=\"2107\" data-end=\"2183\">These more advanced features are often introduced at the intermediate level.<\/p>\n<h2><strong>Common Python intermediate interview questions<\/strong><\/h2>\n<p data-start=\"2548\" data-end=\"2591\">Here\u2019s what you can expect to be tested on:<\/p>\n<ul>\n<li data-start=\"2594\" data-end=\"2634\">String manipulation and pattern matching<\/li>\n<li data-start=\"2637\" data-end=\"2660\">Recursion vs. iteration<\/li>\n<li data-start=\"2663\" data-end=\"2699\">Custom sorting using key parameter<\/li>\n<li data-start=\"2702\" data-end=\"2730\">Working with dates and times<\/li>\n<li data-start=\"2733\" data-end=\"2765\">Dictionary flattening or merging<\/li>\n<li data-start=\"2768\" data-end=\"2801\">Data serialization (Pickle, JSON)<\/li>\n<li data-start=\"2804\" data-end=\"2843\">Multithreading &amp; multiprocessing basics<\/li>\n<li data-start=\"2846\" data-end=\"2880\">Writing and testing custom modules<\/li>\n<\/ul>\n<h2>Benefits of Practicing Python Intermediate Interview Questions<\/h2>\n<p data-start=\"289\" data-end=\"567\">If you&#8217;re aiming to land a job in software development, data science, or automation testing, mastering <strong data-start=\"392\" data-end=\"435\">Python Intermediate Interview Questions<\/strong> offers several key benefits beyond just passing an interview. Here&#8217;s why dedicating time to these questions is a smart career move:<\/p>\n<ul>\n<li>Bridges the Gap Between Beginner and Expert<\/li>\n<li>Improves Problem-Solving and Coding Logic<\/li>\n<li>Prepares You for Real Coding Rounds<\/li>\n<li>Enhances Resume and Portfolio Quality<\/li>\n<li>Builds Foundation for Advanced Concepts<\/li>\n<li>Boosts Confidence for Tech Competitions and Hackathons<\/li>\n<li>Makes You a Better Collaborator<\/li>\n<\/ul>\n<h2 data-start=\"2624\" data-end=\"2644\">\u2705 Final Thoughts on Python Intermediate Questions<\/h2>\n<p data-start=\"2646\" data-end=\"3054\">Mastering <strong data-start=\"2656\" data-end=\"2699\">python intermediate interview questions<\/strong> is essential for landing developer roles and excelling in coding interviews. If you&#8217;re serious about upgrading your skills, it&#8217;s the perfect time to enroll in a <a href=\"https:\/\/www.kaashivinfotech.com\/python-course\/\"><strong data-start=\"2861\" data-end=\"2878\">Python course<\/strong><\/a> that focuses on intermediate to advanced topics. A structured course can help you bridge the gap, build real-world projects, and prepare confidently for technical assessments.<\/p>\n<p data-start=\"3056\" data-end=\"3254\">Whether you&#8217;re aiming to switch careers, crack coding rounds, or simply level up your programming game\u2014investing in a <strong data-start=\"3174\" data-end=\"3191\">Python course<\/strong> is a smart move toward becoming a proficient <a href=\"https:\/\/www.kaashivinfotech.com\/python-full-stack-development-course-in-chennai\/\">Python developer<\/a>.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Deep Dive: Python Intermediate Interview Questions for Career Growth Python intermediate interview questions are designed to test a candidate\u2019s ability to apply Python concepts beyond the beginner level. If you&#8217;re preparing for technical interviews, it&#8217;s essential to practice not only basic syntax but also focus on python intermediate questions and python intermediate coding questions that [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":8681,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[724],"tags":[7850,7852,7849,7847,7848,768,7851,7853,7854],"class_list":["post-2711","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-interview-questions","tag-python-coding-interview","tag-python-for-developers","tag-python-intermediate-coding-questions","tag-python-intermediate-interview-questions","tag-python-intermediate-questions","tag-python-interview-preparation","tag-python-job-questions","tag-python-mid-level-interview","tag-python-technical-questions"],"_links":{"self":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/posts\/2711","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/comments?post=2711"}],"version-history":[{"count":0,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/posts\/2711\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/media\/8681"}],"wp:attachment":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/media?parent=2711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/categories?post=2711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/tags?post=2711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}