0% found this document useful (0 votes)
9 views6 pages

Python 3

The document discusses Python conditional operations and loops. It covers if/else statements, nested if, elif, while, for, and break statements. Examples are provided to illustrate each concept. Flowcharts are also included to demonstrate program logic.

Uploaded by

Alex Yuen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Python 3

The document discusses Python conditional operations and loops. It covers if/else statements, nested if, elif, while, for, and break statements. Examples are provided to illustrate each concept. Flowcharts are also included to demonstrate program logic.

Uploaded by

Alex Yuen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Special Funding Scheme for Innovation in Virtual Education in Business, CUHK

Learning Financial Analysis with Python from Scratch

Module 3 Conditional Operations and Loops


*We will use Colab for illustration in this note, while most of them work in Jupyter as well.

3.1 If statement
If statement allows us to choose which statements to execute.
 if statement checks whether a given condition is met.
 When the condition is true, a set of codes will be executed.
 Otherwise, an alternative set of codes will be executed (or doing nothing).
 To understand how if statement works, let’s type the following code.
1. scale = input("Do you like learning Python (1-100)? ")
2. scale = float(scale) #convert string to float
3. Q6
4. print("Good to know that you like it!")
5. print("Let’s learn more about coding!")
 The section of code to run when the condition is met has to be indented.
 Indentation can simply make by pressing “Enter” after the “:” in an if statement
 You can also make the indentation by ”Tab” or inserting spaces.
 More than one statement can be controlled by a conditional.
 Each statement should use the same indentation
 You can simply press “Enter” after the first statement to make the same indentation.
 Insert a line after line 4, to print a message "Please like and subscribe! " when scale >= 80.
 Python is a case-sensitive language, so IF and If are not working.

3.2 Else
Else statement allows you to perform an action when the given condition is not met.
 Please insert the following lines after printing the message "Please like and subscribe".
6. else:
7. print("The later chapters would be more interesting!")
 An else statement should have the same indentation level as the if statement.
 To delete an indentation level, you can press “Shift”+“Tab” or “Backspace”.
 The action taken when the else condition is met has to have one indentation level
higher than the else statement.

PHU Jacqueline & Dr. Edwin MOK 1


3.3 Nested if statement
If statement can be nested inside another if statement.
 Please be reminded that each pair of if statement and else statement should have the same
indentation level.
1. scale = input("Do you like learning Python (1-100)?")
2. scale = float(scale) #convert string to float
3. if scale >= 80:
4. print("Good to know that you like it!")
5. Q7
6. if scale >= 60:
7. print("The later chapters would be more interesting!")
8. else:
9. print("Cheer up! You will like it one day!")
10. print("Let’s learn more about coding!")

3.4 Elif
Elif statement helps us to make nested if statements easier to code and read.
 if statement, elif statement and else statement should have the same indentation level.
5. elif scale >= 60:
6. print("The later chapters would be more interesting! ")
7. else:
8. print("Cheer up! You will like it one day! ")
9. print("Let’s learn more about coding! ")

PHU Jacqueline & Dr. Edwin MOK 2


3.5 Flow chart
A good practice to code nested conditional statements is to write a flow chart before coding.
 Flow chart for the program in 3.4 is provided in Diagram 1.
 If statement and elif statement are represented by a diamond-shaped box to show two
possible outcomes.
 The condition is written inside the box.
 An arrow line links conditional statements to actions to be performed.
 The path of the arrow line depends on the outcome of the conditional.
 It indicates which actions should be performed.
 The outcomes are written nearby the arrow line with values “Yes” or “No”.
 Actions to be performed are represented by a square box.

Diagram 1
Flow chart for the if function

PHU Jacqueline & Dr. Edwin MOK 3


3.6 While loop
While loop is used when we want to repeat a series of statements several times until a condition is
not met.
 The body of the loop must be indented
 The body starts after the “:” and ends by the first un-indented line.
 Please try the following code:
1. investment = input("How much do you invest today? ")
2. returns = input("What is your annual return (in percentage)? ")
3. target = input("How much do you want to have in the future? ")
4. i, r, t = float(investment), float(returns), float(target)
5. year = 0
6. Q8
7. i = i + i * r / 100
8. year = year + 1
9. print("You have ",i," after ",year," years")
10. print("It takes you ",int(year)," years to meet your financial target.")
 Most of the time, while loop requires a statement to initialize a variable for counting purposes
and another statement for increment.
 What if the input of annual return is 0?
 While loop could lead to endless iteration.
 Pressing “Ctrl” + “C” can interrupt the execution of the program.
 In Colab, you can click the run icon to stop the execution.
 Flow chart for the program is provided in Diagram 2.

Diagram 2
Flow chart for the while loop

PHU Jacqueline & Dr. Edwin MOK 4


3.7 Break
When the condition is True, the action in the loop will play repeatedly until a break statement
 A break statement ends a loop in the middle of an execution.
 Replace statements since line 6 in 3.6 with the following code:
6. Q9
7. i = i + i * r / 100
8. year = year + 1
9. print("You have ",i," after ",year, " years")
10. if i >= t:
11. break
12. print("It takes you ",int(year), " years to meet your financial target.")

3.8 For statement


The for statement iterates a series of statements over a sequence.
 The sequence starts from the first argument and ends up to but does not include the second
argument.
 The number in the range has to be an integer.
 Replace statements since line 3 in 3.6 with the following code:
3. time = input(‘How many years do you want to invest? ’)
4. i, r, time = float(investment), float(returns), int(time)
5. Q10
6. i = i + i * r / 100
7. print("You will have ",i," after ",time," years. ")
 Why did the first element set to 0? How many times did the program run?
 For statement does not require setting up a variable for counting purposes.
 Flow chart for the program is provided in Diagram 3.

PHU Jacqueline & Dr. Edwin MOK 5


Diagram 3
Flow chart for the for statement

3.9 Exercises
1. Write a program to provide the suggested energy consumption per day based on their gender and
age. The following is the suggested calorie consumption:

Age (year) Male Female


18 - <30 2400 1900
30 - <60 2350 1850
>=60 1950 1700

Hints:
 gender = input("Please input your gender (M for male and F for female): ")
 age = int(input("Please input your age: "))
 if age >= 18:

2. Use while loop to ask a user to input again if the first is neither “M” or “F”.

3. Write a program to ask a user to input an integer between 2 and 10,000, and identify whether it is a
prime number.

4. Write a program to ask a user to input a positive integer, and calculate the number of digits of this
number.
End of Module 3

PHU Jacqueline & Dr. Edwin MOK 6

You might also like