Latest
Loading...

Python for Ethical Hacking

Command Prompt

Featured Post

#5 Jarvis

Jarvis      Now we are going to create some functions. With the help of this functions we are going to make our Play music Open website...

Operators || Python for beginners

Operators || Python for beginners

 Operators

  • An operator is a symbol that represents an operation performed on one or more operands.
  • An operands is a quantity on which operations is performed
  • Eg: consider the expression Z=X+Y where Z,X,Y is operand and +,= is a operator. 
  • Python uses different operators.
They are,
  1. Arithmetic operator   -   +, -, /, *, **, //, %
  2. Assignment operator    -   =, +=, -=, /=, *=, **=, //=, %=
  3. Relational operator    -    <, >, <=, >=, !=, ==
  4. Logical operator    -    and, or, not
  5. Bitwise operator    -    &, |, ^, ~, <<, >>
  6. Membership operator     -    in, not in
  7. Identity operator     -     is, is not

Arithmetic Operators

                                An arithmetic operator performs basic arithmetic operations such as addition, subtraction and multiplication on numerical values (constant and variables).

They are

X=5 and Y=2

Operator Meaning of operator Example
+ Addition: add two operands or unary plus Z=X+Y Z=7
- Subtraction: subtracts two operands or unary minus Z=X-Y Z=3
* Multiplication: multiplies values Z=X*Y Z=10
/ Division: divides left hand operand by right hand operand Z=X/Y
Z=2.5
% Remainder: after division (modulo devision) Z=X%Y
Z=1
** Exponent: the left hand operand raised to the power of right hand operand. It performs pewer on operation Z=X**Y
Z=25
// Floor division: the division of operands where the result is the quatiend in which the digits after the decimal point are removed Z=X//Y
Z=2

Assignment Operators

  • An assignment operator is used for assigning a value to a variable.
  • The most common assignment operator is “ = ”

Operator Example Same as Explanation
= A=B A=B Assigns value from right side operands to left side operands
+= A+=B A=A+B Add value from right side operands to left side operands
-= A-=B A=A-B Subtracts value from right side operands to left side operands
*= A*=B A=A*B Multiplies value from right side operands to left side operands
/= A/=B A=A/B Divides value from right side operands to left side operands
%= A%=B A=A%B Modulus value from right side operands to left side operands
**= A**=B A=A**B Performs exponential (power) calculation on operators and assign value to the left operand
//= A//=B A=A//B Performs floor division calculation on operators and assign value to the left operand

Relational Operators

  • A relational operator check the relationship between two operands.
  • If the relation is true, it returns True; if the relation is false, it returns value False.


OperatorMeaning of operatorExample
==Equal to it checks condition if it is true returns True else returns False5==3
return False

>Checks if the value of the left is Greater than the value of the right operand. If yes then the condition becomes true.5 > 3
returns True
<Checks if the value of the left is Less than the value of the right operand. If yes then the condition becomes true.5 < 3
returns False
!=Checks if the value of the left is not equal to the value of the right operand. If yes then the condition becomes true.5 != 3
returns True
>=Checks if the value of the left is Greater than or equal to the value of the right operand. If yes then the condition becomes true.5 >= 3
returns True
<=Checks if the value of the left is Less than or equal to the value of the right operand. If yes then the condition becomes true.5 <= 3
returns False

Program:

a = 4
#assignment operator (=)
b = 3
print("Sum is",(a+b))
#arithmetic operator(+)
print("Subtraction is",(a-b))
#arithmetic operator(-)
print("Greater than",(a>b))
#relational operator(>)
print("Not equal to",(a!=b))
#relational operator(!=)

Logical Operators

  • Logical operators are used to compare and evaluate logical operations.
  • Python supports three logical operators AND, OR, NOT

Operator Meaning of operator
and
(Logical AND)
If both operands are true then condition becomes true, else false.
or
(Logical OR)
If both operands are false then condition becomes false, else true
not
(Logical NOT)
It returns false when the condition is true. It returns true when the condition is false.


Program:

a = True
b = False
print(a and b)
print(a or b)
print(not a)

Output

False
True
False

Bitwise Operators

  • Logical operators are used to perform bit-level operands
  • It compares each bits of first operand with the corresponding bits of second operand.

Operator Meaning of operator
& Bitwise AND operation between two operands.
| Bitwise OR operation between two operands.
^ Bitwise OR operation between two operands.
~ Bitwise 1's complement on a single operand.
<< Shifts the first operand left by the number of bits specified by the second operand.
>> Shifts the first operand left by the number of bits specified by the second operand.


Program:

X, Y = 60, 13
#60 - 00111100
#13 - 00001101
Z = X & Y
print("Bitwise AND", Z)
#00001100      = 12  
Z = X | Y
print("Bitwise OR", Z)
#00111101      = 61

Output

Bitwise AND 12
Bitwise OR 61

Membership Operators

                            Membership operators are used to be test whether a value (variable) is found in a sequence like string, list, tuple and dictionary.

Operator Meaning of operator Example
in Evaluate to true, if it finds a variable in the specified sequence, else false X in Y
not in Evaluate to true, if it does not finds a variable in the specified sequence, else false. X not in Y

Program:

X = "S.M. SECURITY"
print('S' in X)
print('SECURITY' not in X)

Output

True
False 

Identity Operators

  • Identity operators are used to compare the memory location of two objects.
  • It is used to check whether the two values(variables) are located on the same part of the memory.
OperatorMeaning of operatorExample
isEvaluate to true, the variables on either side of the operator point to the same object and false otherwise
X is Y
is notEvaluate to false, the variables on either side of the operator point to the same object and true otherwise
X is not Y

Program:

X = 10
Y = 10
print(X is Y)
print(X is not Y)

Output

True
False






No comments:

Post a Comment