Home › Interview questions › Python
Python Interview Questions & Answers
71 Python interview questions in 4 levels — junior to senior, plus 16 production scenarios. Read 3 free answers with real-world analogies.
Free sample answers
Why is Python such a common language choice for DevOps and automation work?
One-liner
Python is readable, has a huge library of ready-made tools for talking to cloud providers and APIs, and lets you write a working automation script quickly without a lot of upfront setup.
Real-world analogy
It's like a well-stocked toolbox that already has the right tool for almost any everyday job — you rarely need to build something from scratch, since someone's usually already made a library for exactly what you need.
Detailed explanation
- Python's syntax reads close to plain English, making scripts easier to write quickly and easier for someone else to pick up and understand later.
- Nearly every cloud provider and DevOps tool has an official or well-maintained Python library (like boto3 for AWS), meaning you rarely need to build integrations from scratch.
- It's genuinely good at both quick one-off scripts and larger, more structured automation tools, which is part of why it's used across the entire spectrum of DevOps work.
Scenario
A team needs to write a quick script to clean up old, unused resources every night. Choosing Python means they can use an existing, well-documented library to talk to their cloud provider's API directly, rather than needing to hand-roll HTTP requests and authentication logic themselves from scratch.
Best practices
- Learn the standard library well before reaching for third-party packages — a lot of everyday scripting needs are already covered.
- Use an existing, well-maintained library for talking to a specific cloud provider or tool, rather than writing your own integration from scratch.
- Keep scripts readable — Python's main advantage is how easy it is to read later, so don't undermine that with overly clever, condensed code.
How does argparse actually work, and why is it a better approach than manually parsing sys.argv yourself for a command-line tool?
One-liner
argparse automatically handles parsing command-line arguments, generating helpful usage messages, and validating required arguments, all considerably more reliably than manually inspecting and parsing sys.argv by hand.
Real-world analogy
It's like using a proper, dedicated form-processing system that automatically checks for missing required fields and gives a clear, helpful error message, rather than manually reading through a stack of loose papers yourself and hoping you don't miss checking something important.
Detailed explanation
- argparse lets you declare exactly what arguments your script expects, including whether they're required or optional, and what type of value each one should actually be.
- It automatically generates a helpful --help message describing exactly how to use your script, without you needing to write and maintain that documentation separately by hand.
- It also automatically handles a genuinely common source of bugs — validating argument types and providing a clear, specific error message when something's actually missing or invalid, rather than the script simply crashing with an unhelpful, generic error.
Scenario
A team's script manually parses sys.argv, and it breaks confusingly whenever someone passes arguments in a slightly different order than the exact one the original author had personally assumed. Rewriting it with argparse means arguments can be passed in any order using proper named flags, and anyone can quickly see exactly how to use the script correctly with a simple --help flag.
Best practices
- Use argparse (or a similar dedicated library) for any script accepting more than one or two simple command-line arguments.
- Take advantage of the automatically generated help message, rather than writing and maintaining separate usage documentation by hand.
- Mark arguments as required or optional explicitly, letting argparse handle the validation for you automatically.
An infrastructure automation script gets accidentally run twice, and it creates duplicate resources instead of safely detecting they already exist. How do you fix this going forward?
One-liner
This means the script isn't actually idempotent — add a check for the resource's current existence before creating it, so running the script again safely detects it's already there instead of blindly creating a genuine duplicate.
Real-world analogy
It's like a delivery service that doesn't check whether a package has already been delivered before sending out an entirely new one — a simple check first would have easily prevented the genuine, unnecessary duplicate.
Detailed explanation
- Adding a check for the resource's current existence (using an appropriate lookup call) before attempting to actually create it is the core fix for this exact kind of problem.
- If the resource already exists but with different configuration than what's actually desired, deciding whether to update it in place or leave it alone (and clearly logging that decision) completes proper idempotent behavior.
- Testing the fixed script by actually running it twice in a row directly verifies the fix genuinely works, rather than just assuming it does.
Scenario
A scheduled script accidentally runs twice due to a scheduling overlap, and since it always creates a new resource without ever first checking whether one already exists, this results in a genuine duplicate, causing real confusion and unnecessary cost. Adding an existence check before creation makes the script properly idempotent, so a similar future accidental double-run would now correctly detect the resource already exists and safely do nothing further.
Best practices
- Always check for a resource's current existence before creating it in any infrastructure automation script.
- Test your fix by actually running the script twice in a row, directly verifying idempotent behavior rather than just assuming it.
- Review other existing automation scripts for this exact same class of issue, since it's a genuinely common oversight.
A preview of the 71 questions in the Python guide
Junior (20)
- Why is Python such a common language choice for DevOps and automation work?
- What are the core data types in Python — lists, tuples, dictionaries, and sets — and when would you use each one?
- What's the practical difference between a mutable and an immutable object in Python, and why does it actually matter?
- What's the difference between == and is in Python?
+ 16 more Junior questions, with full answers, in the guide.
Mid-level (20)
- What's the difference between a shallow copy and a deep copy in Python, and when does the difference actually matter?
- What is a generator in Python, and why would you use one instead of just returning a full list?
- What is a context manager, and how does the with statement actually relate to it?
- How would you actually write your own decorator that accepts arguments, rather than just a simple, no-argument one?
+ 16 more Mid-level questions, with full answers, in the guide.
Senior (15)
- How would you design a proper internal CLI tool used by many engineers across an organization, rather than a quick, personal script?
- How would you structure a large Python automation codebase used by multiple teams, to keep it maintainable as it grows?
- How would you design a robust retry strategy for a script that regularly calls external cloud or network APIs, which can occasionally fail transiently?
- How would you actually profile a Python script to find its real performance bottleneck, rather than just guessing at what's slow?
+ 11 more Senior questions, with full answers, in the guide.
Scenario (16)
- A script works perfectly on your own machine but fails when run in a CI pipeline. How do you troubleshoot it?
- An infrastructure automation script gets accidentally run twice, and it creates duplicate resources instead of safely detecting they already exist. How do you fix this going forward?
- A script calling a cloud provider's API starts failing with rate limit errors under increased usage. How do you fix it?
- A long-running Python process's memory usage keeps growing steadily over time, eventually crashing. How do you investigate this kind of memory leak?
+ 12 more Scenario questions, with full answers, in the guide.
Get the full DevOps / SRE Interview Guide