Write a Python program to flip a coin 1000 times and count heads and tails


The program simulates flipping a coin 100 times using Python's random module, and counts the number of times the coin lands on heads and tails.

  • A dictionary coin is created to store the counts for heads and tails, initialized with 0 for both.
  • A list s is created from the keys of the dictionary coin.
  • The for loop is used to simulate the coin toss 100 times:
    • random.choice(s) randomly selects either "heads" or "tails".
    • The count for the chosen outcome is incremented by 1 in the coin dictionary.
  • Finally, the counts for heads and tails are printed to the console.

So, each time the program runs, it simulates flipping a coin 100 times and prints the counts for heads and tails. The output will be the number of times the coin landed on heads and tails, respectively.

Source Code

import random
import itertools
 
coin = {
    "heads": 0,
    "tails": 0,
}
s = list(coin.keys())
for i in range(100):
	res = random.choice(s)
	coin[res] += 1
 
print("Heads :", coin["heads"])
print("Tails :", coin["tails"])

Output

Heads : 62
Tails : 38

Example Programs