Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

LAB

Objectives

  • becoming familiar with the input()
  • becoming familiar with comparison operators in Python

Scenario

Using one of the comparison operators in Python, write a simple two-line program that takes the parameter n as input, which is an integer, and prints False if n is less then 100, and True if n is greater than or equal to 100.

Don't create any if blocks (we're going to talk about them very soon). Test your code using the data we've provided for you. 

Conditions and conditional execution 

You already know how to ask Python questions, but you still don't know how to make reasonable use of the answers. You have to have a mechanism which will allow you to do something if a condition is met, and not do it if it isn't. 

It's just like in real life: you do certain things or you don't when a specific condition is met or not, e.g., you go for a walk if the weather is good, or stay home if it's wet and cold

To make such decisions, Python offers a special instruction. Due to its nature and its application, it's called a conditional instruction (or conditional statement).

There are several variants of it. We'll start with the simplest, increasing the difficulty slowly. 

The first form of conditional statement, which you can see below is written very informally but figuratively:

if true_or_not:
   do_this_if_true

This conditional statement consists of the following, strictly necessary, elements in this and this order only:

  1. The if keyword
  2. one or more white spaces
  3. an expression (a question or an answer) whose value will be interpreted solely in terms of True (when its value is non-zero) and False (when it is equal to zero)
  4. colon followed by a newline \n
  5. an indented instruction or set of instructions (at least one instruction is absolutely required); the indentation may be achieved in two ways - by inserting a particular number of spaces (the recommendation is to use four spaces of indentation), or by using the tab character; note: if there is more than one instruction in the indented part, the indentation should be the same in all lines; even though it may look the same if you use tabs mixed with spaces, it's important to make all indentations exactly the same - Python 3 does not allow mixing spaces and tabs for indentation.

How does the that statement work? 

  • If the true_or_not expression represents the truth (i.e., its value is not equal to zero), the indented statements(s) will be executed
  • if the true_or_not expression does not represent the truth (i.e. its value is equal to zero), the indented statement(s) will be omitted (ignored), and the next executed instruction will be one after the original indentation level. 

In real life, we often express desire:

if the weather is good, we'll go for a walk

then, we'll have lunch

As you can see, having lunch is not a conditional activity and doesn't depend on the weather. 

Knowing what conditions influence our behavior, and assuming that we have the parameterless functions go_for_a_walk() and have_lunch(), we can write the following snippet:

if the_weather_is_good:
   go_for_a_walk()
have_lunch()