Product SiteDocumentation Site

Chapter 5. If-else , the control flow

5.1. If statement
5.2. Else statement
5.3. Truth value testing

While working on real life of problems we have to make decisions. Decisions like which camera to buy or which cricket bat is better. At the time of writing a computer program we do the same. We make the decisions using if-else statements, we change the flow of control in the program by using them.

5.1. If statement

The syntax looks like
if expression:
    do this

If the value of expression is true (anything other than zero), do the what is written below under indentation. Please remember to give proper indentation, all the lines indented will be evaluated on the True value of the expression. One simple example is to take some number as input and check if the number is less than 100 or not.
#!/usr/bin/env python
number = int(raw_input("Enter a number: "))
if number < 100:
    print "The number is less than 100"

Then we run it
$ ./number100.py
Enter a number: 12
The number is less than 100