python in Simple Conditions
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history.
python in Simple Conditions
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history.
python in Simple Conditions
The statements will be included in tests or conditions. More syntax for conditions will be introduced later, but for now, consider simple arithmetic comparisons that directly translate from math into Python. Try
2 <5
3> 7
x = 11
x> 10
2 * x <x
type (True)
You see that conditions are either True or False (with no quotes!). 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 results of true-false conditions or tests.
Simple if Statements
Run this example program, suitcase.py. Try it at least twice, with inputs: 30 and then 55. As you can see, you get an extra result, depending on the input. The main code is:
weight = float(input("How many pounds does your suitcase weigh? "))
if weight > 50:
print("There is a $25 charge for luggage that heavy.")
print("Thank you for your business.")
The middle two line is an 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 is
if condition :
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
if balance < 0:
transfer = -balance
# transfer enough from the backup account:
backup account = backup account - transfer
balance = balance + transfer
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, it is brought back to 0 by transferring money from a backup account in several steps.
In the examples above the choice is between doing something (if the condition is True) or nothing (if the condition is False). Often there is a choice of two possibilities, only one of which will be done, depending on the truth of a condition.
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:
temperature = float(input('What is the temperature? '))
if temperature > 70:
print('Wear shorts.')
else:
print('Wear long pants.')
print('Get some exercise outside.')
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 the 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 outdented next, about getting exercise. Since it is outdented, it is not a part of the if-else statement: 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 is
if condition :
indentedStatementBlockForTrueCondition
else:
indentedStatementBlockForFalseCondition
These statement blocks can have any number of statements and can include about any kind of statement.
Comments
Post a Comment