Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Class07 Python II

Uploaded by

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

Class07 Python II

Uploaded by

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

4/4/2023

SIJ1004
Introduction to Computational Biology

Class 7. Python part 2

Control structures
• Computer programs = sequences of instructions (followed one after the other).

• Sequence is a fundamental concept of programming, but alone, it is not sufficient


to solve every problem.

• Altering the sequential flow of a program to suit the needs of a particular


situation.

• Control structures  commands that enable a program to “take decisions”,


following one path or another.
• Sequential control structure: The execution of the program statements happens
line by line.
• Selection control structure: Decision making
• Iteration control structure: Repeat command.

1
4/4/2023

Control structures - Conditional Statements


• Also called “Decision structures”.
• One type of control structures, are statements that allow a
program to execute different sequences of instructions for
different cases,
• Decision making  the basic decision statement in programming
is selection structure.
• The decision is described to computer as a conditional statement
that can be answered “True” or “False” (Boolean condition).
• Conditional statement in Python:
• If statement
• if … else statement
• if … elif … else statement
• Nested if … else statement

1. if statement
• Allow the program to respond to input from user.
• A condition:
• True or false
• Value

2
4/4/2023

2. if … else statement
• Alternative execution.
• Two possibilities determined by the
conditions
• Allow the program to respond to
input from user.
• A condition:
• True or false
• Value

3. if … elif … else statement


• elif  a keyword used in Python for “else if”.
• To place another condition in the program.
• Chained conditional. Allows more than two possibilities
 need more than 2 branches

3
4/4/2023

if statement & comparison


• comparison operators
– ==
– !=
– <=

4. Nested if statement
• We can write an entire “if … else” statement in another “if … else” statement.

Example:

4
4/4/2023

Multi-way decisions: if-elif-else


• A special kind of nesting is the chain of if-
else-if-else-… statements
• Can be written elegantly using if-elif-..-else
if cond1: if cond1:
s1 s1
else: elif cond2:
if cond2: s2
s2 elif cond3:
else: s3
if cond3: elif …
s3 else
else: last-block-of-stmt

Control structures - Looping Statements

• Repetitive program codes


• Looping statement in Python:
• while statement
• for loop statement
• Nested for loop statement

5
4/4/2023

1. while loop statement


• A Python program that repeatedly executes a target
statement as long as a given condition is true

2. for loop statement


• Another repetitive control structure
used to execute a target statement
until the condition becomes false.
• Used to iterate over a sequence (list,
tuple, string).

• Initiating by specifying a variable and


command the action

You might also like