-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperators.py
157 lines (112 loc) · 2.67 KB
/
operators.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# Operators in Python
# Python divides the operators in the following groups:
#
# Arithmetic operators
# Assignment operators
# Comparison operators
# Logical operators
# Identity operators
# Membership operators
# Bitwise operators
# Arithmetic operators
# Arithmetic operators are used with numeric values to perform common mathematical operations:
# Addition
print(10 + 5) # 15
# Subtraction
print(10 - 5) # 5
# Multiplication
print(10 * 5) # 50
# Division
print(10 / 5) # 2.0
# Modulus
print(10 % 5) # 0
# Exponentiation
print(10 ** 5) # 100000
# Floor division
print(10 // 5) # 2
# Assignment operators
# Assignment operators are used to assign values to variables:
# =
x = 5
print(x) # 5
# +=
x += 5 # x = x + 5
print(x) # 10
# -=
x -= 5 # x = x - 5
print(x) # 5
# *=
x *= 5 # x = x * 5
print(x) # 25
# /=
x /= 5 # x = x / 5
print(x) # 5.0
# %=
x %= 5 # x = x % 5
print(x) # 0.0
# **=
x **= 5 # x = x ** 5
print(x) # 0.0
# //=
x //= 5 # x = x // 5
# Comparison operators
# Comparison operators are used to compare two values:
# ==
print(10 == 5) # False
# !=
print(10 != 5) # True
# >
print(10 > 5) # True
# <
print(10 < 5) # False
# >=
print(10 >= 5) # True
# <=
print(10 <= 5) # False
# Logical operators
# Logical operators are used to combine conditional statements:
# and
print(10 > 5 and 10 < 15) # True
# or
print(10 > 5 or 10 > 15) # True
# not
print(not(10 > 5 and 10 < 15)) # False
# Identity operators
# Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location:
# is
x = ["apple", "banana"]
y = ["apple", "banana"]
z = x
print(x is z) # True
# is not
print(x is not z) # False
# Membership operators
# Membership operators are used to test if a sequence is presented in an object:
# in
x = ["apple", "banana"]
print("banana" in x) # True
# not in
print("pineapple" not in x) # True
# Bitwise operators
# Bitwise operators are used to compare (binary) numbers:
# & (AND) Sets each bit to 1 if both bits are 1
print(10 & 5) # 0
# | (OR) Sets each bit to 1 if one of two bits is 1
print(10 | 5) # 15
# ^ (XOR) Sets each bit to 1 if only one of two bits is 1
print(10 ^ 5) # 15
# ~ (NOT) Inverts all the bits
print(~10) # -11
# << (Zero fill left shift) Shift left by pushing zeros in from the right and let the leftmost bits fall off
print(10 << 5) # 320
# >> (Signed right shift) Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off
print(10 >> 5) # 0
# Dry Run in bits
# 10 = 0000 1010
# 5 = 0000 0101
# 10 & 5 = 0000 0000
# 10 | 5 = 0000 1111
# 10 ^ 5 = 0000 1111
# ~10 = 1111 0101
# 10 << 5 = 0010 1000 0000
# 10 >> 5 = 0000 0000