Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views6 pages

Only - Operators

The document provides an overview of various types of operators in programming, including arithmetic, comparison, logical, bitwise, assignment, membership, and identity operators, along with examples for each. It also discusses the use of these operators in functions and conditional statements, demonstrating how they can be combined for more complex operations. Additionally, it highlights the importance of order of operations and the use of parentheses in expressions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Only - Operators

The document provides an overview of various types of operators in programming, including arithmetic, comparison, logical, bitwise, assignment, membership, and identity operators, along with examples for each. It also discusses the use of these operators in functions and conditional statements, demonstrating how they can be combined for more complex operations. Additionally, it highlights the importance of order of operations and the use of parentheses in expressions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

Arithmetic Operators

Addition:
result = 5 + 3

Subtraction:
result = 10 - 4

Multiplication:
result = 7 * 6

Division:
result = 20 / 4

Floor Division:
result = 20 // 3

Modulus:
result = 20 % 3

Exponentiation:
result = 2 ** 3
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Comparison Operators
Equal to:
is_equal = (5 == 5)

Not equal to:


is_not_equal = (5 != 3)

Greater than:
is_greater = (5 > 3)

Less than:
is_less = (5 < 10)

Greater than or equal to:


is_greater_equal = (5 >= 5)

Less than or equal to:


is_less_equal = (5 <= 10)

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Logical Operators
Logical AND:
result = (5 > 3) and (3 < 10)
Logical OR:
result = (5 > 3) or (3 > 10)
Logical NOT:
result = not (5 > 3)

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Bitwise Operators
Bitwise AND:
result = 5 & 3 # 0101 & 0011 = 0001

Bitwise OR:
result = 5 | 3 # 0101 | 0011 = 0111

Bitwise XOR:
result = 5 ^ 3 # 0101 ^ 0011 = 0110
Bitwise NOT:
result = ~5 # Inverts the bits
Left Shift:
result = 5 << 1 # 0101 becomes 1010 (10 in decimal)
Right Shift:
result = 5 >> 1 # 0101 becomes 0010 (2 in decimal)

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Assignment Operators
Simple assignment:
x = 5
Add and assign:
x += 3 # x = x + 3
Subtract and assign:
x -= 2 # x = x - 2
Multiply and assign:
x *= 2 # x = x * 2
Divide and assign:
x /= 2 # x = x / 2
Modulus and assign:
x %= 3 # x = x % 3
Exponentiate and assign:
x **= 2 # x = x ** 2

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Membership Operators
In operator:
result = 5 in [1, 2, 3, 4, 5]
Not in operator:
result = 6 not in [1, 2, 3, 4, 5]

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Identity Operators
Is operator:
a = [1, 2, 3]
b = a
result = (a is b)
Is not operator:
a = [1, 2, 3]
b = [1, 2, 3]
result = (a is not b)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Check if two variables point to the same object:

a = [1, 2, 3]
b = a
result = (a is b) # True
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Check if two different lists are not the same object:
a = [1, 2, 3]
b = [1, 2, 3]
result = (a is not b) # True
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Additional Arithmetic Operations


Using parentheses for order of operations:
result = (2 + 3) * 4 # 20

Combining multiple arithmetic operations:


result = 10 - 2 * 3 + 5 / 5 # 4.0

More Logical Operations


Combining logical operators:
result = (5 > 3) and (3 < 10) or (2 > 1) # True

Using logical operators with variables:


a = True
b = False
result = a and not b # True
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

More Bitwise Operations


Bitwise AND with binary literals:
result = 0b1010 & 0b1100 # 0b1000 (8 in decimal)

Bitwise OR with binary literals:


result = 0b1010 | 0b1100 # 0b1110 (14 in decimal)

More Assignment Operations


Divide and assign with float:
x = 10
x /= 3 # x becomes approximately 3.3333

Using multiple assignment:


a, b, c = 1, 2, 3

More Membership Operations


Check membership in a string:
result = 'a' in 'banana' # True

Check membership in a tuple:


result = 3 not in (1, 2, 3, 4) # False

More Identity Operations


Check if two variables are the same object:

x = [1, 2, 3]
y = x
result = (x is y) # True
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Check if two different objects are not the same:

x = [1, 2, 3]
y = [1, 2, 3]
result = (x is not y) # True
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Using Operators in Functions


Using arithmetic operators in a function:

def add(a, b):


return a + b

result = add(5, 3) # 8
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Using comparison operators in a function:

def is_greater(a, b):


return a > b

result = is_greater(5, 3) # True


:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Combining Operators
Combining multiple types of operators:

a = 10
b = 5
c = 2
result = (a + b) * c > 20 and (a - b) < 10 # True
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Using parentheses to control order of operations:

result = (3 + 2) * (5 - 1) # 20
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Calculating the average of a list:

numbers = [10, 20, 30, 40]


average = sum(numbers) / len(numbers) # 25.0
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Comparison Operators
Chaining comparison operators:

result = 5 < 10 < 15 # True


:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Using comparison operators with strings:

result = "apple" < "banana" # True (lexicographical order)


:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Logical Operators
Using logical operators with boolean variables:
a = True
b = False
result = a or b # True
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Combining logical operators with comparison:

result = (5 > 3) and (10 < 8) # False


:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Bitwise Operators
Using bitwise NOT:

result = ~5 # -6 (inverts the bits)


:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Using bitwise operations to swap two numbers:

a = 5
b = 3
a = a ^ b
b = a ^ b
a = a ^ b # Now a is 3 and b is 5
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Assignment Operators
Using multiple assignment in a single line:

a, b, c = 1, 2, 3
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Using augmented assignment with lists:

my_list = [1, 2, 3]
my_list += [4, 5] # my_list is now [1, 2, 3, 4, 5]
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Membership Operators
Check if a substring exists in a string:

result = "cat" in "concatenation" # True


:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Check if an element exists in a set:

my_set = {1, 2, 3}
result = 2 in my_set # True
Identity Operators
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Check if two lists are the same object:

list1 = [1, 2, 3]
list2 = list1
result = (list1 is list2) # True
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Check if two different lists are not the same object:


list1 = [1, 2, 3]
list2 = [1, 2, 3]
result = (list1 is not list2) # True
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Using Operators in Conditional Statements


Using comparison operators in an if statement:

x = 10
if x > 5:
result = "x is greater than 5"
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Using logical operators in an if statement:

x = 10
y = 5
if x > 5 and y < 10:
result = "Both conditions are true"
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Combining multiple types of operators:

a = 10
b = 5
c = 2
result = (a + b) * c > 20 and (a - b) < 10 # True
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Using arithmetic and comparison operators together:

x = 15
result = (x % 3 == 0) and (x // 3 > 4) # True
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Using Operators in Functions


Using arithmetic operators in a function:

def multiply(a, b):


return a * b

result = multiply(4, 5) # 20
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Using comparison operators in a function:

def is_equal(a, b):


return a == b

result = is_equal(5, 5) # True

You might also like