AI ERA SERIES 01
Python Basics
A first Python course for complete beginners
Problems follow the contents chapter by chapter and section by section. There are four question types (predict the output, fill in the blanks, write code, and code review), and Python in your browser checks your answer right away.
258 problems (99 predict the output, 26 fill in the blanks, 87 write code, 46 code review)
Start practicing in your browser
Chapter 1: Getting started Why fundamentals matter in the AI era
1.1 Hands that type code, eyes that read it No problems
1.2 Your first run in the browser, no installation
- Printing text and calculated results Predict the output · Basic
1.3 The bare minimum about notebooks
- A cell that prints a name Predict the output · Basic
- The run order decides the value Predict the output · Basic
- A notebook that stops on Run all Code review · Basic
1.4 A map of this course The journey, the setup, and how to doubt No problems
1.5 [Pair coding with AI] Asking AI about setup problems
- An answer that says to reinstall Code review · Medium
Chapter 2: Numbers and strings
2.1 Naming values Variables
- Changing the number of likes Predict the output · Basic
2.2 Numbers int and float
- Calculating again from the current value Predict the output · Basic
2.3 Strings A container for text
- + and * change with the data type Predict the output · Basic
- Telling numbers from text Predict the output · Basic
2.4 Indexing and slicing Taking strings apart
- Taking the city and the year out of a string Write code · Basic
2.5 f-strings Building output
- A sentence from an age and a year Write code · Basic
2.6 Type conversion Moving between int, float, and str
- Reviewing variables, slices, and data types Predict the output · Basic
- Reactions and the reaction rate Write code · Medium
- Fixing code that prints a name and an age Code review · Medium
2.7 [Pair coding with AI] Asking for string code Doubt the index boundaries
- A slice that is off by one Predict the output · Medium
Chapter 3: Lists and tuples
3.1 Lists Many values in one container
- Printing a list and counting it Predict the output · Basic
- An empty list and one empty string Predict the output · Medium
- A line that reports the post count Write code · Basic
3.2 The same rules Indexing and slicing again
- Indexing and slicing a list Predict the output · Basic
- The first two and the last two Fill in the blanks · Medium
- View counts from the second to the fourth post Code review · Medium
3.3 Changing lists A new tool called methods
- Inserting, removing, and popping Predict the output · Medium
- Passing the turn in a question queue Write code · Medium
- An insert with the position and the value swapped Code review · Basic
3.4 Sorting and summarizing In place, or a new value?
- What append returns Predict the output · Basic
- A new sorted list that leaves the original alone Write code · Basic
- Fixing and summarizing a list of likes Write code · Medium
- Sorting that gives None Code review · Basic
3.5 Two names, one container Aliasing and copying
- The same list and a copied list Predict the output · Basic
3.6 Tuples Useful because they can't change
- Tuple items and variable names Predict the output · Basic
- Changing an item in a tuple Predict the output · Medium
- Taking the city and the year out of a tuple Write code · Basic
3.7 [Pair coding with AI] Asking for list code Copy or alias?
- Lists inside a list made by multiplying Predict the output · Challenge
- A new list without the spam account Code review · Medium
Chapter 4: Dictionaries and sets
4.1 By name, not by number Dictionaries
- Finding ranks with number keys Predict the output · Basic
- Survey answers with the same key twice Predict the output · Medium
- Looking up follower counts with a name stored in a variable Write code · Medium
4.2 Updating dictionaries Adding, changing, and missing keys
- Looking up a missing key Predict the output · Basic
- The reaction rate of a post Write code · Medium
4.3 Looking inside dictionaries in, len, and three views
- Is data one of the values? Code review · Medium
4.4 Sets A container that refuses duplicates
- A set of viewers that drops duplicates Predict the output · Basic
- Collecting names, starting with empty braces Predict the output · Medium
- Removing and adding subscribers Fill in the blanks · Basic
4.5 Set operations and set() A tool for removing duplicates
- Comparing the tags of two posts Write code · Medium
4.6 A map of four containers Which one should you use?
- Follow pairs that have a direction Predict the output · Medium
- Summarizing post tags Write code · Medium
4.7 [Pair coding with AI] Asking for dictionary code Doubt missing keys
- A default value you cannot tell apart from 0 Code review · Medium
Chapter 5: Operators
5.1 The two faces of division Quotient and remainder
- Six calculations with two numbers Write code · Basic
5.2 Order of operations Precedence and parentheses
- The order of calculation in an engagement score Predict the output · Basic
- Raising a variable that holds a negative number to a power Predict the output · Medium
- A growth rate with missing parentheses Code review · Medium
5.3 The language of true and false Comparison operators and Booleans
- Views that go past the goal Predict the output · Basic
- Several ways to ask whether two tag lists are the same Predict the output · Medium
- Updating the follower count and checking the goal Write code · Medium
5.4 Floats are approximations Before you ask whether two are equal
- Storing the rounded value back Predict the output · Basic
- Watch time just short of 0.8 Predict the output · Medium
- Checking the sum of response proportions Write code · Medium
5.5 Combining conditions and, or, not
- A check that mixes "and", "or", and "not" Predict the output · Challenge
- A condition for a range of views Fill in the blanks · Basic
- A condition that misses values outside the range Code review · Medium
5.6 Is it in there? in, not in, and a map of operators
- Is it in the list? Write code · Basic
- Does it meet both conditions? Write code · Basic
- A post summary and a condition check Write code · Challenge
5.7 [Pair coding with AI] Asking for calculations Doubt division and float comparisons
- Checking code that averages star ratings Predict the output · Medium
- Full pages and leftover posts Code review · Medium
Chapter 6: Conditionals
6.1 What if if and indentation
- An assignment inside a skipped block Predict the output · Basic
- Activity points added separately Write code · Basic
- A congratulations message that slipped out of the block Code review · Basic
6.2 Otherwise Two branches with else
- A second check with the changed points Predict the output · Medium
- A title preview Write code · Medium
- Two thresholds with only one updated Code review · Medium
6.3 Many branches elif and letter grades
- Only the first true branch Predict the output · Basic
- A separate condition after the chain Predict the output · Medium
- Classifying post reactions by comment count Write code · Medium
6.4 Decided at the boundary Boundary values and overlapping conditions
- Matching an elif to its if by indentation Predict the output · Medium
- The boundaries between video lengths Fill in the blanks · Medium
- A new tier hidden by a wider condition Code review · Medium
6.5 Truthy and falsy What counts as true
- What "and" and "or" return Predict the output · Challenge
- Fixing a condition to fit its purpose Code review · Medium
6.6 [Pair coding with AI] Asking for branching code Doubt boundaries and missing cases
- A value that matches no branch Predict the output · Medium
- A views badge written by an AI Code review · Medium
Chapter 7: Loops
7.1 Repeating for and range
- The end of range and the number of iterations Predict the output · Basic
- The step of range Predict the output · Basic
- The range end value for printing up to 8 Fill in the blanks · Basic
- Printing only the odd numbers Write code · Basic
7.2 Going through containers Looping over strings, lists, and dictionaries
- Looping over a dictionary with for Predict the output · Basic
- Printing only the likes at or above a threshold Write code · Basic
- Printing survey results Write code · Basic
7.3 Building up Accumulators and trace tables
- Where the running total is printed Predict the output · Basic
- Counting posts with 100 or more views Predict the output · Medium
- Comparing with the previous value Predict the output · Challenge
- Blanks in a sum of views Fill in the blanks · Basic
- The largest view count Write code · Basic
- The sum of multiples of 3 or 5 Write code · Basic
- The smallest value Write code · Medium
- The largest value and its position Write code · Medium
- The second-highest number of likes Write code · Medium
- Summarizing comment counts Write code · Medium
- Summarizing a list of posts Write code · Medium
- The second score when there are ties Write code · Challenge
- The names in first and second place Write code · Challenge
- Counting ratings of 4 stars or more Code review · Basic
7.4 While it is true while and infinite loops
- A while loop that keeps halving Predict the output · Medium
- When the while loop ends Predict the output · Medium
- The moment the sum first goes over 50 Predict the output · Medium
- The day followers reach 1000 Fill in the blanks · Medium
- The sum of the digits Write code · Medium
- The first day followers go over 500 Write code · Medium
- The digit sum and the number of digits Write code · Medium
7.5 Changing the flow break, continue, pass
- After the loop hits break Predict the output · Basic
- What happens with pass Predict the output · Basic
- continue and break Predict the output · Medium
- Skipping only 4 Fill in the blanks · Basic
7.6 Nested loops and the first math helper GCD and primes
- How many times the inner loop runs Predict the output · Medium
- Making pairs of two people Predict the output · Medium
- Predicting how many times code runs Predict the output · Medium
- A prime check that fails at the boundary Predict the output · Medium
- A blank in a star staircase Fill in the blanks · Basic
- Times tables that skip 2 and 5 Fill in the blanks · Medium
- Odd times tables in reverse order Fill in the blanks · Medium
- A five-line triangle of stars Write code · Basic
- Checking whether a number is prime Write code · Basic
- Finding all the divisors Write code · Basic
- Finding all the common divisors Write code · Basic
- An upside-down triangle Write code · Medium
- A triangle of numbers Write code · Medium
- Making a multiplication table Write code · Medium
- Collecting the primes in a range Write code · Medium
- How many primes are there? Write code · Medium
- The largest prime less than 100 Write code · Medium
- The greatest common divisor Write code · Medium
- The greatest common divisor and the least common multiple Write code · Medium
- The total of the odd times tables Write code · Medium
- Collecting only the odd products Write code · Medium
- A right-aligned triangle Write code · Challenge
- The tenth prime Write code · Challenge
- Code that finds only two primes Code review · Challenge
- Finding the sixth prime Code review · Challenge
7.7 [Pair coding with AI] Asking for loops Doubt range ends and infinite loops
- Splitting two classes into groups of the same size Fill in the blanks · Medium
- A sum of multiples of 5 that misses 60 Code review · Basic
- A loop that never looks at the last item Code review · Medium
Chapter 8: Functions
8.1 Building parts Defining and calling functions
- Calling without an argument Predict the output · Basic
- A welcome message function Write code · Basic
8.2 Giving back return and None
- A function ends at return Predict the output · Basic
- Adding returned values Predict the output · Basic
- Nested calls Predict the output · Medium
- Returning the result Fill in the blanks · Basic
- How many times a digit appears Write code · Medium
- A function that returns the number of divisors Write code · Medium
- The most frequent number Write code · Challenge
- Rotating a list to the right Write code · Challenge
8.3 Handing over ingredients Positional and keyword arguments, and defaults
- Where each argument goes Predict the output · Medium
- Where the default value goes Predict the output · Medium
- Calling with a default value and keyword arguments Predict the output · Medium
- A default discount rate Fill in the blanks · Basic
- The sum of a range Write code · Medium
- A post summary split into functions Write code · Challenge
8.4 Returning several values Returning tuples and unpacking
- Two lists returned as a tuple Predict the output · Medium
- When there is one name too many Predict the output · Medium
- Receiving two values separately Fill in the blanks · Medium
- A function that summarizes a satisfaction survey Fill in the blanks · Medium
- Returning the maximum and the minimum together Write code · Medium
- A function that returns the total and the maximum together Write code · Medium
8.5 Inside and outside a function Local and global variables
- A parameter is a local variable too Predict the output · Medium
- A weekly view report Predict the output · Medium
- A bonus that keeps the original scores Code review · Medium
- The average video watch time Code review · Medium
8.6 The math helper as parts gcd and is_prime
- A prime-checking function Write code · Medium
- The fewest bags of rice Write code · Challenge
- An is_prime that gives wrong results Code review · Medium
8.7 [Pair coding with AI] Asking for functions The spec is the prompt; doubt functions that return nothing
- An engagement rate function that only prints Code review · Medium
Chapter 9: File input and output
9.1 Talking to your program input
- Numbers that come in as strings Predict the output · Basic
- A value that int() cannot convert Predict the output · Medium
- Where to convert to numbers Fill in the blanks · Basic
- An even or odd checker Write code · Basic
- A multiplication table printer Write code · Basic
9.2 Writing to a file open, write, close
- Text printed on the screen and text written to a file Predict the output · Basic
- Writing one per line and closing the file Fill in the blanks · Basic
- Passing a number straight to write() Code review · Basic
9.3 Reading a file read and looping over lines
- The newlines that readlines() keeps Predict the output · Medium
- Followers on the first and last days Fill in the blanks · Basic
- Counting survey answers Write code · Medium
9.4 Syntax that never forgets, and the contract of modes with, r/w/a/x
- A file opened with "w" and nothing written Predict the output · Medium
- The right mode for three files Fill in the blanks · Medium
- Reading a file outside the with block Code review · Medium
9.5 Where is it, and how is it written? Paths and encodings
- Two files with the same name Predict the output · Basic
- A file with the ad comments filtered out Write code · Medium
- Code with a leftover Colab Drive path Code review · Basic
9.6 The math helper remembers Saving and loading a list of primes
- Saving a tag list to a file and reading it back Write code · Challenge
- A save function that writes the newline first Code review · Medium
9.7 [Pair coding with AI] Asking for file code Doubt encodings, paths, and a missing close
- A record file that was never closed Predict the output · Medium
- Accented comments read with a different encoding Code review · Medium
Chapter 10: Pythonic code
10.1 Readable code What "Pythonic" means
- Swapping first and second place Predict the output · Basic
- A boundary that changed in a chained comparison Code review · Basic
10.2 A building loop in one line List comprehensions
- A list of hashtags and their lengths Predict the output · Basic
- Reversing survey scores Fill in the blanks · Basic
- Collecting only the authors from a list of posts Write code · Basic
10.3 Filtering and choosing Comprehensions with conditions
- An if at the end and an if-else at the front Predict the output · Basic
- Filtering out ad comments Write code · Medium
- View counts with the missing days dropped Code review · Medium
10.4 Index and value together enumerate
- Numbers counted from 1, and indexes Predict the output · Medium
- Numbers of the posts with 100 or more likes Write code · Medium
- A comment list where only the loop variable changed Code review · Medium
10.5 Walking side by side zip
- A zip with a missing partner Predict the output · Medium
- Checking the lengths before pairing Fill in the blanks · Medium
- Follower changes from yesterday to today Write code · Basic
10.6 Polishing the math helper Comprehensions and zip in practice
- Printing a summary of new followers Write code · Medium
- Days with more likes than average Write code · Challenge
- A label list that stops at assert Code review · Medium
10.7 [Pair coding with AI] "Make it Pythonic" A hard-to-read one-liner is not an improvement
- Three branches packed into one line Predict the output · Medium
- Engagement rates from swapped pairs Code review · Medium
Chapter 11: Classes
11.1 Data and behavior together Thinking in objects
- Authors and likes that no longer match Predict the output · Basic
- A post bundled in a dictionary, with functions outside Write code · Basic
11.2 Drawing the blueprint class, __init__, self
- A method call with one extra value Predict the output · Medium
- Blanks in a song blueprint Fill in the blanks · Basic
- A post that remembers its likes and comments Write code · Medium
11.3 Methods that change state Deposits and withdrawals
- Someone who votes twice Predict the output · Medium
- A stamp card with a reward for ten stamps Write code · Challenge
- A follower count that never goes down Code review · Basic
11.4 Shared by all, owned by each Class variables and __str__
- The class's default goal and one person's goal Predict the output · Medium
- A number and a list kept in the class Predict the output · Challenge
- Members numbered in the order they join Write code · Medium
11.5 When you need a class State justifies the design
- A method that only the child class has Predict the output · Medium
- Calling a method of the same class Code review · Basic
11.6 The math helper evolves PrimeFinder, which remembers primes
- A cache that remembers tags already counted Fill in the blanks · Medium
- Counting the days posted in a row Write code · Challenge
11.7 [Pair coding with AI] Asking for class design Doubt overdesign
- A like total that grows with every call Code review · Medium
Chapter 12: Modules and packages
12.1 Graduating from notebooks .py files and the terminal
- The lines a script shows Predict the output · Basic
- A script in a different order from the cells Predict the output · Medium
- A summary that throws away returned values Code review · Basic
12.2 import Bringing in parts others made
- The range randint draws from Predict the output · Medium
- A square grid that fits all the photos Write code · Medium
- Adjusting scores without touching the original Write code · Medium
12.3 from, as, and the star Reading import statements
- A variable that overwrote a module name Predict the output · Medium
- A title overwritten by a star import Code review · Medium
12.4 Making your own module gcdlcm.py and the two faces of a file
- A line that prints just from importing Predict the output · Medium
- Using values from a settings module Fill in the blanks · Basic
- An old copy that hides the module's function Code review · Medium
12.5 Packages The math helper, ready to share
- The names of modules inside a package Predict the output · Challenge
- Drawing bars with a survey package Write code · Medium
- The import after moving a file into a folder Code review · Medium
12.6 [Pair coding with AI] Asking for library suggestions Check for modules that don't exist
- A missing function in a real module Predict the output · Basic
- Built-in functions instead of a module that does not exist Code review · Medium
Chapter 13: Exception handling
13.1 Reading the red text to the end Tracebacks
- Adding up likes with a string mixed in Predict the output · Basic
- Where the follower lookup stopped Predict the output · Medium
13.2 A program that doesn't crash try and except
- A response counted before the error Predict the output · Medium
- Failures you can check for and failures you find by trying Fill in the blanks · Basic
- None when the text does not convert to an integer Write code · Basic
13.3 What to catch Error families and wide nets
- The message caught with as e Predict the output · Medium
- A different message for each failure Write code · Medium
- The wrong error name Code review · Basic
13.4 Cleaning up and refusing honestly finally, else, raise
- A finally that runs before the return Predict the output · Medium
- An error raised again inside except Predict the output · Challenge
- Rejecting star ratings out of range Write code · Medium
13.5 The math helper gets sturdy Three layers of armor
- Catching a rejection raised by a helper function Write code · Medium
- When the settings file is missing and when it is broken Write code · Medium
13.6 [Pair coding with AI] Asking to add exception handling Doubt handling that silences errors
- A false message left by a wide net Predict the output · Medium
- A search for the most likes that stops halfway Code review · Medium