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

9 - Python Operators

Uploaded by

imujahid.2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

9 - Python Operators

Uploaded by

imujahid.2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

1312 / 121 CCS-3

Introduction to Programming

Lecture 3, PYTHON – OPERATORS

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

✓Here a, b and c are


called as operands.

✓ + 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

✓Logical operators are useful to create compound conditions.


✓Compound condition is a combination of more than one simple
condition.
✓Each simple condition brings the Boolean result finally the total
compound condition evaluates either True or False.
15
Dr. Quadri Noorulhasan Naveed 15
For Boolean types behavior
✓and

o If both arguments are True, then only result is True

✓or

o If at least one argument is True, then result is True

✓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

✓Membership operators are useful to check whether the given value is


available in sequence or not.
(It may be string, list, set, tuple and dict)

19
Dr. Quadri Noorulhasan Naveed 19
Membership operators (in, not in)
The in operator

✓in operator returns True, if element is found in the collection or


sequences.
✓in operator returns False, if element is not found in the collection or
sequences.

The not in operator

✓not in operator returns True, if an element is not found in the


sequence.
✓not in operator returns False, if an element is found in the sequence.
20
Dr. Quadri Noorulhasan Naveed 20
Membership operators (in, not in) example

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

You might also like