IF- Review
Good Morning students
On request of two boys from the class i shall take a review of the IF statements today.
-------------------------------------------------------------------------------------------------------------
The Google meet ID for today is:
-------------------------------------------------------------------------------
By the end of the session you will be able to
- apply the concept of a simple IF construct to understand the If and the ELSE components
- apply the concept of a simple IF construct to understand the If ,elif and the ELSE components
- understand the nested IF construct.
If Statements
1.1.1. Simple Conditions
We will now consider simple arithmetic comparisons that directly translate from math into Python. Try each line separately in the Shell
You see that conditions are either
True or False. These are the only possible Boolean values (named after 19th century mathematician George Boole). In Python the name Boolean is shortened to the type bool. It is the type of the results of true-false conditions or tests.
Note
The Boolean values
True and False have no quotes around them! Just as '123' is a string and 123 without the quotes is not, 'True' is a string, not of type bool.
1.1.2. Simple if Statements
Run this example program, suitcase.py. Try it at least twice, with inputs: 30 and then 55. As you an see, you get an extra result, depending on the input. The main code is:
The middle two line are an
If it is true that the weight is greater than 50, then print the statement about an extra charge. If it is not true that the weight is greater than 50, then don’t do the indented part: skip printing the extra luggage charge. In any event, when you have finished with the
if statement. It reads pretty much like English.If it is true that the weight is greater than 50, then print the statement about an extra charge. If it is not true that the weight is greater than 50, then don’t do the indented part: skip printing the extra luggage charge. In any event, when you have finished with the
if statement (whether it actually does anything or not), go on to the next statement that is not indented under the if. In this case that is the statement printing “Thank you”.
The general Python syntax for a simple
if statement isifcondition:indentedStatementBlock
If the condition is true, then do the indented statements. If the condition is not true, then skip the indented statements.
Another fragment as an example:
As with other kinds of statements with a heading and an indented block, the block can have more than one statement. The assumption in the example above is that if an account goes negative, display " Not enough balance"
3.1.3. if-else Statements
Run the example program,
clothes.py. Try it at least twice, with inputs 50 and then 80. As you can see, you get different results, depending on the input. The main code of clothes.py is:
The middle four lines are an if-else statement. Again it is close to English, though you might say “otherwise” instead of “else” (but else is shorter!). There are two indented blocks: One, like in the simple
if statement, comes right after the if heading and is executed when the condition in the if heading is true. In the if-else form this is followed by an else: line, followed by another indented block that is only executed when the original condition is false. In an if-else statement exactly one of two possible indented blocks is executed.
A line is also shown dedented next, removing indentation, about getting exercise. Since it is dedented, it is not a part of the if-else statement: Since its amount of indentation matches the
if heading, it is always executed in the normal forward flow of statements, after the if-else statement (whichever block is selected).
The general Python
if-else syntax isifcondition :indentedStatementBlockForTrueConditionelse:indentedStatementBlockForFalseCondition
These statement blocks can have any number of statements, and can include about any kind of statement.
1.1.4. More Conditional Expressions
All the usual arithmetic comparisons may be made, but many do not use standard mathematical symbolism, mostly for lack of proper keys on a standard keyboard.
| Meaning | Math Symbol | Python Symbols |
|---|---|---|
| Less than | < | < |
| Greater than | > | > |
| Less than or equal | ≤ | <= |
| Greater than or equal | ≥ | >= |
| Equals | = | == |
| Not equal | ≠ | != |
There should not be space between the two-symbol Python substitutes.
Notice that the obvious choice for equals, a single equal sign, is not used to check for equality. An annoying second equal sign is required. This is because the single equal sign is already used for assignment in Python, so it is not available for tests.
Warning
It is a common error to use only one equal sign when you mean to test for equality, and not make an assignment!
Tests for equality do not make an assignment, and they do not require a variable on the left. Any expressions can be tested for equality or inequality (
!=). They do not need to be numbers! Predict the results and try each line in the Shell:
An equality check does not make an assignment. Strings are case sensitive. Order matters in a list.
Try in the Shell:
When the comparison does not make sense, as a string is being compared to a number. so an exception will be shown.
Good morning maam
ReplyDeleteEmmanuel
Good morning mam
ReplyDeleteGood morning mam
ReplyDeleteSahil