Python Interview Questions and Answers
The Python questions that decide interviews in Pakistan, with worked answers for lists, decorators, generators, and the live coding tasks employers actually set.
Python interviews in Pakistan test how you use the language, not how many libraries you can name. Lists, dictionaries, decorators, and generators carry most of the questions, so practise writing code without an editor finishing your lines for you. Say the trade-off out loud when a question has two good answers, and show a small project built with real data.
- Python interviews test how you use the language, not how many libraries you can name.
- Lists, dictionaries, decorators, and generators carry most of the questions.
- Say the trade-off out loud when a question has two good answers.
- A small project with real data proves more than a certificate.
- Practise writing code without an editor finishing your lines for you.
1. What Python Interviews Actually Test
A fresh graduate in Islamabad opens a backend job on LinkedIn. The listing says Python, Django, PostgreSQL. The interview starts, and the first question is not about Django at all. It is: what happens when you change a list inside a function? He guesses. He gets it wrong. The next forty minutes never recover. That opening is typical. Pakistani employers hiring Python developers test the language first, because framework knowledge is cheap to pick up and language understanding is not. The interviewer wants to see whether you know how Python actually behaves when you run it, not how it behaves in a tutorial. Four habits separate the candidates who pass. First, they can predict what a short piece of code prints before they run it. Second, they can pick the right data structure for a job, a set for membership checks, a dictionary for lookups, a list when order matters. Third, they can explain why they chose it in one sentence. Fourth, they write code that handles the awkward case, an empty list, a missing key, a file that does not exist. If you prepare by reading about Python, you will fail these tests. If you prepare by running short snippets and predicting the output, you will pass most of them.
2. Lists, Dictionaries and the Code That Catches People Out
Question: what is the difference between a list and a tuple? Worked answer: a list is mutable, so you can append to it or change an item, while a tuple is immutable, so once you create it you cannot change it. Use a list for a collection that grows, like a cart, and a tuple for a fixed record, like coordinates (31.5204, 74.3587) for Lahore. A tuple is also hashable when its items are, so it can be a dictionary key and a list cannot. Question: what does this print? x = [1, 2]; y = x; y.append(3). Worked answer: x prints [1, 2, 3], because y is not a copy, it is a second name for the same list. To copy it, write y = x.copy() or y = x[:]. Question: how do you safely get a value from a dictionary? Worked answer: use user.get("phone", "Not provided") instead of user["phone"], because the square-bracket version raises a KeyError when the key is missing and the get version returns your default. Question: what is a list comprehension? Worked answer: a list comprehension that squares each even number in range(5) builds [0, 4, 16] in one line. Say when you would not use one: when the logic needs three conditions and a loop would read more clearly.
3. Decorators, Generators and Copies, Explained With Real Uses
Question: what is a decorator? Worked answer: a decorator is a function that takes another function and returns a new one with extra behaviour. The classic example is timing a function. You write a wrapper that records the start time, calls the function, records the end time, and prints how long it took, then you put @timer above any function you want to measure. In a real project, decorators handle login checks in Flask and Django, caching, and logging. Question: what is a generator, and why would you use one? Worked answer: a generator produces values one at a time with yield, so it does not hold the whole list in memory. Reading a one-gigabyte CSV line by line with a generator uses a small, steady amount of memory, while readlines loads the whole file and can crash a small server. Question: what is the difference between a shallow copy and a deep copy? Worked answer: a shallow copy made with copy.copy duplicates the outer list but shares the inner objects, so changing a nested list changes both copies. A deep copy made with copy.deepcopy duplicates everything, so the two are fully separate. Use a deep copy when your data has lists inside lists. Question: what does the GIL mean for your code? Worked answer: the Global Interpreter Lock means only one thread runs Python bytecode at a time, so threads help with waiting on network calls but not with heavy calculation, where multiprocessing is the right tool.
4. Errors, Equality and the Small Questions That Decide Rounds
Question: how do you handle a file that might not exist? Worked answer: wrap the open call in try and except FileNotFoundError, and tell the user what happened in plain words. Never write a bare except with no exception type, because it hides real bugs, including a typo in your own code. Question: what is the difference between == and is? Worked answer: == checks whether two values are equal, and is checks whether two names point to the same object in memory. The trap is that a == b can be true while a is b is false, and Python reuses small integers, so 256 is 256 prints True while 257 is 257 can print False. Use is only for None, as in if user is None. Question: how do you write a function that accepts any number of arguments? Worked answer: define the function with the star-args form, which collects the extra arguments into a tuple, and use the double-star kwargs form to collect keyword arguments into a dictionary. Use them for wrappers and configuration functions, not for every function, because explicit parameters are easier to read. Question: how would you count how often each word appears in a paragraph? Worked answer: use collections.Counter, which does it in two lines, instead of writing a manual loop with an if statement. Then say what you would do with a million words: stream the file and update the counter line by line, instead of loading it all at once.
5. Live Coding in Python: A Task You Will Actually Meet
A common live task in a Pakistani software house goes like this: here is a CSV of 500 orders, find the three customers who spent the most, and tell us how long your code took. The interviewer watches the order you work in. Start by reading the file with the csv module or pandas, and say which one you are using and why. Then group the rows by customer, add up the amounts, and sort the totals. The clean version uses a dictionary or a groupby, not a loop inside a loop. Say what happens if an amount is missing or written as text, because real data from a shop in Rawalpindi will have a blank row and a price written as "1,200" with a comma. Clean that before you sum, or your total will be wrong. Then talk about speed. A dictionary lookup is fast, a list search inside a loop is slow, and for 500 rows it hardly matters, but for five million rows it decides whether the script finishes tonight. The strongest candidates finish the working version first, then say what they would improve: validate the input, log the bad rows instead of crashing, and write a test for an empty file. That order matters. A perfect solution delivered after the time runs out scores less than a working one with a clear list of improvements.
6. A Two-Week Plan That Ends With Code You Can Show
Fourteen days, one hour a day, and a laptop with a terminal, not a video player. Days one to three, open a Python REPL and run twenty short snippets a day. Predict the output first, run it, and write down every surprise. Focus on lists, dictionaries, copying, and equality, because those are the traps. Days four to six, rewrite three small programs without looking at a tutorial: a word counter using Counter, a function that reads a CSV and totals a column, and a decorator that times a function. If you cannot write them from memory, you do not know them yet. Days seven to nine, answer questions out loud. Take one question from this guide, set a three-minute timer, and explain it as if the interviewer is on the call. Short answers win: one sentence for what it is, one sentence for when you use it, one example. Days ten to twelve, do two timed tasks. The first is the orders CSV task from the previous section. The second is a function that takes a list of job titles and returns the most common skill word. Days thirteen and fourteen, build one small project end to end: fetch a page of job listings, clean the data, and write the results to a CSV. Put it on GitHub with a README that says what it does in three lines. That project is what you talk about when the interviewer asks what you have built.
Frequently Asked Questions
Is core Python still tested in 2026, or is it all frameworks now?
Yes. Python roles in Lahore, Karachi, and Islamabad still test core Python first, even when the job title says data or backend. Framework questions come second, and a candidate who cannot explain a list comprehension will not get to the Django round.
Which three Python topics trip up the most candidates?
Decorators, generators, and the difference between a deep copy and a shallow copy. Each one has a one-line definition and a real use, and interviewers ask for the use, not the definition.
What should I do when I go blank on a coding question?
Use the time to explain the trade-off you are making, for example speed against memory, and ask the interviewer which one matters more for their use case. That question shows judgement, and it buys you thinking time.
Do I need a portfolio project for a Python job in Pakistan?
A small project with real data beats a certificate. Scrape a public jobs page, clean the results with pandas, and put the notebook on GitHub with a short README that explains what you found.
Ready to Put These Strategies into Practice?
Explore thousands of software, remote, and IT vacancies actively hiring today.
Recommended Career Playbooks
Surviving the 45-Minute System Design Whiteboard: An Architect’s Framework
A structured blueprint for tackling distributed backend system design interviews without freezing or getting lost in the weeds.
Read ArticleWhat to Do When You Get Completely Stuck in a Live Coding Interview
The 5 emergency mental shifts to recover gracefully, communicate your way out of a dead end, and save your interview.
Read ArticleHow to Pass Modern Software Engineering Interviews: A Practical, Low-Stress Guide
What happens in live coding sessions, system design interviews and behavioral questions, and how to prepare for each one.
Read ArticleTypeScript Interview Questions and Answers for 2026
The exact TypeScript questions Pakistani developers face in 2026 interviews, with worked answers you can say out loud and a two-week plan to get ready.
Read Article