Professional Documents
Culture Documents
Srilakshmi
OPERATORS
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
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