1 of 20

If…else statement

2 of 20

How to use if … else statement

  • Normally, if … else statement is used for checking variables matching a setting condition or not.
  • It usually works with comparison, logical, identity and membership operators.
  • The format of if statement is:

Start from if

Condition needs to be matched

Condition should end with :

If the condition is satisfied,

statement 1 will be executed.

Otherwise, statement will not be

performed

4 spaces

3 of 20

If with operators

  • If statement with comparison operators
  • If statement with logical operators

4 of 20

If with operators

  • If statement with identity operators
  • If statement with membership operators

5 of 20

If…else statement

  • else is executed when the condition of if statement is not matched
  • The format of if … else statement is:

Start from else

If the condition of if statement is not

satisfied, statement 2 will be

executed. Otherwise, statement 1

will be executed.

4 spaces

6 of 20

If…else statement

  • Only use if statement
  • Use if … else statement

7 of 20

If … elif … else statement

  • When more than 2 conditions need to be considered, elif can be used to achieve the purpose. elif is executed when the condition of if statement is not matched. Based on elif, else is executed when the conditions from if and elif are all not matched.
  • The format of if … elif … else statement is:

Start from elif

If the condition of if statement Is not

satisfied, statement 2 will be

executed. Otherwise, statement 1

will be executed.

4 spaces

If the condition 1 and 2 are not satisfied,

statement 3 will be executed.

8 of 20

If…elif … else statement

  • Only use if statement
  • Use if … else statement

67

67

97

37

97

37

weight

weight

9 of 20

Exercise

  • Recently, a new drug which can completely cure a type of lung cancer was found. It can recover the patients who has a GeneA mutation. However, this drug will temporary increase the blood pressure. Thus, the patients who have high blood pressure cannot be treated by this drug. Could you help us to create a system to check which patients can be treated by this new drug?

10 of 20

Nested if … else statement

  • If the conditions depends on each other and are ordered, the nested if … else statement is the way to manage them.
  • Nested if … else statement: one if statement embedded in the other one.
  • We can rewrite this script to be more practical.

11 of 20

Sometimes, if we can arrange nice order of statement, it may also work without nested one

12 of 20

Try and except

  • The try except statement can handle exceptions. Exceptions may happen when you run a program.
  • Exceptions are errors that happen during execution of the program. Python won’t tell you about errors like syntax errors (grammar faults), instead it will abruptly stop.
  • How to define try and except statement:

No error occurs, it

will <do something>

If try will cause error, Exception error

will <handle the error>

13 of 20

Try and except

We can also assign the type of error to except for control the output error message

14 of 20

Try except finally

  • The finally statement will always be executed no matter the program is going to try or except statement.

15 of 20

Try except else finally

  • The else clause is executed if and only if no exception is raised. This is different from the finally clause that’s always executed.

16 of 20

Raise exception

  • Exceptions are raised when an error occurs. But in Python you can also force an exception to occur with the keyword raise.

17 of 20

User defined exception

  • Sometimes, you need some exceptions which is not built-in in Python. Then you can define by yourself via a very simple class (we will go to details in the following weeks).

18 of 20

Exercise

  • Please generate a script that the user can key in a value of dose for a medicine by number only. Moreover, it can handle invalid input, such as string “1000d”.

19 of 20

Weather

sunny windy raining

happy sad

Blue

Black

Green

big

Red

small

White

20 of 20