0% found this document useful (0 votes)
3 views

comp_python_lecture_05

This document covers control structures in Python programming, focusing on Boolean data types, relational and logical operators, and conditional statements. It explains how to use if, if-else, if-elif-else, and nested if-else statements to control the flow of code based on conditions. Additionally, it includes examples and an exercise for practical application of these concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

comp_python_lecture_05

This document covers control structures in Python programming, focusing on Boolean data types, relational and logical operators, and conditional statements. It explains how to use if, if-else, if-elif-else, and nested if-else statements to control the flow of code based on conditions. Additionally, it includes examples and an exercise for practical application of these concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

COMP-122

INTRODUCTION TO COMPUTER
PROGRAMMING
Lecture 5: Control Structures

CSIT Department | MIT


Outline
+Boolean Data Type
+Relational / Comparison Operators
+Logical Operators
+Conditional Statements
Boolean Data Types
+In Python, to represent Boolean values, True and False, we use
the bool data type.
+Boolean values are used to evaluate the value of the expression.
For example, when we compare two values, the expression is
evaluated, and Python returns the Boolean True or False.
+Like any other value, Boolean values are used in expressions and
can be stored in variables. If you don’t use the proper case or
you try to use True and False for variable names, Python will
give you an error message.
Boolean Data Types Cont.
+For Example:
Relational Operators
+Relational operators are also called comparison operators.
+It performs a comparison between two values. It returns a
Boolean True or False depending upon the result of the
comparison.
Logical Operators
+Logical operators are useful when checking if a condition is true or
false.
+Python has three logical operators. All logical operator returns a
Boolean value True or False depending on the condition in which it is
used.
Operator Description Example

True if both the operands


and (Logical and) a and b
are True

True if either of the


or (Logical or) a or b
operands is True

not (Logical not) True if the operand is False not a


Logical Operators Cont.
+For Example:
Conditional Statements
+In Python, condition statements (also known as control
statements) act depending on whether a given condition is true
or false.
+You can execute different blocks of codes depending on the
outcome of a condition. Condition statements always evaluate
to either True or False.
+These are conditional statements:
❖if statement
❖if-else
❖if-elif-else
❖nested if-else
The if Statement
+An if statement tells Python to only execute a portion of code if
a condition is met.
+It takes a condition and evaluates to either True or. False
+If the condition is True, then the True block of code will be
executed, and if the condition is False, then the block of code is
skipped, and The controller moves to the next line.
The if Statement Cont.
+In Python, an if statement consists of the following:
i. The if keyword
ii. A condition (that is, an expression that evaluates to True or
False)
iii. A colon
iv. Starting on the next line, an indented block of code
The if Statement Cont.
+The Syntax of if statement:

+Example:
The if - else statement
+The if-else statement checks the condition and executes the if
block of code when the condition is True, and if the condition is
False, it will execute the else block of code.
+Syntax of the if-else statement:

+If the condition is True, then statements 1 and n will be executed


If the condition is False, statements 2 and n will be executed
The if - else statement Cont.
+Example:
The if-elif-else statement
+In Python, the if-elif-else condition statement has an elif blocks to chain
multiple conditions one after another. This is useful when you need to
check multiple conditions.
+Just like if statements, elif statements have three parts:
i. The elif keyword
ii. A test condition, followed by a colon
iii. An indented code block that is executed if the test condition evaluates
to True
The if-elif-else Statement cont.
+Syntax of the if-elif-else statement:
The if-elif-else Statement cont.
+Example:
Nested if-else statement
+In Python, the nested if-else statement is an if statement inside
another if-else statement.
+It is allowed in Python to put any number of if statements in
another if statement.
+Indentation is the only way to differentiate the level of nesting.
The nested if-else is useful when we want to make a series of
decisions.
Nested if-else statement cont.
+Syntax of the nested-if-else:
Nested if-else statement cont.
+ Example: Find a greater number between two numbers
Exercise
Write a script that prompts the user to enter a word using the input()
function, stores that input in a variable, and then displays whether the length
of that string is less than 5 characters, greater than 5 characters, or equal to 5
characters by using a set of if, elif, and else statements.

You might also like