Learn to code for data analysis
Learn to code for data analysis

Start this free course now. Just create an account and sign in. Enrol and complete the course for a free statement of participation or digital badge if available.

Free course

Learn to code for data analysis

1.5 Comparison operators

In Expressions [Tip: hold Ctrl and click a link to open it in a new tab. (Hide tip)] , you learned that Python has arithmetic operators: +, /, - and * and that expressions such as 5 + 2 evaluate to a value (in this case the number 7).

Python also has what are called comparison operators, these are:

== equals

!= not equal

< less than

> greater than

<= less than or equal to

>= greater than or equal to

Expressions involving these operators always evaluate to a Boolean value, that is True or False. Here are some examples:

2 = = 2 evaluates to True

2 + 2 = = 5 evaluates to False

2 != 1 + 1 evaluates to False

45 < 50 evaluates to True

20 > 30 evaluates to False

100 <= 100 evaluates to True

101 >= 100 evaluates to True

The comparison operators can be used with other types of data, not just numbers. Used with strings they compare using alphabetical order. For example:

'aardvark' < 'zebra' evaluates to True

In Calculating over columns you saw that when applied to whole columns, the arithmetic operators did the calculations row by row. Similarly, an expression like df['Country'] >= 'K' will compare the country names, row by row, against the string 'K' and record whether the result is True or False in a series like this:

0 False

1 False

2 False

3 False

4 False

5 False


Name: Country, dtype: bool

If such an expression is put within square brackets immediately after a dataframe’s name, a new dataframe is obtained with only those rows where the result is True. So:

df[df['Country'] >= 'K']

returns a new dataframe with all the columns of df but with only the rows corresponding to countries starting with K or a letter later in the alphabet.

As another example, to see the data for countries with over 80 million inhabitants, the following code will return and display a new dataframe with all the columns of df but with only the rows where it is True that the value in the 'Population (1000s)' column is greater than 80000:

In []:

df[df['Population (1000s)'] > 80000]


 CountryPopulation (1000s)TB deaths
141Russian Federation14283417000
185United States of America320051490
190Viet Nam9168017000

Exercise 2 Comparison operators

You are ready to complete Exercise 2 in the Exercise notebook 2.

Remember to run the existing code in the notebook before you start the exercise. When you’ve completed the exercise, save the notebook.


Take your learning further

Making the decision to study can be a big step, which is why you'll want a trusted University. The Open University has 50 years’ experience delivering flexible learning and 170,000 students are studying with us right now. Take a look at all Open University courses.

If you are new to University-level study, we offer two introductory routes to our qualifications. You could either choose to start with an Access module, or a module which allows you to count your previous learning towards an Open University qualification. Read our guide on Where to take your learning next for more information.

Not ready for formal University study? Then browse over 1000 free courses on OpenLearn and sign up to our newsletter to hear about new free courses as they are released.

Every year, thousands of students decide to study with The Open University. With over 120 qualifications, we’ve got the right course for you.

Request an Open University prospectus371