Computer Science Related Others Courses AvailableThe Best Codder.blogspot.com
Posts

Python Operators

 

Python Operators


Python Operators in general are used to perform operations on values and variables. 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. 

  • OPERATORS: Are the special symbols. Eg- + , * , /, etc.
  • OPERAND: It is the value on which the operator is applied.

Arithmetic Operators

Arithmetic operators are used to performing mathematical operations like addition, subtraction, multiplication, and division.

  • In Python 3.x the result of division is a floating-point while in Python 2.x division of 2 integer was an integer and to obtain an integer result in Python 3.x floored (// integer) is used.
OperatorDescriptionSyntax
+Addition: adds two operandsx + y
Subtraction: subtracts two operandsx – y
*Multiplication: multiplies two operandsx * y
/Division (float): divides the first operand by the secondx / y
//Division (floor): divides the first operand by the secondx // y
%Modulus: returns the remainder when the first operand is divided by the secondx % y
**Power: Returns first raised to power secondx ** y

PRECEDENCE:

  • P – Parentheses
  • E – Exponentiation
  • M – Multiplication     (Multiplication and division have the same precedence)
  • D – Division
  • A – Addition     (Addition and subtraction have the same precedence)
  • S – Subtraction

The modulus operator helps us extract the last digit/s of a number. For example:

  • x % 10 -> yields the last digit
  • x % 100 -> yield last two digits

Example: Arithmetic operators in Python

# Examples of Arithmetic Operator
a = 9
b = 4
  
# Addition of numbers
add = a + b
  
# Subtraction of numbers
sub = a - b
  
# Multiplication of number
mul = a * b
  
# Division(float) of number
div1 = a / b
  
# Division(floor) of number
div2 = a // b
  
# Modulo of both number
mod = a % b
  
# Power
p = a ** b
  
# print results
print(add)
print(sub)
print(mul)
print(div1)
print(div2)
print(mod)
print(p)
Output
13
5
36
2.25
2
1
6561

Post a Comment

© Python . The Best Codder All rights reserved. Distributed by