-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperators.py
More file actions
59 lines (50 loc) · 1.72 KB
/
operators.py
File metadata and controls
59 lines (50 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
a=10
b=3
# Arithmetic operators
print(a+b)
print(a-b)
print(a*b)
print(a/b)
print(a%b) # Modulus operator
print(a//b) # Floor division operator instead of giving the exact result, it rounds down to the nearest whole number floor division cuts off the decimal part and gives you the largest whole number less than or equal to the result.
print(a**b) # Exponentiation operator (a raised to the power of b) a is base , b is exponent 10^3 = 10*10*10 = 1000.
# Comparison operators
print(a>b)
print(a>=b)
print(a<b)
print(a<=b)
print(a==b)
print(a!=b)
# Augmented assignment operator
c = 6
c += 2 # c = c + 2
c -= 4 # c = c - 4
c *= 6 # c = c * 6
c/=3 # c = c / 3
print(c)
# Logical operators
d=20
e=15
print(d>e and d<e) # and operator returns True if both conditions are True
print(d>e or d==e) # or operator returns True if at least one condition is True
print(not (d>e)) # not operator negates the condition, if the condition is True, it returns False
# Identity operators
f=25
g=30
print(f is not g) # is operator checks if both variables point to the same object in memory
print(f != g)
# Membership operators (use in sequence, set, list, string etc)
fruits=['apple', 'banana', 'cherry']
print('cherry' in fruits) # in operator checks if the value is present in the sequence
print('orange' not in fruits) # not in operator checks if the value is not present in the sequence
num={21, 32, 43, 57, 64}
print(24 in num)
# Bitwise operators
a=78
b=62
print(a^b, bin(a), bin(b))
print(a&b, bin(a), bin(b))
print(a|b, bin(a), bin(b))
print(a<<2, bin(a), bin(b)) # Left shift operator (shifts bits to the left)
print(a>>2, bin(a), bin(b)) # Right shift operator (shifts bits to the right)
print(~a, bin(a)) # Bitwise NOT operator (inverts the bits)