Technology Python Programming Foundations

Learn with diagrams, code, systems and practical examples.

</>
Computer Science · Students & Professionals

Python Programming Foundations

Learn Python through practical examples covering syntax, data structures, functions, files, and problem solving.

PythonProgrammingProblem Solving

Start with Your First Python Program →

Course roadmap

Know where you are going before you begin

A useful course should make the starting point, destination, and learning method clear—not make you guess from a list of links.

Learning material

Inside this course

Move through the material in order unless a prerequisite or practice link gives you a better reason to branch.

Interview preparation

Course interview practice

Use these as short execution and debugging drills after the course. Predict the program state before answering, name the data-model or control-flow choice that matters, and explain the smallest change that fixes the behavior rather than reciting Python syntax.

01 · IntermediateA program reads age = input("Age: ") and then fails on age + 1. What happened, and where should the fix live?

Answer out loud before opening the expected response.

Practice

Expected answer
input() returns text, so age is a string and cannot be added directly to an integer. Convert and validate the value at the input boundary, for example with int(age), while handling invalid user input explicitly.

Why this answer works
The interview signal is whether you trace types through execution instead of guessing at the error. External input begins as untrusted text. Converting close to the boundary makes the program’s internal assumptions clearer, but conversion can fail, so robust code decides what to do with blank, malformed, or out-of-range values rather than scattering casts throughout later calculations.

Review the relevant lesson →

02 · IntermediateTwo variables point to the same list, one variable appends an item, and both variables now appear changed. Why, and how would you make an independent list?

Answer out loud before opening the expected response.

Practice

Expected answer
Assignment binds another name to the same mutable list object; it does not clone the list. Create a separate shallow copy with list(existing) or existing.copy() when independent top-level mutation is required.

Why this answer works
This tests the difference between names and objects. The useful trace is to ask whether the operation creates a new object or mutates an existing one. A shallow copy is enough for a flat list, but nested mutable objects can still be shared, so a strong answer distinguishes shallow copying from the rarer need for a deliberate deep copy.

Review the relevant lesson →

03 · IntermediateYou repeatedly need to ask whether a user ID has already been seen, but the current code scans a growing list every time. What would you change and why?

Answer out loud before opening the expected response.

Practice

Expected answer
Use a set when the requirement is unique membership rather than positional order. Set membership is typically constant expected time, while scanning a list grows linearly with the number of stored IDs.

Why this answer works
The important skill is matching the data structure to the operation. Lists are valuable when order, duplicates, or indexing matter; sets express uniqueness and fast membership directly. A strong answer also notes that changing structures can change ordering semantics and that performance claims should be understood as expected behavior rather than a universal guarantee.

Review the relevant lesson →

04 · IntermediateAn if/elif chain checks score >= 60 before score >= 90, so a score of 95 is labeled “pass” instead of “excellent.” Why does this happen?

Answer out loud before opening the expected response.

Practice

Expected answer
Python evaluates the chain from top to bottom and stops at the first true branch. The broad >= 60 condition captures 95 before the more specific >= 90 condition is reached, so the conditions should be ordered or rewritten to reflect the intended ranges.

Why this answer works
This is an execution-tracing question. Do not read every branch as if it were independently evaluated. For mutually exclusive ranges, trace the first condition that becomes true and ask whether earlier conditions accidentally contain later cases. Reordering from most specific/highest threshold downward is one clear fix when that matches the domain rules.

Review the relevant lesson →

05 · SeniorThree parts of a program repeat the same price-after-discount calculation with slightly different variable names. What refactoring would you propose?

Answer out loud before opening the expected response.

Practice

Expected answer
Extract the business rule into a function with explicit inputs and a returned result, then have each caller supply its values. Keep input/output or unrelated state outside the function unless those effects are part of its responsibility.

Why this answer works
Interviewers are looking for more than “use a function.” A good boundary names one responsibility, makes dependencies visible as parameters, returns a useful value, and can be tested independently. Copy-pasted formulas drift over time; centralizing the rule reduces inconsistent fixes while avoiding a giant function that mixes calculation, user interaction, and unrelated side effects.

Review the relevant lesson →

Continue to comprehensive Interview Prep →

Knowledge check

Test what you can apply

Five course-specific questions covering the most important ideas in Python Programming Foundations. Commit to an answer before reading the explanation.

Score0 / 50 answered
01

A program runs age = input("Age: ") and the user types 12. What happens if the next line is print(age + 1) without any conversion?

02

You need to record product IDs seen in a data feed and quickly answer “have we already seen this ID?” while storing each ID only once. Which built-in structure best matches that requirement?

03

A checkout program needs to look up the price for a product by SKU, such as prices["A17"]. Which structure most directly represents that relationship?

04

Consider: temperature = 31; if temperature > 35: print("hot"); elif temperature > 25: print("warm"); else: print("cool"). What is printed?

05

A program repeats the same tax calculation in five places, and a rule change requires editing all five copies. Which refactor most directly reduces that maintenance risk?