Tech News, Tech & Coding · Free Online Tools

IT Question Answer
Tools
Back to IT Jobs, Careers & Interview Prep

Python Interview Questions and Answers for Beginners and Juniors

Interview PreparationBy Muhammad Fareed
pythoninterviewquestionscoding

Short answer: Python interviews for junior roles usually test core language knowledge: data types, mutability, functions, comprehensions, generators, decorators, exceptions and how the interpreter works. The answers below are short enough to say aloud and precise enough to show understanding.

Core language questions

What is the difference between a list and a tuple?

Lists are mutable and tuples are not. Tuples can be dictionary keys and are slightly lighter, so use them for fixed records.

What does mutable mean, and why does it matter?

A mutable object can change in place. Passing a list to a function lets that function change the caller's list. A classic bug is a mutable default argument such as def f(x, items=[]), because the same list is reused across calls. Use None and create the list inside.

What is the difference between == and is?

== compares values, and is checks whether two names refer to the same object.

Functions and decorators

What are *args and **kwargs?

*args collects extra positional arguments into a tuple, and **kwargs collects extra keyword arguments into a dictionary.

What is a decorator?

A decorator is a function that takes another function and returns a modified version. It is used for logging, caching and access checks, and is applied with @name above a function.

What is a lambda?

A small anonymous function written in one expression, for example lambda x: x * 2.

Iterators and generators

What is a generator?

A function that uses yield to produce values one at a time instead of building a full list. It saves memory for large or endless sequences.

What is a list comprehension?

A compact way to build a list, such as [x * x for x in range(5)]. It is usually clearer and faster than appending in a loop.

How Python runs

What is the GIL?

The Global Interpreter Lock lets only one thread run Python bytecode at a time in the standard CPython interpreter. Threads still help with waiting on input or output, but CPU-heavy work is better served by multiprocessing or native libraries.

How does memory management work?

CPython uses reference counting plus a garbage collector that handles reference cycles.

Errors and files

How do you handle exceptions?

Use try, except, else and finally. Catch specific exceptions instead of a bare except, which hides real bugs.

Why use the with statement?

It guarantees cleanup, such as closing a file, even when an error occurs.

A small coding exercise

Interviewers often ask for something like counting words:

from collections import Counter

def word_count(text):
    return Counter(text.lower().split())

Explain your choice, mention edge cases such as punctuation, and state the time complexity, which here is linear in the length of the text.

Object-oriented questions

What are __init__ and self?

__init__ is the initializer that runs when an object is created, and self is the reference to the current object inside its methods.

What is the difference between a class method and a static method?

A class method receives the class as its first argument and can create or configure instances. A static method receives neither the instance nor the class and is just a function grouped inside the class.

How to prepare

  1. Write small programs by hand, without autocomplete.
  2. Practice explaining each answer in two or three sentences.
  3. Know one real project well, including trade-offs you made.
  4. Review the standard library: collections, itertools and pathlib.

Frequently asked questions

Is Python interpreted or compiled?

CPython compiles code to bytecode and then runs it on a virtual machine, so it is both in a sense. People usually call it interpreted.

Are Python functions first-class objects?

Yes. You can store them in variables, pass them as arguments and return them from other functions.

What is PEP 8?

The official style guide for Python code, covering naming, spacing and layout.