100% found this document useful (1 vote)
164 views

Lecture 4 - Control Structures in Python PDF

The document discusses control structures in Python, including conditionals like if, if-else, and if-elif statements and loops like while and for loops. It provides examples and explanations of how each control structure works, how to nest conditions, and how to use logical operators. It also discusses the range() function for iterating a specific number of times in a for loop.

Uploaded by

malvin muthoni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
164 views

Lecture 4 - Control Structures in Python PDF

The document discusses control structures in Python, including conditionals like if, if-else, and if-elif statements and loops like while and for loops. It provides examples and explanations of how each control structure works, how to nest conditions, and how to use logical operators. It also discusses the range() function for iterating a specific number of times in a for loop.

Uploaded by

malvin muthoni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Lecture 4 – Control Structures in

Python.

BBT 4211 - BCOM – Kurui Daniel


Content

• What are control structures?


• Types of Control Structures
• Conditionals
• Loops
• Lab Exercises

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 2


What are control structures?

• Control structures are portions of program code that contain statements


within them and, depending on the circumstances, execute these
statements in a certain way.
• In simple sentence, a control structure is just a decision that the computer
makes.

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 3


Types of control structures?

• There are typically two kinds: conditionals and loops.

Loops

Conditional

AGE >18 Adult

Finish 12 laps? Race is complete

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 4


Conditionals

• In order for a program to change its behavior depending on the input,


there must a way to test that input.

• Conditionals allow the program to check the values of variables and to


execute (or not execute) certain statements.

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 5


Conditionals cont’d

• Python has the following conditional structures.


• IF
• IF….ELSE
• ELIF
• Nested IF

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 6


IF Statement

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 7


IF statement

• In Python, If Statement is used for decision making. It will run the body of
code only when IF statement is true.
• When you want to justify one condition while the other condition is not
true, then you use "if statement".
• The easiest method for making a decision in Python is by using the if
statement. The if statement enables you to select the actions to perform if
the evaluated condition is true.
• The syntax of the if statement is:

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 8


IF statement

• In Python, If Statement is used for decision making. It will run the body of
code only when IF statement is true.
• When you want to justify one condition while the other condition is not
true, then you use "if statement".
• The easiest method for making a decision in Python is by using the if
statement. The if statement enables you to select the actions to perform if
the evaluated condition is true.
• The syntax of the if statement is:

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 9


IF statement cont’d

• What do you think the output of this example will be?

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 10


IF statement cont’d

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 11


IF …ELSE Statement

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 12


IF..ELSE statement

• The if…else statement in Python is used when you need to choose


between one of two alternatives. The else clause in the code will
be executed if the condition for the if statement isn’t met, which allows
you to perform an alternative task.
• The syntax of the if…else statement is:

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 13


IF..ELSE statement

• The if…else statement in Python is used when you need to choose


between one of two alternatives. The else clause in the code will
be executed if the condition for the if statement isn’t met, which allows
you to perform an alternative task.
• The syntax of the if…else statement is:

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 14


IF..ELSE statement cont’d

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 15


IF..ELSE statement cont’d

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 16


IF..ELSE statement cont’d

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 17


IF..ELIF Statement

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 18


IF..ELIF statement

• The if…elif statement in Python is used when you need to choose between
more than two alternatives.
• The elif clause can be used to create a number of conditions that can be
evaluated.
• The syntax of the statement is:

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 19


IF..ELIF statement

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 20


IF..ELIF statement

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 21


NESTED IF Statement

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 22


Nested IF statement

• You can place an if (or if…else, if…elif) statement inside another


statement.
• This process is called nesting and enables you to make complex decisions
based on different inputs.
• Here is a sample syntax:

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 23


Nested IF statement
• You can place an if (or if…else, if…elif) statement inside another
statement. This process is called nesting and enables you to make complex
decisions based on different inputs.
• Here is a sample syntax:

• Note how the lines above are indented. The indentation is important
because it informs Python that the indented statement is the second-level
statement.

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 24


Nested IF statement cont’d
• Consider this example

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 25


Nested IF statement cont’d
• Here is the output with some given values

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 26


Use of logical Operators with IF statements
• Logical operators in Python (and, or, not) are often used in
the if, if…else, and if…elif statements.
• They enable you to make multiple comparisons inside a single
statement, such as to determine whether a value is within a certain
range.

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 27


Use of logical Operators with IF statements cont’d
• Consider the following example:

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 28


Use of logical Operators with IF statements cont’d
• Another example

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 29


Use of logical Operators with IF statements cont’d
• Output

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 30


Loops
• Loops execute certain statements while certain conditions are met.
• A loop statement allows us to execute a statement or group of
statements multiple times. The following diagram illustrates a loop
statement

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 31


Loops

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 32


Loops cont’d
• Python programming language provides following types of loops to
handle looping requirements.
1. While loop
Repeats a statement or group of statements while a given condition is
TRUE. It tests the condition before executing the loop body.
2. For loop
Executes a sequence of statements multiple times and abbreviates the
code that manages the loop variable.
3. Nested loops
You can use one or more loop inside any another while or for loop.

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 33


While Loop

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 34


While Loop
• The while loop in Python is used to repeatedly execute statements as
long as the given condition is true.
• When the condition becomes false, the program execution is resumed
at the next statement after the body of the while loop.
• The syntax of the while loop is:

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 35


While Loop cont’d
• Here is a simple example of the while loop in action:

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 36


While Loop cont’d

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 37


Real life application of while loop
• You can use a while loop in a situation where you want to send
monthly reminders of customers who have active loans. So while the
loan status is active(not completely paid), send a reminder to the
customer

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 38


For Loop

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 39


For Loop
• One of the most common looping statements is the for loop.
• The syntax of this loop is:

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 40


For Loop
• Take a look at the following example:

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 41


For Loop with the range() function
• The range() function in Python is often used in for statements to define
the number of loop iterations.
• This built-in function creates lists containing arithmetic progressions.
The syntax of the range() function is:

• The start argument is the starting number. The stop argument is the last
number (which is not included). The step argument is optional and
defines the difference between each number in the sequence.
• Note: If the step argument is omitted, it defaults to 1. If
the start argument is omitted, it defaults to 0. Note that all arguments
have to be integers.

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 42


For Loop with the range() function cont’d
• Here is an example. Let’s say that we want to loop through a for loop 10
times. We can use the range(1, 11) function inside a for statement to do
that:

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 43


For Loop with the range() function cont’d

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 44


Nested loop statements

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 45


Nested loop statements
• You can place a loop statement inside another loop statement. This
process is called nesting.
• Consider the following example:

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 46


Nested loop statements cont’d
• You can place a loop statement inside another loop statement. This process
is called nesting.
• Consider the following example:

• The simple example above asks the user to enter a number and then prints
all numbers less than that number. The process will continue until the user
enters 0:

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 47


Nested loop statements cont’d

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 48


That was it!

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 49


Lab Exercise
• Write a python program to accept ones age and printout whether the
individual is an adult or not (adult > 18 years)
• Write a python program to print all integers between 0 and 1000 that
are prime (prime numbers)
• Write a python Program to check whether number is Even or Odd
• Write a python program to accept student’s marks for: Math, Physics,
Chemistry and Biology and print out their grades using the following
grade list : A - >80 B >70 C > 60 D >50 E <50
• Write a python program to accept the year as input and print out
whether it is a leap year or not.

20 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 50


END
Any Questions?
Thank you!
BBT 4211 - Comp Programming - Kurui Daniel

You might also like