Python Operators:

An operator is a symbol that performs operations on variables and values.

Types of Operators:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Identity operators
  • Membership operators
  • Bitwise operators

Arithmetic Operators: These perform basic mathematical operations.

  • + : Addition
  • - : Subtraction
  • * : Multiplication
  • / : Division
  • % : Modulus (remainder)
  • ** : Exponentiation (power)
  • // : Floor division (division that results in a whole number rounded down)
a = 10
b = 3

print(a + b)   # Addition
print(a - b)   # Subtraction
print(a * b)   # Multiplication
print(a / b)   # Division:
print(a % b)   # Modulus: (remainder of 10/3)
print(a ** b)  # Exponentiation: (10^3)
print(a // b)  # Floor division: (quotient of 10/3, rounded down)
Output:
13
7
30
3.3333333333333335
1
1000
3

Comparison (Relational) Operators: These compare two values.

  • == : Equal to
  • != : Not equal to
  • > : Greater than
  • < : Less than
  • >= : Greater than or equal to
  • <= : Less than or equal to
x = 5
y = 8

print(x == y)  # Equal to
print(x != y)  # Not equal to
print(x > y)   # Greater than
print(x < y)   # Less than
print(x >= y)  # Greater than or equal to
print(x <= y)  # Less than or equal to
Output
False
True
False
True
False
True

Assignment Operators: These assign values to variables.

  • = : Simple assignment
  • += : Add and assign
  • -= : Subtract and assign
  • *= : Multiply and assign
  • /= : Divide and assign
  • //= : Floor divide and assign
  • **= : Exponentiate and assign
  • %= : Modulus and assign
num = 10
num += 5   # num = num + 5, now num is 15
print(num)
num -= 3   # num = num - 3, now num is 12
print(num)
num *= 2   # num = num * 2, now num is 24
print(num)
num /= 4   # num = num / 4, now num is 6.0
print(num)
num //= 2  # num = num // 2, now num is 3.0
print(num)
num **= 3  # num = num ** 3, now num is 27.0
print(num)
num %= 5   # num = num % 5, now num is 2.0
print(num)
Output:
15
12
24
6.0
3.0
27.0
2.0

Logical Operators: These are used to combine conditional statements.

  • and : Returns True if both statements are true
  • or : Returns True if one of the statements is true
  • not : Reverses the result, returns False if the result is true
a = True
b = False

print(a and b)  # Logical AND
print(a or b)   # Logical OR
print(not a)    # Logical NOT

Output:
False
True
False

Bitwise Operators: These perform operations on binary numbers.

  • & : AND
  • | : OR
  • ^ : XOR
  • ~ : NOT
  • << : Zero fill left shift
  • >> : Signed right shift
p = 5    # 0101 in binary
q = 3    # 0011 in binary

print(p & q)   # Bitwise AND: 1 (0001 in binary)
print(p | q)   # Bitwise OR: 7 (0111 in binary)
print(p ^ q)   # Bitwise XOR: 6 (0110 in binary)
print(~p)      # Bitwise NOT: -6 (inverts all bits)
print(p << 2)  # Zero fill left shift: 20 (10100 in binary)
print(p >> 2)  # Signed right shift: 1 (0001 in binary)
Output:
1
7
6
-6
20
1

Identity Operators: These check if two objects are the same (i.e., occupy the same memory location).

  • is : Returns True if both variables are the same object
  • is not : Returns True if the variables are not the same object
a = [1, 2, 3]
b = a
c = a[:]

print(a is b)       # (b references the same list as a)
print(a is not c)   # (c is a copy of a, not the same object)

Output:
True
True

Membership Operators: These test for membership in a sequence, such as lists, strings, or tuples.

  • in : Returns True if a sequence with the specified value is present
  • not in : Returns True if a sequence with the specified value is not present
numbers = [1, 2, 3, 4, 5]
value = 3

print(value in numbers)    # (3 is in the list)
print(6 not in numbers)    # (6 is not in the list)

Output:
True
True

Register

Login here

Forgot your password?

ads

ads

I am an enthusiastic advocate for the transformative power of data in the fashion realm. Armed with a strong background in data science, I am committed to revolutionizing the industry by unlocking valuable insights, optimizing processes, and fostering a data-centric culture that propels fashion businesses into a successful and forward-thinking future. - Masud Rana, Certified Data Scientist, IABAC

© Data4Fashion 2023-2024

Developed by: Behostweb.com

Please accept cookies
Accept All Cookies