[go: up one dir, main page]

0% found this document useful (0 votes)
7 views28 pages

ch04 3

The document provides an overview of mathematical and logical operators in Python, detailing their types, uses, and importance in programming. It covers arithmetic operators, unary and binary operators, augmented assignment operators, and boolean types, along with examples and evaluation rules. Additionally, it explains operator precedence and the significance of logical operators in creating new boolean values.

Uploaded by

AKRM YOUSSEF
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)
7 views28 pages

ch04 3

The document provides an overview of mathematical and logical operators in Python, detailing their types, uses, and importance in programming. It covers arithmetic operators, unary and binary operators, augmented assignment operators, and boolean types, along with examples and evaluation rules. Additionally, it explains operator precedence and the significance of logical operators in creating new boolean values.

Uploaded by

AKRM YOUSSEF
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/ 28

Mathematical and logical operators in Python

Computer Programming 1
Program: Intermediate Diploma in Programming and Computer Science

Course Code: APPC121

Amira Alotaibi

College: Applied College

Institution: University Of Jeddah


Outline

❑ Introduction to Operators in Python


❑ Using Arithmetic Operators
❑ Unary Operator & Binary Operator
❑ Arithmetic Expressions
❑ How to Evaluate an Expression
❑ Augmented Assignment Operators
Introduction to Operators in Python
What are Operators?

Operators are symbols or keywords that allow Python programs to perform computations,
comparisons, and decision-making.

Why are Operators Important?


• They manipulate data efficiently.
• They perform calculations and control program logic.
• They compare values and manage program flow.
Using Arithmetic Operators
Arithmetic Operators in Python
• Operators: +, -, *, /, //, %, **

Operator Examples:
Unary Operator & Binary Operator

• The +, - and * operators are straightforward, but note that the + and - operators can be both unary
and binary

• A unary operator has only one operand a binary operator has two

• For example, the operator in +5 is a unary operator to negate the number 5 whereas the - operator
in 4 - 5 is a binary operator for subtracting 5 from 4
Float and Integer Division ( / & // ) Operator

The (/ )operator performs a float division that results in a floating number . For example:

The //operator performs an integer division; the result is an integer, and any fractional part is
truncated. For example:
Exponentiation ( ** ) Operator

To compute 𝒂𝒃 (a with an exponent of b) for any numbers a and b, you can write a** b in Python. For
example:
Remainder (%) Operator

The operator (%), known as remainder or modulo operator , yields the remainder after
division.

The left side operand is the dividend and the right side operand is the divisor .

Therefore, 7 % 3 yields 1, 3 % 7 yields 3,12 % 4 yields 0, 26 % 8 yields 2, and 20 % 13 yields 7.


Remainder (%) Operator

• Remainder is very useful in programming.


• For example, an even number % 2 is always 0
• An odd number % 2 is always 1
• So you can use this property to determine whether a number is even or odd.
• You can also mod by other values to achieve valuable results.
Check Point #6

What are the results of the following expressions?


Expression Result
Arithmetic Expressions

• Python expressions are written the same way as normal arithmetic expressions
• Example

can be translated into a Python expression as:

(3 + 4 * x) / 5 – 10 * (y - 5) * (a + b + c) / x + 9 * (4 / x + (9 + x) / y)
How to Evaluate an Expression
You can safely apply the arithmetic rule for evaluating a Python expression
1.Operators inside parenthesis are evaluated first
• Parenthesis can be nested
• Expression in inner parenthesis is evaluated first
2.Use operator precedence rule
• Exponentiation (**) is applied first
• Multiplication(*) float division(/) integer division(//) and remainder operators(%) are applied
next
▪ If an expression contains several multiplication, division, and remainder operators they
are applied from left to right
• Addition (+) and subtraction(-) operators are applied last
▪ If an expression contains several addition and subtraction operators they are applied
from left to right
Example

Here is an example of how an expression is evaluated:


Augmented Assignment Operators

• Very often the current value of a variable is used, modified, and then reassigned back to
the same variable
For example, the following statement increases the variable count by 1:
count = count + 1
• Python allows you to combine assignment and addition operators using an augmented (or
compound) assignment operator
• For example:

count += 1
Augmented Assignment Operators
Caution

There are no spaces in the augmented assignment operators.


For example, + = should be +=.
Augmented Assignment Operators

Using +=, -=, *=


• Example:
Boolean Types, Values, and Expressions

❑ Boolean Data Types


❑ Relational Operators
❑ Boolean Variables
❑ Convert Boolean Value to Integer
❑ Convert Numeric Value to Boolean
Boolean Data Types
Often in a program you need to compare two values, such as whether x is
greater than y.
▪ Or if an inputted value, such as radius, is less than zero.
Comparison Operators

Caution
The equal to comparison operator is two equal signs (==), not a single equal sign (=).
The latter symbol is for assignment.
Boolean variable

• A variable that holds a Boolean value is known as a Boolean variable.


• A Boolean variable can hold one of the two values: True or False.
• For example:
lightsOn = True
convert Boolean to int
Boolean variable

▪ convert Boolean to int


You can use the int function to convert a Boolean value to an integer. For example:

Output

▪ bool function
You can also use the bool function to convert a numeric value to a Boolean value. For example:

Output
Logical Operators

❑ Truth Table for Operator not


❑ Truth Table for Operator and
❑ Truth Table for Operator or
❑ Program 6: Test Boolean Operators
Logical Operators

Logical operators, also known as Boolean operators, operate on Boolean values to create a new
Boolean value.
Truth Table for Operator (not)
Truth Table for Operator (and)
Truth Table for Operator (or)
Operator Precedence and Associativity
▪ Order in which Python evaluates operators:
1- The expression in the parentheses is evaluated first
2- The operators are applied according to the precedence rule and the associativity
rule.
▪ The precedence rule

You might also like