|
100 | 100 | { |
101 | 101 | "cell_type": "code", |
102 | 102 | "execution_count": null, |
103 | | - "metadata": {}, |
| 103 | + "metadata": { |
| 104 | + "collapsed": true |
| 105 | + }, |
104 | 106 | "outputs": [], |
105 | 107 | "source": [ |
106 | 108 | "x = -3\n", |
|
160 | 162 | { |
161 | 163 | "cell_type": "code", |
162 | 164 | "execution_count": null, |
163 | | - "metadata": {}, |
| 165 | + "metadata": { |
| 166 | + "collapsed": true |
| 167 | + }, |
164 | 168 | "outputs": [], |
165 | 169 | "source": [ |
166 | 170 | "gene = \"BRCA2\"\n", |
|
187 | 191 | { |
188 | 192 | "cell_type": "code", |
189 | 193 | "execution_count": null, |
190 | | - "metadata": {}, |
| 194 | + "metadata": { |
| 195 | + "collapsed": true |
| 196 | + }, |
191 | 197 | "outputs": [], |
192 | 198 | "source": [ |
193 | 199 | "x = 11\n", |
|
231 | 237 | { |
232 | 238 | "cell_type": "code", |
233 | 239 | "execution_count": null, |
234 | | - "metadata": {}, |
| 240 | + "metadata": { |
| 241 | + "collapsed": true |
| 242 | + }, |
235 | 243 | "outputs": [], |
236 | 244 | "source": [ |
237 | 245 | "x = -5\n", |
|
247 | 255 | "cell_type": "markdown", |
248 | 256 | "metadata": {}, |
249 | 257 | "source": [ |
250 | | - "Python has two additional comparison operators <tt>is</tt> and <tt>is not</tt>. These compare whether two objects are the same object, whereas <tt>==</tt> and <tt>!=</tt> compare whether values are the same.\n", |
| 258 | + "Python has two additional comparison operators <tt>is</tt> and <tt>is not</tt>.\n", |
| 259 | + "\n", |
| 260 | + "These compare whether two objects are the same object, whereas <tt>==</tt> and <tt>!=</tt> compare whether values are the same.\n", |
| 261 | + "\n", |
| 262 | + "There is a simple rule of thumb to tell when to use `==` or `is`:\n", |
| 263 | + "- `==` is for **value** equality. Use it to check if two objects have the same value.\n", |
| 264 | + "- `is` is for **reference** equality. Use it to check if two references refer to the same object.\n", |
251 | 265 | "\n", |
252 | | - "As an example in Python:" |
| 266 | + "*Something to note*, you will get unexpected and inconsistent results if you mistakenly use `is` to compare for reference equality on integers:" |
| 267 | + ] |
| 268 | + }, |
| 269 | + { |
| 270 | + "cell_type": "code", |
| 271 | + "execution_count": null, |
| 272 | + "metadata": {}, |
| 273 | + "outputs": [], |
| 274 | + "source": [ |
| 275 | + "a = 500\n", |
| 276 | + "b = 500\n", |
| 277 | + "print(a == b) # True\n", |
| 278 | + "print(a is b) # False" |
| 279 | + ] |
| 280 | + }, |
| 281 | + { |
| 282 | + "cell_type": "markdown", |
| 283 | + "metadata": {}, |
| 284 | + "source": [ |
| 285 | + "Another example with lists `x`, `y`, and `z`:\n", |
| 286 | + "- `y` being a copy of `x`\n", |
| 287 | + "- and `z` being another reference to the same object as `x`" |
253 | 288 | ] |
254 | 289 | }, |
255 | 290 | { |
|
262 | 297 | "y = x[:] # y is a copy of x\n", |
263 | 298 | "z = x\n", |
264 | 299 | "print(x)\n", |
| 300 | + "print(y)\n", |
| 301 | + "print(z)\n", |
265 | 302 | "print(\"Are values of y and x the same?\", y == x)\n", |
266 | 303 | "print(\"Are objects y and x the same?\", y is x)\n", |
267 | 304 | "print(\"Are values of z and x the same?\", z == x)\n", |
268 | | - "print(\"Are objects z and x the same?\", z is x)\n", |
| 305 | + "print(\"Are objects z and x the same?\", z is x)" |
| 306 | + ] |
| 307 | + }, |
| 308 | + { |
| 309 | + "cell_type": "code", |
| 310 | + "execution_count": null, |
| 311 | + "metadata": {}, |
| 312 | + "outputs": [], |
| 313 | + "source": [ |
269 | 314 | "# Let's change x\n", |
270 | 315 | "x[1] = 23\n", |
271 | 316 | "print(x)\n", |
| 317 | + "print(y)\n", |
| 318 | + "print(z)\n", |
272 | 319 | "print(\"Are values of y and x the same?\", y == x)\n", |
273 | 320 | "print(\"Are objects y and x the same?\", y is x)\n", |
274 | 321 | "print(\"Are values of z and x the same?\", z == x)\n", |
|
299 | 346 | { |
300 | 347 | "cell_type": "code", |
301 | 348 | "execution_count": null, |
302 | | - "metadata": {}, |
| 349 | + "metadata": { |
| 350 | + "collapsed": true |
| 351 | + }, |
303 | 352 | "outputs": [], |
304 | 353 | "source": [ |
305 | 354 | "x = '' # An empty string\n", |
|
0 commit comments