Basic Python For ML December 12 ,2024

Operators are special symbols or keywords that perform operations on variables or values. Python has several types of operators categorized by their function.

 

Types of Operators in Python:

Operator Type

Purpose

Examples

Arithmetic OperatorsPerform basic mathematical operations+, -, *, /
Comparison OperatorsCompare two values and return True or False==, !=, >, <
Logical OperatorsCombine multiple conditionsand, or, not
Assignment OperatorsAssign values to variables=, +=, -=, *=
Bitwise OperatorsPerform bit-level operations&, `
Membership OperatorsCheck if a value exists in a sequencein, not in
Identity OperatorsCheck if two objects are the same in memoryis, is not

 

 

Arithmetic Operators

These are used for performing mathematical calculations.

Operator

Description

Example

+Addition3 + 5 = 8
-Subtraction10 - 4 = 6
*Multiplication6 * 3 = 18
/Division (float result)15 / 2 = 7.5
//Floor Division15 // 2 = 7
%Modulus (remainder)10 % 3 = 1
**Exponentiation2 ** 3 = 8

Example:

a = 10
b = 3
print(a + b)  # Output: 13
print(a // b) # Output: 3 (floor division)
print(a ** b) # Output: 1000 (10 raised to the power of 3)


 

 

 

Comparison Operators

These compare two values and return a boolean (True or False).

Operator

Description

Example

==Equal to5 == 5 → True
!=Not equal to5 != 4 → True
>Greater than7 > 3 → True
<Less than3 < 5 → True
>=Greater than or equal8 >= 8 → True
<=Less than or equal2 <= 3 → True

Example:

x = 10
y = 5
print(x > y)  # Output: True
print(x == y) # Output: False

 

 

 

Logical Operators

These are used to combine multiple conditions.

Operator

Description

Example

andTrue if both conditions are true(5 > 3) and (4 > 2) → True
orTrue if at least one is true(5 > 3) or (2 > 4) → True
notReverses the conditionnot (5 > 3) → False

Example:

a = True
b = False
print(a and b)  # Output: False
print(a or b)   # Output: True
print(not a)    # Output: False

 

 

Assignment Operators

These are used to assign values to variables.

Operator

Description

Example

=Assign valuex = 5
+=Add and assignx += 3 (same as x = x + 3)
-=Subtract and assignx -= 2
*=Multiply and assignx *= 4

Example:

x = 10
x += 5  # Same as x = x + 5
print(x)  # Output: 15


 

Bitwise Operators

Bitwise operators work on binary (bit-level) representations of integers. They allow you to manipulate data at the level of 0s and 1s.

OperatorDescriptionExample
&AND: Sets each bit to 1 if both bits are 1.5 & 3 → 1
^XOR: Sets each bit to 1 if the bits are different.5 ^ 3 → 6
~NOT: Inverts all the bits. (Unary operator)~5 → -6
<<Left Shift: Shifts bits left by a specified number of positions (adds 0s).5 << 2 → 20
>>Right Shift: Shifts bits right by a specified number of positions.5 >> 2 → 1

Examples:

# Binary: 5 → 0101, 3 → 0011

result = 5 & 3

print(result)  # Output: 1 (Binary: 0001)

 

 

 

Membership Operators

Check if a value exists in a sequence (like a list, string, or tuple).

Operator

Description

Example

inValue is in the sequence'a' in 'apple' → True
not inValue is not in the sequence'z' not in 'apple' → True

Example:

text = "Python"
print('P' in text)       # Output: True
print('z' not in text)   # Output: True

 

 

Identity Operators

Identity operators check whether two objects are the same object in memory, not just whether their values are the same.

OperatorDescriptionExample
isReturns True if two variables reference the same object in memory.a is b
is notReturns True if two variables reference different objects in memory.a is not b

Examples:

a = [1, 2, 3]
b = a  # Both point to the same object
print(a is b)  # Output: True

 

 

By mastering these foundational concepts, you’re setting yourself up for success in Python programming and beyond. Keep practicing with real-world examples to solidify your understanding!

 

Next Blog : Control Flow in Python
 

 

Purnima
0

You must logged in to post comments.

Get In Touch

123 Street, New York, USA

+012 345 67890

techiefreak87@gmail.com

© Design & Developed by HW Infotech