{ }codelper

100 Python Practice Problems for Beginners

If you have just learned Python syntax, work through these 100 problems in order. Each topic starts with the easiest ones, and you can run and check every answer on this site with nothing to install.

100 problemsLevel 1–2Solve in Python

1. Input and output #1–9

input() gives you one line as a string.
To use it as a number, write int(input()).
When one line has several numbers, read them with map(int, input().split()).
Shape your output with sep and end in print, and with f-strings.

Syntax refresher: Python input(): Read User Input · Python print(): Output with sep and end · Python split(): Split a String into a List · Python f-string: Format Numbers and Text

  1. Copying a guestbook line8P0311
  2. Sum of two numbers8P0001
  3. Bonus-added scores joined with commas8P0373
  4. Numbering the lines of a song8P0347
  5. Score statistics8P0003
  6. Exactly the money you have8P0043
  7. Sum to the end8P0018
  8. Counting paragraphs split by empty lines8P0438
  9. Weight on the scale8P0221

2. Variables and types #10–21

Python integers have no size limit, so you do not need to worry about overflow as in other languages.
But mixing a string and a number, as in "3" + 4, raises TypeError, so always keep track of whether the value you read is a string or a number.
Swapping two values takes one line: a, b = b, a.

  1. Marbles passed in turn8P0251
  2. The shorter one stands in front8P0344
  3. Four lines of true and false8P0217
  4. Ticket number from a seat code8P0226
  5. Sale price, or list price if there is none8P0281
  6. Swap the cards8P0046
  7. Big sum8P0015
  8. Truncate, floor, round8P0044
  9. A siren that keeps sounding until switched off8P0289
  10. Letter chain8P0045
  11. Ride capacity8P0086
  12. Three hops to the power box contact8P0286

3. Arithmetic #22–33

/ always gives a float, and // gives the quotient rounded down.
-7 // 2 is -4 and -7 % 2 is 1 (the remainder takes the sign of the divisor).
round() goes to the even side when the value is exactly halfway, so when the output asks for a set number of decimal places, f"{x:.2f}" is the safer choice.

  1. Distance per liter8P0216
  2. Sum of digits8P0006
  3. How many combinations a luggage lock has8P0423
  4. Grid distance8P0017
  5. Grading addition homework8P0222
  6. Seconds to H:MM:SS8P0013
  7. Free-throw success rate sheet8P0314
  8. Weekday after k days8P0016
  9. Round half to even8P0048
  10. This year's savings goal rate8P0407
  11. Powers that fit8P0050
  12. Closest to the target8P0218

4. Conditionals #34–44

Split into branches with if, elif and else.
You can chain a range, as in 0 <= x < 10, and ask whether a value is one of several with x in (1, 3, 5).
Indentation is the block, so being off by a single space changes the meaning.

  1. A photo fitted to a square frame8P0254
  2. Letter grades8P0011
  3. Leap year8P0019
  4. Locker extra fee8P0372
  5. Three-day temperature trend8P0227
  6. Member discount and delivery fee8P0191
  7. Kite-flying notice board8P0315
  8. Middle of three8P0020
  9. Four-operation calculator8P0021
  10. Password rule checker8P0098
  11. Due dates that skip closed days8P0431

5. Loops #45–56

range(a, b) does not include b.
To count down, use range(n, 0, -1).
When you also need the position, use enumerate.
When you do not know how many times to repeat, use while together with break.

Syntax refresher: Python for Loop · Python range(): Start, Stop and Step · Python while Loop · Python break vs continue

  1. Clap game8P0002
  2. Star triangle8P0007
  3. One row of the times table8P0022
  4. One more throw for yut or mo8P0255
  5. First multiple8P0023
  6. Street lamps, with one at the very end8P0371
  7. A reading marathon that grows every day8P0282
  8. Fibonacci number8P0010
  9. Counting temperature records8P0219
  10. Count the digits8P0052
  11. Loading a delivery truck in order8P0223
  12. Count down by steps8P0024

6. Strings #57–68

Strings cannot be changed (they are immutable), so s[0] = "a" does not work.
You have to build a new string.
Reverse with s[::-1], split with split, and join back together with "separator".join(items).

Syntax refresher: Python Slicing: [start:stop:step] Explained · Python split(): Split a String into a List · Python join(): Join a List into a String

  1. Count the vowels8P0009
  2. First and last position of a character8P0053
  3. Letters picked along the diagonal8P0283
  4. Count the words8P0012
  5. Palindrome check8P0004
  6. Reverse it, keep the sign8P0252
  7. Swap the case8P0025
  8. A price tag with dots and commas swapped8P0412
  9. Characters left in a message with Korean text8P0341
  10. Letter numbers in a note8P0192
  11. Cut from the a-th to the b-th character8P0054
  12. Reverse the word order8P0028

7. Lists #69–79

b = a does not copy.
It puts one more name on the same list.
To work on a separate one, copy it with a[:] or a.copy().
a.sort() changes that list and returns None, while sorted(a) returns a new list.

Syntax refresher: Python List append(), extend() and insert() · Python sort() vs sorted(): Sort a List · Python Slicing: [start:stop:step] Explained

  1. Largest number and its position8P0005
  2. Numbers of the students who retake8P0374
  3. Showing one page of a board8P0284
  4. Sort the numbers8P0036
  5. Sorting sign letters8P0253
  6. Is it on the list?8P0059
  7. Swapping places in pairs8P0312
  8. Travel time on a one-way bus route8P0224
  9. Remove every match8P0058
  10. Rotate a list right8P0032
  11. Row and column sums8P0033

8. Sum, average, maximum #80–84

sum, max, min and len are built in.
It helps to work them out with your own loop first and then shorten the code with the built-in functions.
When a problem asks for the average as an integer, use //.
When it asks for a decimal, use /.

  1. Dart scores thrown in turns8P0342
  2. Mornings at or above average8P0343
  3. Second largest8P0031
  4. Class averages and the year average8P0225
  5. The day closest to the average8P0418

9. Functions #85–92

Make one with def and give a value back with return.
Without return you get None back, so watch out for the mistake of only calling print on the result and not returning it.

  1. A thermometer that cannot leave its scale8P0402
  2. Balls that landed in the sandbox8P0285
  3. Prime check8P0008
  4. A running course made of three flags8P0435
  5. A box-count function with defaults8P0257
  6. Tidy locker numbers8P0196
  7. Hall chairs numbered in a zigzag8P0428
  8. Pick the top word with a key function passed in8P0318

10. Dictionaries (dict) #93–100

Use one to look up a value by name.
Reading a missing key with d[k] raises KeyError, so in counting problems use get, as in d[k] = d.get(k, 0) + 1.

Syntax refresher: Python Dictionary (dict): Keys and Values

  1. Tally scans in order8P0220
  2. Phone book lookup8P0063
  3. Only the last vote counts8P0228
  4. Find mutual picks8P0256
  5. Clearing out the fridge8P0410
  6. Find stock by part-code range8P0229
  7. Remove duplicates8P0035
  8. Booths that start when k students gather8P0349

Good to know

See also