UNIVERSITY OF CAGAYAN VALLEY
Tuguegarao City
College of Information Technology
First Semester, S.Y. 2020-2021
Course Code :
Course Title : INTEGRATIVE PROGRAMMING 2
PRELIM PERIODIC COVERAGE
MODULE No. 03
TITLE: PYTHON OPERATION
INTRODUCTION
LEARNING 1.
OUTCOMES
LEARNING 1.
OBJECTIVES
Discussion/Situational analysis/Content Etc.:
Chapter 3 – Python Operators
Operators in general are used to perform operations on values and variables in Python. These are
standard symbols used for the purpose of logical and arithmetic operations. In this article, we will
look into different types of Python operators.
1. Arithmetic operators: Arithmetic operators are used to perform mathematical operations like
addition, subtraction, multiplication and division.
Operator Description Syntax
+ Addition: adds two operands x+y
- Subtraction: subtracts two operands x-y
* Multiplication: multiplies two operands x*y
/ Division (float): divides the first operand by the second x/y
// Division (floor): divides the first operand by the second x // y
Modulus: returns the remainder when first operand is
% divided by the second x%y
x ** y
2**3
** Power : Returns first raised to power second (23)
2. Relational Operators: Relational operators compares the values. It either
returns True or False according to the condition.
Operator Description Syntax
Page 1
UNIVERSITY OF CAGAYAN VALLEY
Tuguegarao City
College of Information Technology
First Semester, S.Y. 2020-2021
Greater than: True if left operand is greater than the x>y
> right 9>5 =true
x<y
< Less than: True if left operand is less than the right 3<4 = true
x == y
10==10 =
== Equal to: True if both operands are equal true
x != y
!= Not equal to - True if operands are not equal 10!=9 = true
Greater than or equal to: True if left operand is greater
>= than or equal to the right x >= y
Less than or equal to: True if left operand is less than or
<= equal to the right x <= y
3. Logical operators: Logical operators perform Logical AND, Logical OR and Logical
NOT operations.
Operator Description Syntax
and Logical AND: True if both the operands are true x and y
or Logical OR: True if either of the operands is true x or y
not Logical NOT: True if operand is false not x
4. Assignment operators: Assignment operators are used to assign values to the variables.
Page 2
UNIVERSITY OF CAGAYAN VALLEY
Operator Tuguegarao City
Description Syntax
Assign value ofof
College right side of expression
Information to
Technology
= left sideFirst
operandSemester, S.Y. 2020-2021x = y + z
Add AND: Add right side operand with left
5.Special
+= side operand and then assign to left operand a+=b a=a+b
Subtract AND: Subtract right operand from
-= left operand and then assign to left operand a-=b a=a-b
Multiply AND: Multiply right operand with
*= left operand and then assign to left operand a*=b a=a*b
Divide AND: Divide left operand with right
/= operand and then assign to left operand a/=b a=a/b
Modulus AND: Takes modulus using left
and right operands and assign result to left
%= operand a%=b a=a%b
Divide(floor) AND: Divide left operand
with right operand and then assign the
//= value(floor) to left operand a//=b a=a//b
Exponent AND: Calculate exponent(raise
power) value using operands and assign
**= value to left operand a**=b a=a**b
Performs Bitwise AND on operands and
&= assign value to left operand a&=b a=a&b
Performs Bitwise OR on operands and
|= assign value to left operand a|=b a=a|b
Performs Bitwise xOR on operands and
^= assign value to left operand a^=b a=a^b
Performs Bitwise right shift on operands and
>>= assign value to left operand a>>=b a=a>>b
Performs Bitwise left shift on operands and a <<= b a=
<<= assign value to left operand a << b
operators: There are some special type of operators like-
Identity operators-
is and is not are the identity operators both are used to check if two values are located on the
same part of the memory. Two variables that are equal does not imply that they are identical.
is True if the operands are identical
is not True if the operands are not identical
Page 3
UNIVERSITY OF CAGAYAN VALLEY
Tuguegarao City
College of Information Technology
First Semester, S.Y. 2020-2021
Membership operators-
in and not in are the membership operators; used to test whether a value or variable is in a
sequence.
in True if value is found in the sequence
not in True if value is not found in the sequence
TERNARY OPERATORS
Ternary operators also known as conditional expressions are operators that evaluate something
based on a condition being true or false.
It simply allows to test a condition in a single line replacing the multiline if-else making the code compact.
Syntax :
[on_true] if [expression] else [on_false]
1. Simple Method to use ternary operator (if-else)
2. Direct Method by using tuples, Dictionary and lambda
3. Ternary operator can be written as nested if-else
DIVISION OPERATORS
Consider the below statements in Python.
print (5//2)
print (-5//2)
Output:
2
-3
In Python, the “//” operator works as a floor division for integer and float arguments. However, the
operator / returns a float value if one of the arguments is a float.
print (5.0/2)
print (-5.0/2)
2.5
-2.5
The real floor division operator is “//”. It returns floor value for both integer and floating point
arguments.
print (5//2) #integer
print (-5//2) #integer (realnumber)
print (5.0//2) #float (decimal point)
print (-5.0//2)#float
2
-3
2.0
Page 4
UNIVERSITY OF CAGAYAN VALLEY
Tuguegarao City
College of Information Technology
First Semester, S.Y. 2020-2021
-3.0
Page 5