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

L03P

The document explains the modulus operator in Python, which returns the remainder of a division, and provides examples of its usage. It also covers boolean expressions, logical operators, and conditional statements including if, if-else, and if-elif-else structures for decision-making in programming. An example of a grading program using if-elif-else is provided to illustrate how these concepts are applied.

Uploaded by

mohaalhabshi515
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)
2 views

L03P

The document explains the modulus operator in Python, which returns the remainder of a division, and provides examples of its usage. It also covers boolean expressions, logical operators, and conditional statements including if, if-else, and if-elif-else structures for decision-making in programming. An example of a grading program using if-elif-else is provided to illustrate how these concepts are applied.

Uploaded by

mohaalhabshi515
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/ 15

Python

If Conditions
Salem Belgurzi
Modulus Operator
 The modulus operator works on integers and yields the remainder
when the first operand is divided by the second. In Python, the
modulus operator is a percent sign (%). The syntax is the same as
for other operators:

>>> quotient = 7 / 3
>>> print (quotient)
2

>>> remainder = 7 % 3
>>> print (remainder)
1
Modulus Operator – Usage Examples
 Check whether one number is divisible by another—if x % y is zero,
then x is divisible by y.

 Also, you can extract the right-most digit or digits from a number.
For example, x % 10 yields the right-most digit of x (in base 10).
Similarly x % 100 yields the last two digits.
Boolean Expressions
 A boolean expression is an expression that is either true or false.
The following example use the operator ==, which compares two
operands and produces True if they are equal and False otherwise:

>>> 5 == 5
True
>>> 5 == 6
False
Boolean Expressions
 The == operator is one of the relational operators; the others are:

x != y # x is not equal to y

x>y # x is greater than y

x<y # x is less than y

x >= y # x is greater than or equal to y

x <= y # x is less than or equal to y


Logical Operators
 There are three logical operators: and, or, and not. The meaning
of these operators is similar to their meaning in English.

For example, x > 0 and x < 10 is true only if x is greater than 0


and less than 10

n%2 == 0 or n%3 == 0 is true if either of the conditions is true, that


is, if the number is divisible by 2 or 3

Finally, the not operator negates a boolean expression, so


not (x > y) is true if x > y is false, that is, if x is less than or equal
to y
If Conditions
 In order to write useful programs, we almost always need the ability
to check conditions and change the behavior of the program
accordingly. Conditional statements give us this ability. The
simplest form is the if statement:

if x > 0:
print ('x is positive’)

The boolean expression after if is called the condition. If it is true,


then the indented statement gets executed. If not, nothing
happens.
Topic
 The if-else Statement
The if-else Statement (1 of 3)
 Dual alternative decision structure: two possible paths of execution
– One is taken if the condition is true, and the other if the condition is
false
 Syntax: if condition:
statements
else:
other statements
 if clause and else clause must be aligned
 Statements must be consistently indented
The if-else Statement (2 of 3)
The if-else Statement (3 of 3)
The if-elif-else Statement
 if-elif-else statement: special version of a decision structure
 Makes logic of nested decision structures simpler to write
 Can include multiple elif statements
 Syntax:

if condition_1:
statement(s)
elif condition_2:
statement(s) Insert as many elif clauses
elif condition_3: as necessary.
statement(s)
else
statement(s)
The if-elif-else Statement
Example:
Write a Python program that takes a user's numeric grade as
input and converts it to a letter grade based on common grading
scales
The if-elif-else Statement

 Nested decision structure to determine a grade


The if-elif-else Statement
grade = int(input('Enter the grade as an integer: '))
if grade >= 90:
print('A')
elif grade >= 80:
print('B')
elif grade >= 70:
print('C')
elif grade >= 60:
print('D')
else:
print('F')

You might also like