SlideShare a Scribd company logo
Mohammad Hassan
Operators
• Operators
• Arithmetic operators
• Including mod and integer division
• Expression
• Priorities of operators
• Bindings of operators
• Variables
• Rules for naming
• Different types
• How to use them
• Printing output to the screen
• Getting input from the user
• Assignment operators
• Comparison operators
• Boolean operators
Objectives
Python Basic Operators
An operator is a symbol of the programming language, which
is able to operate on the values.
Operators are the constructs which can manipulate and
evaluate our data.
Consider the expression:
num = 4 + 5
operator
Types of Operators in Python
Arithmetic Operators
Assignment Operators
Comparison Operators
Logical Operators
Membership Operators
Bitwise Operators
Identity Operators
focus of today’s lecture
Arguments
when both arguments are integers, the result is an
integer, too;
when at least one argument is a float, the result is a
float, too.
Operators – Addition & Subtraction
the + (plus) sign is the operator which is able to add two
numbers, giving the result of the addition.
“Lowest” priority in the order of operations
Function as they normally do
Examples:
1. cash = cash - bills
2. (5 + 7) / 2
3. ( ((2 + 4) * 5) / (9 - 6) )
4. -4 + 4 = 0
5. -4. + 8 = 4.0
The subtraction operator, unary and
binary operators
The subtraction operator is obviously the - (minus) sign, although you
should note that this operator also has another meaning ‒ it can change
the sign of a number.
This is a great opportunity to present a very important distinction
between unary and binary operators.
In subtracting applications, the minus operator expects two arguments:
the left (a minuend in arithmetical terms) and right (a subtrahend).
For this reason, the subtraction operator is considered to be one of the
binary operators, just like the addition, multiplication and division
operators.
-4 – 4= - 8
4. – 8 = - 4.0
-1.1= - 1.1
Operators – Multiplication & Division
An * (asterisk) sign is a multiplication operator.
A // (slash) sign is a division operator.
The value in front of the slash is a dividend, the value behind the slash,
a divisor.
The result produced by the division operator is always a float, regardless
of whether or not the result seems to be a float at first glance: 1/2, or if it looks
like a pure integer: 2/1.
Higher priority in the order of operations
than addition and subtraction
Function as they normally do
Examples:
1. tax = subtotal * 0.06
2. area = PI * (radius * radius)
3. totalDays = hours / 24
Operators – Integer Division
Reminder: integers (or ints) are whole numbers
What do you think integer division is?
A // (double slash) sign is an integer division operator. It differs from the
standard / operator in two details:
•its result lacks the fractional part ‒ it's absent (for integers), or is always equal to zero (for
floats); this means that the results are always rounded; rounding always goes to the
lesser integer.
•it conforms to the integer vs. float rule.
•integer by integer division gives an integer result. All other cases
produce floats.
Remember division in grade school?
Integer division is
 Division done without decimals
 And the remainder is discarded
 Integer division can also be called floor division
How not to divide
As you probably know, division by zero doesn't work.
Do not try to:
•perform a division by zero;
•perform an integer division by zero;
•find a remainder of a division by zero
Examples: Integer Division
Integer division uses double slashes (//)
Examples:
1. 7 / 5 = 1.4
2. 7 // 5 = 1
3. 2 / 8 = 0.25
4. 2 // 8 = 0
5. 4 // 17 // 5 = 0
6. -6 // 4 = -2
7. 6. // -4 = -2.0
evaluate from left to right
Operators – Mod
Also called “Remainder” or “modulo” or “modulus”
 Its graphical representation in Python is the % (percent) sign, which may look a bit
confusing.
Example: 17 % 5 = 2
 What do you think mod does?
 The result of the operator is a remainder left after the
integer division.
Remember division in grade school?
Modulo gives you the remainder
 The “opposite” of integer division
Examples: Mod
Mod uses the percent sign (%)
Examples:
1. 7 % 5 = 2
2. 5 % 9 = 5
3. 16 % 6 = 4
4. 23 % 4 = 3
5. 48692451673 % 2 = 1
6. 12 % 4.5 = 3.0
Modulo Answers
Result of a modulo operation will always be:
 Positive
 No less than 0
 No more than the divisor minus 1
Examples:
1. 8 % 3 =
2. 21 % 3 =
3. 13 % 3 =
2
0
1 no less than zero
no more than
the divisor minus
1
Operators – Exponentiation
“Exponentiation” is just another word for raising one
number to the power of another
Examples:
1. binary8 = 2 ** 8
2. squareArea = length ** 2
3. cubeVolume = length ** 3
4. squareRoot = num ** 0.5
Exponentiation
We've surrounded the double asterisks with spaces in our
examples. It's not compulsory, but it improves
the readability of the code.
2 ** 3 = 8
2 ** 3. = 8.0
2. ** 3 = 8.0
2. ** 3. = 8.0
Floating Point Errors
Division: Floats and Integers
Floats (decimals) and integers (whole numbers) behave in
two different ways in Python
 And in many other programming languages
Biggest difference is how their division works
 Python 3 automatically performs decimal division
 For both integers and floats
 Have to explicitly call integer division
Division Examples
What do the following expressions evaluate to?
1. 4 / 3
2. 4 // 3
3. 8 / 3
4. 8. / 2
5. 5 / 7
6. 5 // 7
= 1.3333333333333333
= 1
= 2.6666666666666667
= 4.0
= 0.7142857142857143
= 0
Rounding Errors
Sometimes we need to approximate the representation
of numbers
 0.66666666666666666666666667…
 3.14159265358979323846264338328…
We know that this can lead to incorrect
answers when doing calculations later
 Something similar happens when numbers
are stored in a computer’s memory
Float Arithmetic Examples
What do the following expressions evaluate to?
1. 8 / 3
2. 5 / 7
3. 1.99 + 0.12
4. 0.99 + 0.12
5. 1.13 * 1.19
= 2.6666666666666667
= 0.7142857142857143
= 2.11
= 1.1099999999999999
= 1.3446999999999998
Because computers store
numbers differently, they
sometimes run into different sets
of rounding errors
What’s
going on
here???
Handling Floating Point Errors
How to fix floating point errors?
You can’t!
¯_(ツ)_/¯
 They’re present in every single programming language
that uses the float data type
Just be aware that the problem exists
 Don’t rely on having exact numerical representations
when using floats in Python
Expression
Data and operators when connected together
form expressions. The simplest expression is a literal
itself.
Operators and their priorities
We've treated each operator as if it had no connection with
the others. Obviously, such an ideal and simple situation is a
rarity in real programming.
you will very often find more than one operator in one
expression, and then things are no longer so simple.
The order of their appearance is not accidental.
The phenomenon that causes some operators to act before
others is known as the hierarchy of priorities.
Python precisely defines the priorities of all operators, and
assumes that operators of a higher priority perform their
operations before the operators of a lower priority.
Arithmetic Operators in Python
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
// Integer division
% Modulo (remainder)
** Exponentiation
Order of Operations (Arithmetic)
Expressions are evaluated in what direction?
What can change this ordering?
 Parentheses!
from left to right
Operator(s) Priority
highest
lowest
**
* / // %
+ -
Operators and parentheses
you're always allowed to use parentheses, which can
change the natural order of a calculation.
In accordance with the arithmetic
rules, subexpressions in parentheses are always
calculated first.
You can use as many parentheses as you need, and
they're often used to improve the readability of an
expression, even if they don't change the order of the
operations.
An example of an expression with multiple parentheses
is here:
10.0
print((5 * ((25 % 13) + 100) / (2 * 13)) // 2)
Operators and their bindings
The binding of the operator determines the order of
computations performed by some operators with equal
priority, put side by side in one expression.
Most of Python's operators have left-sided binding,
which means that the calculation of the expression is
conducted from left to right.
(9 % 6 % 2) =1
There are two possible ways of evaluating this
expression:
• from left to right: first 9 % 6 gives 3, and then 3 %
2 gives 1;
• from right to left: first 6 % 2 gives 0, and then 9 %
0 causes a fatal error.
Operators and their bindings
But there's one interesting exception.
Use this snippet of code:
print(2 ** 2 ** 3) -> 256
The two possible results are:
•2 ** 2 → 4; 4 ** 3 → 64
•2 ** 3 → 8; 2 ** 8 → 256
•The result clearly shows that the exponentiation operator uses right-
sided binding.
print(-3 ** 2) -> -9
print(-2 ** 3) -> -8
print(-(3 ** 2)) -> 9

More Related Content

PPTX
Session 4.pptx
PPTX
Operators in Python
PDF
Operators in python
PDF
Variables in Python & Data Types and Their Values
PPTX
Operators in Python Arithmetic Operators
PPTX
Python Programming | JNTUK | UNIT 1 | Lecture 5
PDF
Unit ii chapter 1 operator and expressions in c
PDF
python2oxhvoudhuSGFsughusgdogusuosFU.pdf
Session 4.pptx
Operators in Python
Operators in python
Variables in Python & Data Types and Their Values
Operators in Python Arithmetic Operators
Python Programming | JNTUK | UNIT 1 | Lecture 5
Unit ii chapter 1 operator and expressions in c
python2oxhvoudhuSGFsughusgdogusuosFU.pdf

Similar to Lecture 05.pptx (20)

PPTX
OPERATOR IN PYTHON-PART1
PDF
ICP - Lecture 5
PPTX
Introduction to Python Values, Variables Data Types Chapter 2
PPTX
Operators Concept in Python-N.Kavitha.pptx
PPTX
Precedence of operators IN C PROGRAMMING
PPTX
Basic operators and it's types in c languages
PPTX
Strings in python are surrounded by eith
PDF
Coper in C
PPTX
Python PCEP Operators
PPTX
Different Types of Operators in Python.pptx
PDF
PPTX
Python notes for students to develop and learn
PDF
Python : basic operators
PPTX
Python Training in Bangalore | Python Operators | Learnbay.in
PPTX
Operators and expressions
PPTX
3. C_OperatorsExpressions on c languyage.pptx
PPSX
Python Data Types, Operators and Control Flow
PDF
Python Operators.pdf
OPERATOR IN PYTHON-PART1
ICP - Lecture 5
Introduction to Python Values, Variables Data Types Chapter 2
Operators Concept in Python-N.Kavitha.pptx
Precedence of operators IN C PROGRAMMING
Basic operators and it's types in c languages
Strings in python are surrounded by eith
Coper in C
Python PCEP Operators
Different Types of Operators in Python.pptx
Python notes for students to develop and learn
Python : basic operators
Python Training in Bangalore | Python Operators | Learnbay.in
Operators and expressions
3. C_OperatorsExpressions on c languyage.pptx
Python Data Types, Operators and Control Flow
Python Operators.pdf
Ad

Recently uploaded (20)

PDF
VCE English Exam - Section C Student Revision Booklet
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Insiders guide to clinical Medicine.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
master seminar digital applications in india
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Institutional Correction lecture only . . .
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Basic Mud Logging Guide for educational purpose
VCE English Exam - Section C Student Revision Booklet
STATICS OF THE RIGID BODIES Hibbelers.pdf
Anesthesia in Laparoscopic Surgery in India
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Microbial diseases, their pathogenesis and prophylaxis
Sports Quiz easy sports quiz sports quiz
PPH.pptx obstetrics and gynecology in nursing
Insiders guide to clinical Medicine.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
master seminar digital applications in india
Renaissance Architecture: A Journey from Faith to Humanism
Institutional Correction lecture only . . .
O5-L3 Freight Transport Ops (International) V1.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Final Presentation General Medicine 03-08-2024.pptx
Basic Mud Logging Guide for educational purpose
Ad

Lecture 05.pptx

  • 2. • Operators • Arithmetic operators • Including mod and integer division • Expression • Priorities of operators • Bindings of operators • Variables • Rules for naming • Different types • How to use them • Printing output to the screen • Getting input from the user • Assignment operators • Comparison operators • Boolean operators Objectives
  • 3. Python Basic Operators An operator is a symbol of the programming language, which is able to operate on the values. Operators are the constructs which can manipulate and evaluate our data. Consider the expression: num = 4 + 5 operator
  • 4. Types of Operators in Python Arithmetic Operators Assignment Operators Comparison Operators Logical Operators Membership Operators Bitwise Operators Identity Operators focus of today’s lecture
  • 5. Arguments when both arguments are integers, the result is an integer, too; when at least one argument is a float, the result is a float, too.
  • 6. Operators – Addition & Subtraction the + (plus) sign is the operator which is able to add two numbers, giving the result of the addition. “Lowest” priority in the order of operations Function as they normally do Examples: 1. cash = cash - bills 2. (5 + 7) / 2 3. ( ((2 + 4) * 5) / (9 - 6) ) 4. -4 + 4 = 0 5. -4. + 8 = 4.0
  • 7. The subtraction operator, unary and binary operators The subtraction operator is obviously the - (minus) sign, although you should note that this operator also has another meaning ‒ it can change the sign of a number. This is a great opportunity to present a very important distinction between unary and binary operators. In subtracting applications, the minus operator expects two arguments: the left (a minuend in arithmetical terms) and right (a subtrahend). For this reason, the subtraction operator is considered to be one of the binary operators, just like the addition, multiplication and division operators. -4 – 4= - 8 4. – 8 = - 4.0 -1.1= - 1.1
  • 8. Operators – Multiplication & Division An * (asterisk) sign is a multiplication operator. A // (slash) sign is a division operator. The value in front of the slash is a dividend, the value behind the slash, a divisor. The result produced by the division operator is always a float, regardless of whether or not the result seems to be a float at first glance: 1/2, or if it looks like a pure integer: 2/1. Higher priority in the order of operations than addition and subtraction Function as they normally do Examples: 1. tax = subtotal * 0.06 2. area = PI * (radius * radius) 3. totalDays = hours / 24
  • 9. Operators – Integer Division Reminder: integers (or ints) are whole numbers What do you think integer division is? A // (double slash) sign is an integer division operator. It differs from the standard / operator in two details: •its result lacks the fractional part ‒ it's absent (for integers), or is always equal to zero (for floats); this means that the results are always rounded; rounding always goes to the lesser integer. •it conforms to the integer vs. float rule. •integer by integer division gives an integer result. All other cases produce floats. Remember division in grade school? Integer division is  Division done without decimals  And the remainder is discarded  Integer division can also be called floor division
  • 10. How not to divide As you probably know, division by zero doesn't work. Do not try to: •perform a division by zero; •perform an integer division by zero; •find a remainder of a division by zero
  • 11. Examples: Integer Division Integer division uses double slashes (//) Examples: 1. 7 / 5 = 1.4 2. 7 // 5 = 1 3. 2 / 8 = 0.25 4. 2 // 8 = 0 5. 4 // 17 // 5 = 0 6. -6 // 4 = -2 7. 6. // -4 = -2.0 evaluate from left to right
  • 12. Operators – Mod Also called “Remainder” or “modulo” or “modulus”  Its graphical representation in Python is the % (percent) sign, which may look a bit confusing. Example: 17 % 5 = 2  What do you think mod does?  The result of the operator is a remainder left after the integer division. Remember division in grade school? Modulo gives you the remainder  The “opposite” of integer division
  • 13. Examples: Mod Mod uses the percent sign (%) Examples: 1. 7 % 5 = 2 2. 5 % 9 = 5 3. 16 % 6 = 4 4. 23 % 4 = 3 5. 48692451673 % 2 = 1 6. 12 % 4.5 = 3.0
  • 14. Modulo Answers Result of a modulo operation will always be:  Positive  No less than 0  No more than the divisor minus 1 Examples: 1. 8 % 3 = 2. 21 % 3 = 3. 13 % 3 = 2 0 1 no less than zero no more than the divisor minus 1
  • 15. Operators – Exponentiation “Exponentiation” is just another word for raising one number to the power of another Examples: 1. binary8 = 2 ** 8 2. squareArea = length ** 2 3. cubeVolume = length ** 3 4. squareRoot = num ** 0.5
  • 16. Exponentiation We've surrounded the double asterisks with spaces in our examples. It's not compulsory, but it improves the readability of the code. 2 ** 3 = 8 2 ** 3. = 8.0 2. ** 3 = 8.0 2. ** 3. = 8.0
  • 18. Division: Floats and Integers Floats (decimals) and integers (whole numbers) behave in two different ways in Python  And in many other programming languages Biggest difference is how their division works  Python 3 automatically performs decimal division  For both integers and floats  Have to explicitly call integer division
  • 19. Division Examples What do the following expressions evaluate to? 1. 4 / 3 2. 4 // 3 3. 8 / 3 4. 8. / 2 5. 5 / 7 6. 5 // 7 = 1.3333333333333333 = 1 = 2.6666666666666667 = 4.0 = 0.7142857142857143 = 0
  • 20. Rounding Errors Sometimes we need to approximate the representation of numbers  0.66666666666666666666666667…  3.14159265358979323846264338328… We know that this can lead to incorrect answers when doing calculations later  Something similar happens when numbers are stored in a computer’s memory
  • 21. Float Arithmetic Examples What do the following expressions evaluate to? 1. 8 / 3 2. 5 / 7 3. 1.99 + 0.12 4. 0.99 + 0.12 5. 1.13 * 1.19 = 2.6666666666666667 = 0.7142857142857143 = 2.11 = 1.1099999999999999 = 1.3446999999999998 Because computers store numbers differently, they sometimes run into different sets of rounding errors What’s going on here???
  • 22. Handling Floating Point Errors How to fix floating point errors? You can’t! ¯_(ツ)_/¯  They’re present in every single programming language that uses the float data type Just be aware that the problem exists  Don’t rely on having exact numerical representations when using floats in Python
  • 23. Expression Data and operators when connected together form expressions. The simplest expression is a literal itself.
  • 24. Operators and their priorities We've treated each operator as if it had no connection with the others. Obviously, such an ideal and simple situation is a rarity in real programming. you will very often find more than one operator in one expression, and then things are no longer so simple. The order of their appearance is not accidental. The phenomenon that causes some operators to act before others is known as the hierarchy of priorities. Python precisely defines the priorities of all operators, and assumes that operators of a higher priority perform their operations before the operators of a lower priority.
  • 25. Arithmetic Operators in Python Operator Meaning + Addition - Subtraction * Multiplication / Division // Integer division % Modulo (remainder) ** Exponentiation
  • 26. Order of Operations (Arithmetic) Expressions are evaluated in what direction? What can change this ordering?  Parentheses! from left to right Operator(s) Priority highest lowest ** * / // % + -
  • 27. Operators and parentheses you're always allowed to use parentheses, which can change the natural order of a calculation. In accordance with the arithmetic rules, subexpressions in parentheses are always calculated first. You can use as many parentheses as you need, and they're often used to improve the readability of an expression, even if they don't change the order of the operations. An example of an expression with multiple parentheses is here: 10.0 print((5 * ((25 % 13) + 100) / (2 * 13)) // 2)
  • 28. Operators and their bindings The binding of the operator determines the order of computations performed by some operators with equal priority, put side by side in one expression. Most of Python's operators have left-sided binding, which means that the calculation of the expression is conducted from left to right. (9 % 6 % 2) =1 There are two possible ways of evaluating this expression: • from left to right: first 9 % 6 gives 3, and then 3 % 2 gives 1; • from right to left: first 6 % 2 gives 0, and then 9 % 0 causes a fatal error.
  • 29. Operators and their bindings But there's one interesting exception. Use this snippet of code: print(2 ** 2 ** 3) -> 256 The two possible results are: •2 ** 2 → 4; 4 ** 3 → 64 •2 ** 3 → 8; 2 ** 8 → 256 •The result clearly shows that the exponentiation operator uses right- sided binding. print(-3 ** 2) -> -9 print(-2 ** 3) -> -8 print(-(3 ** 2)) -> 9

Editor's Notes

  • #2: Instructions To create em dash above headline Same size and weight as the headline and set using a soft return. PC: Em dash (—): Alt+Ctrl+ - (minus) Mac: Em dash (—): Shift+Alt/Option+hyphen