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

Modul 3 Python

blabalbla

Uploaded by

haikal.pkl24
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Modul 3 Python

blabalbla

Uploaded by

haikal.pkl24
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Modul 3 : Conditional

Muh. Sulkifly Said, S.SI., M.Eng


Conditional Statements

conditional statements are used to control the flow of a program based on certain
conditions. These statements allow the execution of different code blocks depending on
whether a specified condition evaluates to True or False.
Nested Conditionals

In Python, a nested conditional statement is a situation where one or more conditional


statements (such as if, elif, and else) are nested inside another conditional statement.
This allows for more complex decision-making logic based on multiple conditions.
Conditional / Nested Conditional Statements

● The if Statement
● The if-else Statement
● The if-elif-else Statement
● Nested Conditionals
● Ternary Conditional Operator
● Truthy and Falsy Values
The if Statement

In Python, the if statement is a fundamental control flow structure used to execute a


block of code conditionally.

The if statement evaluates a specified condition, and if the condition is True, the
indented block of code beneath it is executed

If the condition is False, the block is skipped.


Example : The if Statement
The if-else Statement

the if-else statement is used to create conditional logic where a block of code is
executed if a specified condition is True, and a different block of code is executed if the
condition is False

This structure allows the program to take different paths based on whether the given
condition is met or not
Example : The if-else Statement
The if-elif-else Statement

the if-elif-else statement is used to create a sequence of conditions where each


condition is evaluated in order. If one of the conditions is True, the corresponding block
of code is executed, and the rest of the conditions are skipped

If none of the conditions is True, the block of code under the else statement is executed.
Example : if-elif-else Statement
Ternary Conditional Operator

Ternary conditional statements provide a more concise syntax


Example : Ternary Conditional Operator
Boolean expressions

boolean expressions are expressions that evaluate to either True or False.

These expressions typically involve the use of comparison operators, logical operators,
and other constructs that yield boolean results

Boolean expressions are fundamental to control flow structures, such as if statements,


loops, and more
Boolean expressions

● Comparison Operator
● Logical Operator
● Membership Operator
● Nested Conditionals
● Ternary Conditional Operator
● Truthy and Falsy Values
Comparison Operator

== (equal to): Returns True if the values on both sides are equal.

!= (not equal to): Returns True if the values on both sides are not equal.

< (less than): Returns True if the value on the left is less than the value on the right.

> (greater than): Returns True if the value on the left is greater than the value on the right.

<= (less than or equal to): Returns True if the value on the left is less than or equal to the value on the right.

>= (greater than or equal to): Returns True if the value on the left is greater than or equal to the value on the
right.
Example : Comparison Operator
Logical Operator

Logical operators are used to combine boolean expressions. The common logical
operators in Python are:

● and: Returns True if both boolean expressions on the left and right are True.
● or: Returns True if at least one of the boolean expressions on the left or right is
True.
● not: Returns the opposite boolean value of the expression after it.
Example Logical Operator
Membership Operator

Membership operators are used to test whether a value is a member of a


sequence (e.g., a list, tuple, or string). The common membership
operators are:

● in: Returns True if the specified value is found in the sequence.


● not in: Returns True if the specified value is not found in the
sequence.
Example Membership Operator
Truthy and Falsy Value

In Python, values other than False, None, 0, "" (empty string),


[] (empty list), {} (empty dictionary), and () (empty tuple) are
considered truthy. All other values are considered falsy.
Example Truthy and Falsy Value
Catching exceptions using try and except

the try and except statements are used to handle exceptions, which are
events that can occur during the execution of a program and disrupt its
normal flow

By using try and except, you can catch and handle exceptions gracefully,
preventing the program from crashing and allowing for more robust error
handling.
Example Catching exceptions using try and except

You might also like