Skip to main content

Python Operators

Python Operators
Operators provides a vital role in programming, and in combination with values and other identifiers form expressions and statements, which is also an important building block for Python programming.

Operators and Operands

Python operators are symbol that is used to perform mathematical or logical manipulations. Operands are the values or variables with which the operator is applied to, and values of operands can manipulate by using the operators.
Let us take a Scenario:
6 + 2=8, where there are two operands and a plus (+) operator, and the result turns 8.
Here a single operator is used to manipulate the values. The +, -, *, / and ** does addition, subtraction, multiplication, division & exponentiation respectively.

Types of Python Operators

Python programming language is rich with built-in operators.
The following types of operators are supported by Python:
  • Arithmetic Operators
  • Assignment Operators
  • Comparison (Relational) Operators
  • Logical Operators
  • Identity Operators
  • Bitwise Operators
  • Membership Operators

Arithmetic Operators

Python Arithmetic Operators
SymbolOperator NameDescription
+AdditionAdds the values on either side of operator and calculate a result.
SubtractionSubtracts values of right side operand from left side operand.
*MultiplicationMultiplies the values on both sides of the operator.
/DivisionDivides left side operand with right side operand.
%ModulusIt returns the remainder by dividing the left side operand with right side operand
**ExponentCalculates the exponential power
//Floor DivisionHere the result is the quotient in which the digits after decimal points are not taken into account.

Assignment Operators

Python Assignment Operators
SymbolOperator NameDescription
=EqualAssigns the values of right side operand to left side operand.
+=Add ANDAdds right side operand value to the left side operand value and assigns the results to left operand.
-=Subtract ANDSubtracts right side operand value to the left side operand value and assigns the results to left operand.
*=Multiply ANDSimilarly does their respective operations and assigns the operator value to the left operand.
/=Division AND
%=Modulus AND
**=Exponent AND
//=Floor Division AND

Comparison (Relational) Operators

Python Comparison Operators
SymbolOperator NameDescription
==Double EqualIf the two value of its operands are equal, then the condition becomes true, otherwise false
!= or <>Not Equal ToIf two operands values are not equal, then condition becomes true. Both the operators defines the same meaning and function
>Greater ThanIf the value of left hand operand is greater than the value of right hand operand, condition becomes true.
<Less ThanIf the value of left hand operand is less than the value of right operand, then condition becomes true.
<=Less Than Equal ToIf the value of left hand operand is less than or equal to the value of right hand operand, condition becomes true.
>=Greater Than Equal ToIf the value of left hand operand is greater than or equal to the value of right hand operand, condition becomes true.

Logical Operators

Python Logical Operators
SymbolOperator NameDescription
orLogical ORIf any of the two operands are non-zero, then condition is true.
andLogical ANDIf both the operands are true then condition is true.
notLogical NOTIt is used to reverse the logical state of its operand.

Identity Operators

For comparing memory locations of two objects, identity operators are used.
There are two types of identity operators. These are:
Python Identity Operators
SymbolOperator NameDescription
isisThe result becomes true, if values on either side of the operator points to the same object & False otherwise.
is notis notThe result becomes False if the variables on either side of the operator points to the same object

Bitwise Operators

These operators are used to manipulate with bits, & performs bit-by-bit operations.
There are six types of bitwise operators supported by Python. These are:
Python Bitwise Operators
SymbolOperator NameDescription
&Binary ANDThis operator copies the bit to the result if it exists in both operands.
|Binary ORThis operator copies the bit if it exists in either of the operand.
^Binary XORThis operator copies the bit if it is set in one operand but not both.
~Binary 1s ComplementThis is a unary operator and has the ability of ‘flipping’ bits.
<<Binary Left ShiftThe left operands value is moved left by the number of bits specified by the right operand using this operator.
>>Binary Right ShiftThe left operands value is moved right by the number of bits specified by the right operand using this operator.

Membership Operators

Python Membership Operators
SymbolOperator NameDescription
ininThe result of this operation becomes True if it finds a value in a specified sequence & False otherwise.
not innot inresult of this operation becomes True if it doesn’t finds a value in a specified sequence & False otherwise.

Comments

For Programs Click Here

Popular posts from this blog

Syllabus

Syllabus  C Programming Tutorials C Tutorial C Introduction History of C Programming Language C Installation C Program Structure C Input and Output (I/O) C Format Specifiers Declaration & Assignments C Tokens C Identifiers C Keywords C Constants C Operators C Data Types C Variables C Preprocessors C Type Casting C Custom Header File Flow Control C Decision Making C if Statements C if-else Statements C Nested if-else Statements C else-if Statements C goto Statement C switch Statements C Loops C while loops C do while loops C for loops Functions C Functions C Function Arguments C Library Functions C Variable Scope Arrays & Strings C Arrays C Strings Pointers C Pointers C Dynamic Memory Allocation Structure & Union C Structures C Unions File I/O C File Handling C fopen C fclose C getc C putc C getw C putw C fprintf C fscanf C fgets C fputs C feof                                     ...

Syllabus

Python Tutorials Python Tutorial Python Overview Python Installation Basics of Python Programming Python Operators Python Keywords Python Numbers Python Strings Python Data Types Python Variables Python Lists Python Tuples Python Date and Time Python Decision Making Python Loops Python File Handling Python Dictionaries Python Functions Python Modules Python Exceptions Handling Python Object Oriented Inheritance in Python Python Regular Expressions Python Networking Programming Python Multithreaded Programming Python CGI Programming Python Database Connection Python Metaprogramming Python Data Processing And Encoding Python GUI Programming

C++ Data Abstraction

C++ Data Abstraction Object oriented programming offers various features to write programs with various concepts that help to minimize problems and increase flexibility in program. One of the features of object oriented programming is Data abstraction. In this chapter you will learn about how the concept data abstraction is carried out within the C++ program. What is Data abstraction? Data abstraction allows a program to ignore the details of how a data type is represented. Abstraction (derived from a Latin word  abs , meaning away from and  trahere , meaning to draw) refers to the act of representing essential features without including the background details or explanations. C++ classes use the technique of abstraction and are defined as a list of abstract attributes such as width, cost, size etc and functions to operate on these attributes. They put in a nutshell all the essential properties of an object that are required to be created. The attributes are therefore called...