View on GitHub

python-for-kids

Learning and exercise content on Python for Kids

Day 9 - Condition (if…else…)

Condition is important in Programming language. In the process of writing your logic you often want to check if something is correct (or True) then do something, else do nothing (or do something else).

For example if you want to buy an online movie ticket then the system will first check if the movie hall has seats available then only it will issue a ticket. Else it will tell you that there is no more seats available.

if SOME_CONDITION_IS_TRUE
    do something
else
    do another thing

The above in can be written in Python like

a = 10
b = 9
if a > b:
    print("a is greater than b")
else:
    print("a is not greater than b")

Notice here it will print the correct statement. If you inter change the numbers i.e., a = 9 or b = 10.

You can use any of the comparison operators like,

Name Operator Example
Equals == i==j
Not equals != i!=j
Less than < i<j
Less than equals <= i<=j
Greater than > i>j
Greater than equals >= i>=j

You can also use and to check two or more conditions to true.

You can use or to check if one of the conditions is true

Watch the video

Video link

Day 9 - Exercise

  1. Think about a condition you want to check and print the output. Take some example as per your imagination.
  2. Try to use if two conditions are true.

Next: Day 10 - Collections

Back to Index