| 244 | **Dictionary Differences** |
| 245 | {{{#!th style="background: #ddd" |
| 246 | '''Python 2''' |
| 247 | }}} |
| 248 | {{{#!th style="background: #ddd" |
| 249 | '''Python 3''' |
| 250 | }}} |
| 251 | |----------------------- |
| 252 | {{{#!td |
| 253 | {{{ |
| 254 | >>> d = {'a': 1, b'b': 2, u'c':3} |
| 255 | |
| 256 | >>>d['a'] |
| 257 | 1 |
| 258 | >>>d[b'a'] |
| 259 | 1 |
| 260 | >>>d[u'a'] |
| 261 | 1 |
| 262 | |
| 263 | >>>d['b'] |
| 264 | 2 |
| 265 | >>>d[b'b'] |
| 266 | 2 |
| 267 | >>>d[u'b'] |
| 268 | 2 |
| 269 | |
| 270 | >>>d['c'] |
| 271 | 3 |
| 272 | >>>d[b'c'] |
| 273 | 3 |
| 274 | >>>d[u'c'] |
| 275 | 3 |
| 276 | }}} |
| 277 | }}} |
| 278 | {{{#!td |
| 279 | {{{ |
| 280 | >>> d = {'a': 1, b'b': 2, u'c':3} |
| 281 | |
| 282 | >>>d['a'] |
| 283 | 1 |
| 284 | >>>d[b'a'] |
| 285 | KeyError: b'a' |
| 286 | >>>d[u'a'] |
| 287 | 1 |
| 288 | |
| 289 | >>>d['b'] |
| 290 | KeyError: 'b' |
| 291 | >>>d[b'b'] |
| 292 | 2 |
| 293 | >>>d[u'b'] |
| 294 | KeyError: 'b' |
| 295 | |
| 296 | >>>d['c'] |
| 297 | 3 |
| 298 | >>>d[b'c'] |
| 299 | KeyError: b'c' |
| 300 | >>>d[u'c'] |
| 301 | 3 |
| 302 | }}} |
| 303 | }}} |
| 304 | |----------------------- |
| 305 | {{{#!th colspan=2 |
| 306 | When special characters are involved: |
| 307 | }}} |
| 308 | |----------------------- |
| 309 | {{{#!td |
| 310 | {{{ |
| 311 | >>> d = {'ř': 1, b'š': 2, u'ý':3} |
| 312 | |
| 313 | >>>d['ř'] |
| 314 | 1 |
| 315 | >>>d[b'ř'] |
| 316 | 1 |
| 317 | >>>d[u'ř'] |
| 318 | KeyError: u '\u0159' |
| 319 | |
| 320 | >>>d['š'] |
| 321 | 1 |
| 322 | >>>d[b'š'] |
| 323 | 1 |
| 324 | >>>d[u'š'] |
| 325 | KeyError: u '\u0161' |
| 326 | |
| 327 | >>>d['ý'] |
| 328 | KeyError: '\xc3\xbd' |
| 329 | >>>d[b'ý'] |
| 330 | KeyError: '\xc3\xbd' |
| 331 | >>>d[u'ý'] |
| 332 | 3 |
| 333 | }}} |
| 334 | }}} |
| 335 | {{{#!td |
| 336 | {{{ |
| 337 | >>> d = {'ř': 1, b's': 2, u'ý':3} |
| 338 | |
| 339 | >>>d['ř'] |
| 340 | 1 |
| 341 | >>>d[b'ř'] |
| 342 | SyntaxError |
| 343 | >>>d[u'ř'] |
| 344 | 1 |
| 345 | |
| 346 | >>>d['s'] |
| 347 | KeyError: 's' |
| 348 | >>>d[b's'] |
| 349 | 2 |
| 350 | >>>d[u's'] |
| 351 | KeyError: 's' |
| 352 | |
| 353 | >>>d['ý'] |
| 354 | 3 |
| 355 | >>>d[b'ý'] |
| 356 | SyntaxError |
| 357 | >>>d[u'ý'] |
| 358 | 3 |
| 359 | }}} |
| 360 | }}} |
| 361 | |