Innings2
Powered by Innings 2

Glossary

Select one of the keywords on the left…

Learn Python > Logical Operators

Logical Operators

Divisibility Truth Table Explorer

ConditionResultExplanation
Divisible by 5
Divisible by 11
Divisible by both 5 AND 11
Divisible by either 5 OR 11

The logical operator truth table is create above. Update different values and check how the result is changing. Try for values like 5, 11, 55, 23 etc.

For input 5 it is divisble by 5 but not divisible by 11 so:

  • the statement "divisible by 5 and divisible by 11" is
  • the statement "divisible by 5 or divisible by 11" is

For 11:

Is it divisible by 5? No (11 ÷ 5 = 2.2) Is it divisible by 11? Yes (11 ÷ 11 = 1) Therefore: "divisible by 5 AND divisible by 11" is "divisible by 5 OR divisible by 11" is

For 55:

Is it divisible by 5? Yes (55 ÷ 5 = 11) Is it divisible by 11? Yes (55 ÷ 11 = 5) Therefore: "divisible by 5 AND divisible by 11" is "divisible by 5 OR divisible by 11" is

For 23:

Is it divisible by 5? No (23 ÷ 5 = 4.6) Is it divisible by 11? No (23 ÷ 11 = 2.09) Therefore: "divisible by 5 AND divisible by 11" is "divisible by 5 OR divisible by 11" is

To combine two conditions, we use logical operators. The most common logical operators in Python are 'and' and 'or'. When you use 'and', only when the both the conditions are true the overall condition becomes TRUE. When you use 'or' even if one of the conditions is true the overall condition becomes TRUE. So using the logical 'and' operator we can write the solution for the above problem as

25 = 25 and 60000 >= 50000
1 < 0 and sky == "blue"
90 >= 90 or 81 >= 80
21 == 22 and 21 >= 18
100 = 100 and 75 >= 50
15 > 20 and 30 == 30
85 >= 80 or 70 >= 75
5 + 5 == 11 and 8 >= 7

Choose the right logical operator and replace ??? below

num=55
if(num%5 == 0 ??? num%11 == 0):
    print('Divisible by 5 and 11')
else:
    print('Not Divisible by 5 and 11')

Great. Now lets try to combine all the concepts into one problem. See if you can solve it.

Problem: Store marks of different subjects. Find their total. If the total is above or equal to 80 give A grade. If total is between 60 and 80 give B grade. If total is between 40 and 60 give C grade. If total is below 40 give F grade.

Some more fun exercises you can try:

  1. You have 10 marbles. Your brother has 5 marbles. If you have more marbles print you are the winner. If you have lesser marbles, you are the loser. If both have equal number of marbles, its a tie.
  2. Take the current year in a variable. Print if it is a leap year or not.
  3. Find the max of 3 variables.