Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

How to solve a probability problem in Python

Srivasthavi Mannepula

Probability is how likely an event will occur. It is the value of how likely an event is to occur divided by the total outcomes. For example, the probability of drawing an ace out of a deck of cards, choosing a number out of a set of numbers where the number is prime, tossing a coin, selecting a colored ball out of a bag, etc. These are all different probability problems.

How to write a program

To write a program for a probability question, there are two basic steps:

  1. First, collect a set of items/events.

  2. Then, write a function to solve the problem.

Example

Let’s take the example of choosing one number out of a set of numbers. We can find different probabilities for this particular problem; for instance:

  1. selecting an even number
  2. selecting a number divisible by 3 and 5
  3. selecting an odd number
  4. selecting a prime number

and so on.

Collecting the set of items/events

We will first take the set of numbers the user is going to perform the probability algorithm on.

set_of_numbers=[]
n=int(input("enter the number of elements: "))
print("enter the elements: ")
for i in range(0,n):
  j=int(input())
  set_of_numbers.append(j)
#print the set of numbers 
print(" the set of numbers are: ",set_of_numbers)

Write a function to solve the problem

Let’s say the first probability question is to choose an even number. The first function would be to find the number of even numbers out of the given set of numbers.

def probability_of_even(a):
  c=0
  # here the set of numbers is the list a
  total=len(a)
  for i in a:
    #checks whether the number is even or not
    if(i%2==0):
      c+=1;
      #returns the probability of total even numbers/total outcomes
  return(c/total)

The second probability question might be to choose a prime number. We will write a function to calculate the number of prime numbers that are present in that particular set and return the probability value.

def probability_of_prime(a):
  c=0
  # here the set of numbers is the list a
  total=len(a)
  for i in a:
    #funciton to see if its a prime or not
    if(i>1):
      c2=0
      for j in range(2,i):
        if(i%j==0):
          c2+=1
      if(c2==0):
        c+=1
        #probability value:
  return(c/total)

Let’s say the last probability question is choosing a number that is divisible by both 3 and 5. Then, the function would be as follows:

def probability_divisible(a):
  c=0
  #here the set of numbers is list a
  total=len(a)
  for i in a:
    #check divisibility
    if(i %3==0 and i%5==0):
      c+=1
  # probability value:
  return(c/total)

Final code

The final code combines all of the functions.

def probability_of_even(a):
  c=0
  # here the set of numbers is the list a
  total=len(a)
  for i in a:
    #checks whether the number is even or not
    if(i%2==0):
      c+=1;
      #returns the probability of total even numbers/total outcomes
  return(c/total)

def probability_of_prime(a):
  c=0
  # here the set of numbers is the list a
  total=len(a)
  for i in a:
    #function to see if its a prime or not
    if(i>1):
      c2=0
      for j in range(2,i):
        if(i%j==0):
          c2+=1
      if(c2==0):
        c+=1
        #probability value:
  return(c/total)  

def probability_divisible(a):
  c=0
  #here the set of numbers is list a
  total=len(a)
  for i in a:
    #check divisibility
    if(i %3==0 and i%5==0):
      c+=1
  # probability value:
  return(c/total) 

# let's say we have the following
set_of_numbers=[1,2,3,4,5,6,7,8,9,10,11,23]
n=12
#calling the functions:
print(" the probability of choosing an even number would be: ",probability_of_even(set_of_numbers))
print(" the probability of choosing a prime number would be: ",probability_of_prime(set_of_numbers))
print(" the probability of choosing a number divisible by 3 and five is: ",probability_divisible(set_of_numbers))

For any given probability problem, we can follow the same procedure, i.e., take the set of events/items first, and then write functions to solve the probability question.

RELATED TAGS

python
communitycreator

CONTRIBUTOR

Srivasthavi Mannepula
RELATED COURSES

View all Courses

Keep Exploring