Dev Global Jobs
Interviews
13 min read2,861 words

Python Interview Questions and Answers

The Python questions interviewers ask most, with short, accurate answers and examples, from data types and mutability to generators, decorators, testing and live coding tips.

By Published: October 26, 2026
A software developer works with code on two monitors in a bright open plan office
Image by googlerankfaster from Pixabay

Most Python interview questions and answers test the same areas: core concepts such as data types, mutability and scope, data structures, functions, generators and decorators, classes and inheritance, error handling, testing and a short coding task. This guide gives short, accurate answers for beginners and experienced developers. Explain your thinking aloud, write clear code and test it with simple examples.

Python interviews usually test core concepts (data types, mutability, scope), data structures (lists, tuples, dictionaries, sets), functions (arguments, lambdas, decorators, generators), classes and inheritance, error handling, testing and a short coding task. Explain your thinking aloud, write clear code and test it with simple examples before you finish.

Key facts

These Python interview questions and answers cover what interviewers ask most, from beginner concepts to the topics experienced developers are expected to explain clearly. Python is in high demand: O*NET found it named in about 815,000 US job postings in 2025, more than any other programming language in its hot technologies list. The answers below are short and accurate, so you can understand them, then explain them in your own words.

Python programming interview questions and answers: core concepts

1. What are Python's main built-in data types?

Numbers (int, float, complex), strings (str), booleans (bool), sequences (list, tuple, range), mappings (dict), sets (set, frozenset), bytes, and None.

2. What is the difference between a list and a tuple?

Lists are mutable: you can change, add and remove items. Tuples are immutable: once created they cannot change. Tuples can be used as dictionary keys if all their items are hashable; lists cannot.

3. What does mutable and immutable mean?

A mutable object can change after it is created, such as a list, dict or set. An immutable object cannot, such as an int, str or tuple. Operations that seem to change an immutable object actually create a new one.

4. What is the difference between == and is?

== checks whether two values are equal. is checks whether two names point to the same object. Use is mainly to compare with None.

5. How does a dictionary work, and what can be a key?

A dictionary maps keys to values using a hash table, giving fast lookups on average. Keys must be hashable, such as strings, numbers and tuples of hashable items. Since Python 3.7, dictionaries keep insertion order.

6. What is a list comprehension?

A short way to build a list from an iterable, optionally with a condition:

squares = [n * n for n in range(10) if n % 2 == 0]

7. What are *args and **kwargs?

*args collects extra positional arguments into a tuple. **kwargs collects extra keyword arguments into a dict. They let a function accept a flexible number of arguments.

8. Why should you avoid a mutable default argument?

Default values are evaluated once, when the function is defined. A list or dict default is shared between calls, so changes persist. Use None and create the object inside the function:

def add_item(item, items=None):
    if items is None:
        items = []
    items.append(item)
    return items

9. What is a lambda?

A small anonymous function written in one expression, for example lambda x: x * 2. It is useful for short functions passed to sorted, map or filter.

10. Explain variable scope (LEGB).

Python looks up names in this order: Local, Enclosing function, Global (module), Built-in. Use global or nonlocal to assign to names in outer scopes.

Functions, generators and decorators

11. What is a generator?

A function that uses yield to produce values one at a time, pausing between them. Generators save memory because they do not build the whole sequence at once.

def countdown(n):
    while n > 0:
        yield n
        n -= 1

12. What is a decorator?

A function that takes a function and returns a new function that adds behaviour, applied with the @ syntax. Common uses are logging, timing, caching and access checks.

13. What is the difference between an iterable and an iterator?

An iterable can return an iterator, for example a list. An iterator produces items one at a time with next() and remembers its position. Generators are iterators.

Object-oriented Python

14. What is self?

The first parameter of an instance method. It refers to the object the method is called on, giving access to its attributes and other methods.

15. What is the difference between a class attribute and an instance attribute?

A class attribute is shared by all instances. An instance attribute, usually set in __init__, belongs to one object.

16. What are @staticmethod and @classmethod?

A static method does not receive the instance or the class; it is a plain function grouped with the class. A class method receives the class as cls and is often used for alternative constructors.

17. What is inheritance, and what does super() do?

Inheritance lets a class reuse and extend another class. super() calls a method from the parent class, often inside __init__.

Errors, files and testing

18. How do you handle exceptions?

With try, except, optional else (runs if no exception) and finally (always runs). Catch specific exceptions rather than a bare except.

19. What is a context manager?

An object used with with that sets up and cleans up a resource automatically, such as closing a file even if an error occurs.

20. How do you test Python code?

Write small automated tests with the built-in unittest module or a test framework, cover normal cases and edge cases, and run them often. Mention any experience with testing in your projects.

More python interview questions and answers for experienced developers

21. What is the difference between a shallow copy and a deep copy?

A shallow copy creates a new outer object but keeps references to the same inner objects. A deep copy, made with copy.deepcopy, copies the inner objects too, so changes to nested items do not affect the original.

22. What is the Global Interpreter Lock?

In the standard CPython implementation, the Global Interpreter Lock lets only one thread run Python bytecode at a time. Threads still help for tasks that wait on input and output, such as network calls. For heavy CPU work, developers often use multiple processes instead.

23. When would you use a set instead of a list?

When you need unique items or fast membership checks. Checking whether an item is in a set is fast on average, while checking a list means scanning it item by item.

24. How do you sort a list of dictionaries by one key?

people.sort(key=lambda p: p["age"])

sorted() returns a new list; list.sort() sorts in place.

25. What are dunder methods?

Special methods with double underscores, such as __init__, __repr__, __len__ and __eq__. They let your classes work with built-in syntax and functions, such as printing, comparing or using len().

26. What is the difference between __str__ and __repr__?

__str__ gives a readable description for users. __repr__ gives an unambiguous description for developers, ideally one that could recreate the object.

27. What are virtual environments?

Isolated Python environments with their own installed packages. They keep project dependencies separate, so one project's versions do not break another.

28. How do you read a large file without loading it all into memory?

Loop over the file object line by line inside a with block. The file is read lazily, one line at a time.

with open("data.txt") as f:
    for line in f:
        process(line)

29. What are type hints?

Optional annotations that show the expected types of variables, arguments and return values, such as def total(prices: list[float]) -> float. Python does not enforce them at run time, but tools can check them and they make code easier to read.

30. How would you find the most common items in a list?

Use collections.Counter, for example Counter(words).most_common(3).

How to handle a live coding task

  1. Repeat the problem in your own words and ask about inputs, outputs and edge cases.
  2. Talk through a plan before you type. A simple working solution first is better than a clever unfinished one.
  3. Write clear code with meaningful names.
  4. Test with small examples, including empty input and unusual values.
  5. Discuss improvements, such as speed or memory, once it works.

The UK National Careers Service notes that in online interviews you may have to share your screen to do a practical task, so practise coding while talking and sharing your screen before the day.

Behavioural questions for developers

Technical skill is only part of the interview. Expect questions such as tell me about a bug you found and fixed, a time you disagreed with a technical decision, or a project you are proud of. Answer with the STAR method, which the National Careers Service recommends: situation, task, action, result. Explain the problem simply, your own actions and what you learned.

A one-week preparation plan

  • Days 1 and 2: review data types, data structures and functions.
  • Days 3 and 4: practise generators, decorators, classes and error handling.
  • Day 5: solve small coding problems, speaking your thinking aloud.
  • Day 6: prepare two project stories and questions for the interviewer.
  • Day 7: rest, then do one short mock interview.

Common mistakes

  • Jumping into code without understanding the problem.
  • Staying silent while thinking; interviewers want to hear your reasoning.
  • Forgetting edge cases such as empty lists or missing keys.
  • Claiming experience with tools you cannot explain.
  • Giving up when stuck instead of trying a simpler approach.

Questions by job type

Data analysis and data science

Expect questions about cleaning data, handling missing values, grouping and summarising data, and explaining results to non-technical people. You may be asked to load a small dataset and answer a business question. Explain each step and check your numbers with a quick sanity test, such as comparing a total with what you expect.

Web development

Expect questions about how a web request is handled, how you structure a web application, how you work with a database, and how you protect user input. Be ready to explain how you would test an endpoint and handle errors gracefully.

Automation and scripting

Expect tasks such as reading files, renaming items, calling an online service or scheduling a job. Show that you handle errors, log what happens and make scripts safe to run more than once.

Testing and quality roles

Expect questions about writing test cases, testing edge cases, using mocks for outside services and keeping tests fast and reliable. Show how tests helped you find a real bug.

Talking about your projects

Interviewers almost always ask about something you built. Pick one or two projects and prepare a short story for each: the problem, your approach, the tools you used, one difficulty you solved and the result. Be honest about what you did yourself and what others did. If your code is public, make sure it is tidy, has a short readme and runs as described. A small, finished project you can explain well is worth more than a large one you cannot.

How to keep learning Python

Python changes slowly but steadily, and good developers keep learning. Read the official Python tutorial and documentation for topics you use often. Practise with small problems regularly. Read other people's well-written code to learn new patterns. Write tests for your own projects. Each new skill gives you a fresh example for your next interview and makes your day-to-day work easier.

Example: from analyst to developer

This example was written for this guide and is not about a real person. Lina worked as a financial analyst and used spreadsheets every day. She started automating reports with short Python scripts, then built a small tool that cleaned and merged monthly files. She practised core questions, prepared a story about her reporting tool and did mock coding tasks with a friend. In her interview she explained her code clearly, tested it with edge cases and talked through what she would improve. She was offered a junior Python developer role in a data team.

Checklist before your interview

  • I can explain lists, tuples, dictionaries and sets, and when to use each.
  • I can explain mutability, scope and the mutable default argument trap.
  • I can write and explain a generator, a decorator and a simple class.
  • I can handle errors with try and except, and use a with block for files.
  • I have practised small coding problems while speaking my thinking aloud.
  • I have two project stories ready, using the STAR method.
  • My screen sharing, microphone and code editor are tested for an online interview.
  • I have two questions to ask the interviewer about the team and the work.

What interviewers really look for

Most interviewers are not hunting for perfect answers. They want to see how you think. They look for clear reasoning, honest answers about what you know and do not know, sensible code, careful testing and good communication. A candidate who solves a simpler version of a problem cleanly and explains it well often does better than one who rushes into a complex solution and gets lost. Ask questions, think aloud, and stay calm when you get stuck. Treat the interview as working through a problem with a future colleague, because that is what it is.

After the interview

Write down the questions you were asked, especially the technical ones, and note where you felt unsure. Look those topics up the same day while they are fresh. If you are not offered the job, ask politely for feedback on your technical answers. Each interview shows you what to practise next, and your answers will grow stronger and more confident each time.

Writing clean code under pressure

Interviewers notice how your code reads, not only whether it runs. Use clear names such as total_price instead of tp. Keep functions short and focused on one job. Handle the obvious edge cases. Add a short comment only where the logic is not obvious. If you run out of time, say what you would refactor and why. Clean, readable code shows that you will be easy to work with on a real team, where other people must read and change your code every day. Practise this habit in every exercise you do before the interview, so it comes naturally when the pressure is on and the interview clock is running down very fast.

Live on Dev Global Jobs

Right now there are 191,925 open jobs on Dev Global Jobs (live count, 26 October 2026). Browse technology jobs that ask for Python, practise with the interview preparation tool, and show your projects in a clean resume from the CV builder.

Frequently Asked Questions

What are common python interview questions and answers for beginners?

Lists versus tuples, mutable versus immutable types, == versus is, dictionaries and keys, list comprehensions, *args and **kwargs, and basic exception handling.

What python programming interview questions and answers do experienced developers face?

Generators and iterators, decorators, scope, mutable default arguments, class and static methods, inheritance, context managers, testing and performance trade-offs.

How should I approach a live coding task?

Repeat the problem, ask about edge cases, explain your plan, write clear code, then test it with simple examples before saying you are done.

Is Python in demand for jobs?

Yes. O*NET found Python named in about 815,000 US job postings in 2025, more than any other programming language in its hot technologies list.

Do I need to memorise the standard library?

No. Know the core data structures and common modules well, and explain how you would look up the rest. Interviewers value clear reasoning over memorisation.

How can I prepare in a week?

Review core concepts daily, solve a few small problems each day, explain your solutions aloud, and prepare two project stories using the STAR method.

Sources

  1. Employer-Based Hot Technologies, O*NET OnLine. Accessed 3 October 2026.
  2. Occupational Outlook Handbook, Software Developers, Quality Assurance Analysts, and Testers, U.S. Bureau of Labor Statistics. Accessed 3 October 2026.
  3. Fastest growing occupations, 2025 and projected 2035, U.S. Bureau of Labor Statistics. Accessed 3 October 2026.
  4. The STAR method, National Careers Service (UK). Accessed 3 October 2026.
  5. How to do well in video interviews, National Careers Service (UK). Accessed 3 October 2026.
  6. The Python Tutorial, Python Software Foundation. Accessed 3 October 2026.

Source: Dev Global Jobs, updated 26 October 2026.

Career Content Editor, Dev Global Jobs

Lutaf Ali is the Career Content Editor at Dev Global Jobs. He has written and edited our guides on jobs, CVs, visas, salaries and careers abroad since the site went live on 18 January 2026, and he runs our official social media. Every fact is checked against official and trusted sources, named in the post, and errors are corrected as soon as readers report them.

Lutaf Ali · About the editorial team · Editorial Policy

Lutaf Ali runs our social media. Follow Dev Global Jobs: Facebook · LinkedIn · Instagram · YouTube · X · Threads · Pinterest

Related Articles

Related Tools

Looking for your next international opportunity?