You are on page 1of 27

Ch.

Srilakshmi
OPERATORS

• An operator is a symbol that represents an


operations that may be performed on one or
more operands.
• An operand is a value that a given operator is
applied to.
• Example: 4+(3*k)
+, * are operator and 4,3,k are operands
Different forms of operator
•  Unary Operator:
– Unary arithmetic operators perform mathematical operations on
one operand only. The „+‟ and „-„‟ are two unary operators.
– Example:
>>> x = -5 #Negates the value of X
>>> x
-5
•  Binary operator:
– A Binary operator operates on two operands
– Example:
>>> 3 + 10
>>>13
>>> 10 – 7
>>> 3
Types of Operators
1. Arithmetic operator
2. Relational operator
3. Logical operator
4. Bitwise operator
5. Assignment operator
6. Special operator
1. Arithmetic operator
• Arithmetic operators are basic mathematical
operations.
Operator Meaning Example Result
+ Addition C=12+1 C=13
- Subtraction C=12-1 C=11
* Multiplication C=12*1 C=12
/ Division C=12/1 C=12
// Floor division C=12//10 1
% Modulus C=12%10 C=2
** Exponentiation C=10**2 C=100
Example of Arithmetic Operator
print("Arithmetic Operator")
a=10
b=5
print("Addition:",a+b) Output:
print("Subtraction:",a-b)
print("Multiplication:",a*b)
print("Division:",a/b)
print("Floor Division:",a//b)
print("Modulus:",a%b)
print("Exponent",a**b)
2. Relational operator
• Relational operators are also called as Comparison
operators
• It is used to compare values.
• It either returns True or False according to condition.
Operator Meaning Example Result
> Greater than 5>6 False
< Less than 5<6 True
== Equal to 5==6 False
!= Not equal to 5!=6 True
>= Greater than or equal to 5>=6 False
<= Less than or equal to 5<=6 True
Example of Relational Operator
print("Relational Operator")

a=10
b=5 Output:
print(a>b)
print(a<b)
print(a==b)
print(a!=b)
print(a>=b)
print(a<=b)
3. Logical operator
• Logical operator are typically used with
Boolean(logical) values.
• They allow a program to make a decision
based on multiple condition.
Operator Meaning Example Result
and True if both the 10<5 and 10<20 False
operands are true
or True if either of the 10<5 or 10<20 True
operands is true
not True if operands is not (10<20) False
false ( complements
the operand)
Example of Logical Operator
print("Logical Operator")
print(10<5 and 10<20)
print(10<5 or 10<20)
print(not(10<20))
Output:

Logical Operator

False
True
False
4. Bitwise operator
• Bitwise operators act on operands as if they are
string of binary digits.
• It operates bit by bit.
Operator Meaning Example
& Bitwise AND a&b
| Bitwise OR a|b
~ Bitwise NOT a~b
^ Bitwise XOR a^b
>> Bitwise right shift a >> 2
<< Bitwise left shift a << 2
5. Assignment operator
• Assignment operators are used to assign values
to variables.
Operator Meaning Example
= Assign a value a=5
+= Adds and assign the result to the variable a+=1 (a=a+1)
-= Subtracts and assign the result to the variable a-=1 (a=a-1)
*= Multiplies and assign the result to the variable a*=5 (a=a*5)
/= Division and assign the result to the variable a/= (a=a/5)
//= Floor division and assign the result to the variable a//=5(a=a//5)
%= Find modulus and assign the result to the variable a%=5 (a=a%5)
**= Find Exponentiation and assign the result to the a**=5 (a=a**5)
variable
Operator Meaning Example
&= Find Bitwise AND and assign the result to the variable a&=5(a=a&5)
|= Find Bitwise OR and assign the result to the variable a|=5(a=a|5)
^= Find Bitwise XOR and assign the result to the variable a^=5(a=a^5)
>>= Find Bitwise right shift and assign the result to the a>>=5 (a=a>>5)
variable
<<= Find Bitwise left shift and assign the result to the a<<=5 (a=a<<5)
variable
6. Special operator
• Python offers some special operators like
identity operator and the membership operator.
• Identity Operator:
–is and is not are the identity operator

Operator Meaning Example


is True if the operands a is true
are identical
is not True if the operands a is not true
are not identical
Example of Identity Operator
a1=5
b1=5
a2="Hello"
b2="Hello"
a3=[1,2,3]
b3=[1,2,3] Output:

print(a1 is not b1) False


print(a2 is b2) True
print(a2 is b3) False
• Membership Operators:
–in and not in are the membership operators.

Operator Meaning Example


in True if value/ 5 in a
variable is found in
the sequence
not in True if value/ 5 not in a
variable is not
found in the
sequence
Example of Membership Operator
a="Hello world"
b={1,"a","b",2}

print("H" in a)
print("hello" in a ) Output:

print(1 in b) True
False
print("b" in b) True
True
Print(“c” not in b) True
Features of OOP
Ability to simulate real-world event much more effectively
Code is reusable thus less code may have to be written
Data becomes active
Better able to create GUI (graphical user interface) applications
Programmers are able to produce faster, more accurate and better-
written applications
Fundamental concepts of OOP in Python
The four major principles of object orientation are:
Encapsulation
Data Abstraction
Inheritance
Polymorphism
What is an Object..?
Objects are the basic run-time entities in an object-oriented
system.
They may represent a person, a place, a bank account, a table of
data or any item that the program must handle.
When a program is executed the objects interact by sending
messages to one another.
Objects have two components:
- Data (i.e., attributes)
- Behaviors (i.e., methods)
Object Attributes and Methods Example
Object Attributes Object Methods
Store the data for that object Define the behaviors for the
Example (taxi): object
Driver Example (taxi):
OnDuty - PickUp
NumPassengers - DropOff
Location - GoOnDuty
- GoOffDuty
- GetDriver
- SetDriver
- GetNumPassengers
What is a Class..?
A class is a special data type which defines how to build a certain
kind of object.
The class also stores some data items that are shared by all the
instances of this class
Instances are objects that are created which follow the definition
given inside of the class
Python doesn’t use separate class interface definitions as in some
languages
You just define the class and then use it
Methods in Classes
Define a method in a class by including function definitions
within the scope of the class block
There must be a special first argument self in all of method
definitions which gets bound to the calling instance
There is usually a special method called __init__ in most
classes
Self
The first argument of every method is a reference to the current
instance of the class
By convention, we name this argument self
In __init__, self refers to the object currently being created; so, in
other class methods, it refers to the instance whose method was
called
Similar to the keyword this in Java or C++
But Python uses self more often than Java uses this
You do not give a value for this parameter(self) when you call the
method, Python will provide it.
Continue…
…Continue

Although you must specify self explicitly when defining the


method, you don’t include it when calling the method.
Python passes it for you automatically

Defining a method: Calling a method:


(this code inside a class definition.)
def get_age(self, num): >>> x.get_age(23)
self.age = num
Deleting instances: No Need to “free”
When you are done with an object, you don’t have to delete
or free it explicitly.
Python has automatic garbage collection.
Python will automatically detect when all of the references to
a piece of memory have gone out of scope. Automatically
frees that memory.
Generally works well, few memory leaks
There’s also no “destructor” method for classes
Syntax for accessing attributes and methods
>>> f = student(“Python”, 14)

>>> f.full_name # Access attribute


“Python”

>>> f.get_age() # Access a method


14

You might also like