Python Conditional Statements Exercise

Exercises 1 Question

Python Conditional Statements Exercise

1 Question
Blog / Exercises / Python Conditional Statements Exercise

Conditional Statements Exercise:

Create a function that counts the number of elements within a list that are greater than 30.

Here’s an example list you can use to test your work: num = [1,4,62,78,32,23,90,24,2,34].

The tutorial you may need: Learning How to Use Conditionals in Python



We start off by defining a function that takes a list as its only parameter.

Next, we define a variable ‘count’ within the function to keep track of the number of elements greater than 30. Remember to initially assign 0 to the variable “count” since we don’t know if there are any 0 elements in the list.

Now that we have set our counter, we can go through the list. Using a ‘for’ loop, we go through each element one by one and check if it is greater than 30.

If an element satisfies our condition and the if-statement, we increase our ‘count’ variable by 1. If it doesn’t, we need not take any action, so no need to write any ELIF or ELSE statements.

Next, after the ‘for’ loop has concluded, we return the ‘count’ variable that kept track of all elements greater than 30.

def greater_than_30(numbers):

    count = 0

    for n in numbers:

        if n>30:


    return count

To make sure our work is correct we need to call the function. Since it requires a parameter, we need to define a list of numbers outside the function. Remember to put the name of the list in parentheses when calling the function. You can create other lists or adjust the existing one to check your code. Don’t hesitate to try that, not only with integers but also floats!

Want to practice some more on lists? You can check the other exercises here.



Leave a Reply

Your email address will not be published.

A Free Data Science Career Guide?

Join a community of 8,000 other data science enthusiasts.

Check your email shortly!