9 - Python Operators
9 - Python Operators
Introduction to Programming
1
PYTHON – OPERATORS
Lecture Goals
1.Operator
2.Type of operators:
1. Arithmetic Operators: (+, -, *, /, %, **, //)
2. Assignment operator: (=, +=, -=, *=, /=, %=, **=, //=)
3. Unary minus operator: (-)
4. Relational operators (>, >=, <, <=, ==, !=)
5. Logical operators (and, or, not)
6. Membership operators (in, not in)
2
Dr. Quadri Noorulhasan Naveed 2
1. OPERATOR
✓An operator is a symbol that performs an operation.
✓An operator acts on some variables, those variables are
called as operands.
✓If an operator acts on a single operand, then it is called as unary
operator.
✓If an operator acts on 2 operands, then it is called as binary
operator.
✓If an operator acts on 3 operands, then it is called as ternary
operator.
3
Dr. Quadri Noorulhasan Naveed 3
operator and operands example
✓ + symbol is called as
operator.
4
Dr. Quadri Noorulhasan Naveed 4
2. Type of operators:
1.Arithmetic Operators: (+, -, *, /, %, **, //)
2.Assignment operator: (=, +=, -=, *=, /=, %=, **=, //=)
3.Unary minus operator: (-)
4.Relational operators (>, >=, <, <=, ==, !=)
5.Logical operators (and, or, not)
6.Membership operators (in, not in)
Make a note:
✓Python does not have increment and decrement operators.
5
Dr. Quadri Noorulhasan Naveed 5
1. Arithmetic Operators: (+, -, *, /, %, **, //)
Assume that,
a = 13 and b = 5
Operator Meaning Example Result
+ Addition a+b 18
- Subtraction a-b 8
* Multiplication a*b 65
/ Division a/b 2.6
% Modulus a%b 3
(Remainder of division)
** Exponent operator a ** b 371293
(exponential power value)
// Integer division a // b 2
(gives only integer quotient)
6
Dr. Quadri Noorulhasan Naveed 6
Arithmetic Operators
Make a note:
✓Division operator / always performs floating point arithmetic, so it
returns float values.
✓Floor division (//) can perform both floating point and integral as well,
oIf values are int type, then result is int type.
oIf at least one value is float type, then result is float type.
7
Dr. Quadri Noorulhasan Naveed 7
Arithmetic Operators example
8
Dr. Quadri Noorulhasan Naveed 8
2. Assignment operator: (=, +=, -=, *=, /=, %=, **=, //=)
✓By using these operators, we can assign values to variables.
Assume that, a = 13
9
Dr. Quadri Noorulhasan Naveed 9
Assignment operator example
10
Dr. Quadri Noorulhasan Naveed 10
3. Unary minus operator: (-)
✓Symbol of unary minus operator is –
11
Dr. Quadri Noorulhasan Naveed 11
4. Relational operators (>, >=, <, <=, ==, !=)
✓By using these operators, we can create simple conditions.
✓These operators are used to compare two values.
✓These operators’ returns result as Boolean type either True or False.
Assume that,
a = 13
b=5
12
Dr. Quadri Noorulhasan Naveed 12
Relational operators
13
Dr. Quadri Noorulhasan Naveed 13
Relational operators' example
14
Dr. Quadri Noorulhasan Naveed 14
5. Logical operators (and, or, not)
✓In python there are three logical operators those are,
o and
o or
o Not
✓or
✓not
o complement
16
Dr. Quadri Noorulhasan Naveed 16
Logical operators (and, or, not) example
17
Dr. Quadri Noorulhasan Naveed 17
Logical operators' example
18
Dr. Quadri Noorulhasan Naveed 18
6. Membership operators (in, not in)
✓There are two membership operators,
o in
o not in
19
Dr. Quadri Noorulhasan Naveed 19
Membership operators (in, not in)
The in operator
21
Dr. Quadri Noorulhasan Naveed 21
Membership operators (in, not in) example
22
Dr. Quadri Noorulhasan Naveed 22
1312 / 121 CCS-3
Introduction to Programming
Questions?
23