L03P
L03P
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
if x > 0:
print ('x is positive’)
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