Python Programming/Conditions

This lesson introduces conditions and exception handling.

Objectives and Skills
Objectives and skills for this lesson include:
 * Control Flow
 * The if statement
 * Exceptions
 * Errors and exceptions
 * Handling and raising Exceptions
 * Try, Finally and the with statement

Readings

 * 1)  Conditional (computer programming)
 * 2)  Exception handling
 * 3) Python for Everyone: Conditional execution

Multimedia

 * 1) YouTube: Python Tutorial for Beginners 6: Conditionals and Booleans - If, Else, and Elif Statements
 * 2) YouTube: Python for Informatics - Chapter 3 - Conditional Execution
 * 3) YouTube: If, Then, Else in Python
 * 4) YouTube: Python if elif else
 * 5) YouTube: How to Use If Else Statements in Python

Comparison Operations
There are eight comparison operations in Python.

Boolean Operations
The Boolean operations include and, or, and not.

Bitwise Operations
Bitwise operations only make sense for integers. Negative numbers are treated as their 2’s complement value.

If Statement
An if statement may contain zero or more elif parts, and the else part is optional. The keyword ‘elif‘ is short for ‘else if’, and is useful to avoid excessive indentation. An if ... elif ... elif ... sequence is a substitute for the switch or case statements found in other languages.

Try Statement
The try statement executes the try clause. If no exception occurs, the except clause is skipped. If an exception occurs during execution of the try clause, the except clause is executed, and then execution continues after the try statement.

Except Statement
A try statement may have more than one except clause, to specify handlers for different exceptions.

Tutorials

 * 1) Complete one or more of the following tutorials:
 * 2) * LearnPython
 * 3) ** Conditions
 * 4) ** Exception Handling
 * 5) * TutorialsPoint
 * 6) ** Decision Making
 * 7) ** Exceptions
 * 8) * Codecademy
 * 9) ** Conditionals and Control Flow
 * 10) * Wikiversity
 * 11) ** Python/If Statement
 * 12) ** Python/Try Statement
 * 13) * Wikibooks
 * 14) ** A Beginner's Python Tutorial/Loops, Conditionals
 * 15) ** A Beginner's Python Tutorial/Exception Handling

Practice

 * 1) Review Python.org: Truth Value Testing. Create a Python program that uses different values and conditions to confirm that in Python zero is false and anything non-zero is true. Also show that the explicit value of False is 0 and the explicit value of True is 1.
 * 2) Create a Python program that asks the user how old they are in years. Then ask the user if they would like to know how old they are in months, days, hours, or seconds. Use an if/elif/else statement to display their approximate age in the selected timeframe.
 * 3) Create a Python program to prompt the user for hours and rate per hour to compute gross pay (hours * rate). Include a calculation to give 1.5 times the hourly rate for any overtime (hours worked above 40 hours).
 * 4) Review MathsIsFun: Conversion of Temperature. Create a Python program that asks the user if they would like to convert Fahrenheit to Celsius or Celsius to Fahrenheit. Use an if/elif/else statement to determine their selection and then gather the appropriate input and calculate and display the converted temperature.
 * 5) Review MathsIsFun: Area of Plane Shapes. Create a Python program that asks the user what shape they would like to calculate the area for. Use an if/elif/else statement to determine their selection and then gather the appropriate input and calculate and display the area of the shape.
 * 6) Extend one or more of the programs above by adding a try/except block to handle any runtime errors caused by the user entering invalid values for the input.

Games

 * 1) Play CodeCombat.

Condition Concepts
""
 * Conditional statements are features of a programming language which perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false.
 * The pseudocode structure of a conditional statement is:
 * If the condition is true, the statements following the  are executed. Otherwise, the execution continues in the following branch – either in the   block (which is usually optional), or if there is no   branch, then after the.
 * By using /, it is possible to combine several conditions. Only the statements following the first condition that is found to be true will be executed. All other statements will be skipped.
 * Exception handling is the process of responding to the occurrence, during computation, of anomalous or exceptional events requiring special processing, often changing the normal flow of program execution.
 * In programming language mechanisms for exception handling, the term exception is typically used in a specific sense to denote a data structure storing information about an exceptional condition.
 * One mechanism to transfer control, or raise an exception, is known as a throw.
 * The scope for exception handlers starts with a "try" clause.
 * An exception is said to be thrown and execution is transferred to a "catch" or "except" statement.

Python Conditions
"" ""
 * The Python syntax for a conditional statement is:
 * An if statement may contain zero or more elif parts, and the else part is optional.
 * An if ... elif ... elif ... sequence is a substitute for the switch or case statements found in other languages.
 * There are eight comparison operations in Python, including ==, !=, <, >, <=, >=, is, is not.
 * There are three Boolean operations in Python, including and, or, not.
 * There are six bitwise operations in Python, including &, |, ^, ~, <<, >>.
 * Bitwise operations only make sense for integers. Negative numbers are treated as their 2’s complement value.
 * The Python syntax for a try statement is:
 * The try statement executes the try clause. If f no exception occurs, the except clause is skipped. If an exception occurs during execution of the try clause, the except clause is executed, and then execution continues after the try statement.
 * A try statement may have more than one except clause, to specify handlers for different exceptions.

Key Terms

 * body
 * The sequence of statements within a compound statement.


 * boolean expression
 * An expression whose value is either True or False.


 * branch
 * One of the alternative sequences of statements in a conditional statement.


 * chained conditional
 * A conditional statement with a series of alternative branches.


 * comparison operator
 * One of the operators that compares its operands ==, !=, >, <, >=, and <=.


 * conditional statement
 * A statement that controls the flow of execution depending on some condition.


 * condition
 * The boolean expression in a conditional statement that determines which branch is executed.


 * compound statement
 * A statement that consists of a header and a body. The header ends with a colon . The body is indented relative to the header.


 * guardian pattern
 * Where we construct a logical expression with additional comparisons to take advantage of the short-circuit behavior.


 * logical operator
 * One of the operators that combines boolean expressions and, or, and not.


 * nested conditional
 * A conditional statement that appears in one of the branches of another conditional statement.


 * traceback
 * A list of the functions that are executing, printed when an exception occurs.


 * short circuit
 * When Python is part-way through evaluating a logical expression and stops the evaluation because Python knows the final value for the expression without needing to evaluate the rest of the expression.

Assessments

 * Flashcards: Quizlet: Python Conditionals & Control Flow
 * Quiz: Quizlet: Python Conditionals & Control Flow