Lecture 03
Lecture 03
Python I
Lecture 03:
Selection Control Structures
1
Outline
• Relational and Logical Operators
• if-else Statements
• if statement
• if-elif-else statement
• Algorithms
2
Relational and Logical Operators
• A condition is an expression
• Involving relational operators (such as < and >=)
• Logical operators (such as and, or, and not)
• Evaluates to either True or False
• Form:
if-else Statements
else:
12
if-else Statements
• Example: Program finds larger of two numbers input by user.
if number1 == number2:
print(number1, 'is equal to', number2)
if number1 != number2:
print(number1, 'is not equal to', number2)
17
if-elif-else Statements
else:
18
Algorithms
19
Algorithms
• Any computing problem can be solved by executing a series of actions in a
specific order.
• An algorithm is a step-by-step procedure for solving a problem in terms of:
• the actions to execute and
• the order in which these actions execute
• Specifying the order in which statements (actions) execute in a program is called
program control.
• An algorithm can expressed in two way:
• Pseudocode
• Flowchart
20
Pseudocode
• An informal language that helps you develop algorithms without having to
worry about the strict details of programming language syntax.
• Similar to everyday English.
• Helps you “think out” a program before attempting to write it in a
programming language.
• Pseudocode normally describes only statements representing the actions that
occur (e.g., input, output or calculations) after you convert a program from
pseudocode to Python and run the program.
21
Addition-Program Pseudocode
1. Prompt the user to enter the first integer
2. Input the first integer
3. Prompt the user to enter the second integer
4. Input the second integer
5. Add first integer and second integer, store their sum
6. Display the numbers and their sum
22
Flowchart
• Graphical representation of an algorithm or a part of one.
• Drawn using rectangles, diamonds, rounded rectangles and small
circles connected by arrows called flowlines.
23
Flowchart for Sequential Execution
• Sequential Execution: statements in a program are executed in the
order in which they’re written.
24
if statement Flowchart
25
if-else statement Flowchart
26
if-elif-else statement Flowchart
27