Generate All Coin Flip Outcomes
Intermediate · Write the code · from Python itertools: Powerful Tools for Working with Iterables
From the lesson: Python itertools: Powerful Tools for Working with Iterables
Imagine you have a Swiss Army knife for loops. Instead of writing complicated nested loops and temporary lists, you pull out the right tiny tool and the job is done in one clean line.
Read the lessonUse product to generate all possible outcomes of flipping a coin 3 times. The coin has sides 'H' and 'T'. Print each outcome tuple on its own line.
from itertools import product
# Generate and print all 3-flip outcomes
More itertools exercises
- Flatten a Nested ListBeginner
- Find All 2-Letter PermutationsIntermediate
- Predict the takewhile OutputIntermediate
- Compute a Running TotalBeginner
- Group Numbers as Even or OddAdvanced
- Fix the groupby BugAdvanced
All Python exercises · 50 practice problems · Standard Library Essentials quiz