Getting started

8 Python Exercises for Beginners With Expected Results

Practice Python strings, lists, conditions, counting, and debugging with eight prompts that give you a result to check by hand.

Start with an exercise you can solve on paper. Given “ Ada ”, an empty string, and “Bo”, strip the spaces and count the nonempty names. The answer is two. Then try the other exercises and change one input in each to check whether you understand the rule.

Work with text and numbers

  1. Trim and count. Given the names “ Ada ”, an empty string, and “Bo”, strip the spaces and count the nonempty values. You should get 2. Then add a string containing three spaces and decide whether it counts.
  2. Convert a reading. Given the text values “18” and “24”, convert them to integers and calculate their mean. You should get 21. Try “missing” as a third value and decide how the caller should learn about the problem.
  3. Format a label. Present a room named Cedar with 5 available seats as “Cedar: 5 seats”. Change the count to 1 and decide whether your label should use the singular.

Express rules with lists and conditions

  1. Keep the requested order. Compare a list of requested titles with a list of titles available. Given requests B, C, A and available titles A and B, report B then A. Explain which collection controls the order.
  2. Classify a value. Label a score of 70 or above “ready” and all lower scores “review”. Try 69, 70, and 71. Make sure the expectation at exactly 70 matches the words in the prompt.
  3. Count answers. Given votes yes, no, yes, return counts of 2 and 1. Add an unknown answer and decide if it should be counted separately or rejected.

Practice reasoning about changes

  1. Track a shared list. Assign one list to two names and then append an item through one name. Predict what printing the other name will show. Repeat after making an outer copy. Explain how the second experiment differs.
  2. Preserve an edge case. Write a function that returns the first nonblank line from a list. Try an empty list and a list of spaces. Choose a consistent behavior when the result is missing, then test that behavior alongside a normal input.

Pick the next exercise based on the mistake you made

When you miss an expected result, record which idea needs work. If the value was a string when you expected a number, practice inspecting types and converting explicit inputs. If a count was one too high, hand trace the state after each loop step. If the first item or the last item disappears, check which boundaries your loop includes.

Save one exercise you solved and revisit it a few days later. Change the input shape or add a rule. If you can predict the new result and explain the needed change, the practice has transferred beyond memorizing one example.

When the expected results feel too easy, work up to a coding challenge with less guidance.

Sources and further reading

Related reading

Browse Getting started · Start Free Lesson 1

Search PythonProjects.org