Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduce_syntax_and_history_of_Python_from_2.4...

Manabu TERADA
June 28, 2018
260

 Introduce_syntax_and_history_of_Python_from_2.4_to_3.6-inKyushu-2.pdf

Manabu TERADA

June 28, 2018
Tweet

Transcript

  1. Introduce syntax and Introduce syntax and Introduce syntax and Introduce

    syntax and Introduce syntax and Introduce syntax and history of Python from 2.4 to 3.6 history of Python from 2.4 to 3.6 history of Python from 2.4 to 3.6 history of Python from 2.4 to 3.6 history of Python from 2.4 to 3.6 history of Python from 2.4 to 3.6 - PyCon Kyushu 2018 - - PyCon Kyushu 2018 - - PyCon Kyushu 2018 - - PyCon Kyushu 2018 - - PyCon Kyushu 2018 - - PyCon Kyushu 2018 - 2018-06-30 2018-06-30 2018-06-30 2018-06-30 2018-06-30 2018-06-30 @terapyon @terapyon @terapyon @terapyon @terapyon @terapyon 1 / 45
  2. Self introduction Self introduction Self introduction Self introduction Self introduction

    Self introduction Manabu TERADA Manabu TERADA Manabu TERADA Manabu TERADA Manabu TERADA Manabu TERADA ( 寺田 学) ( 寺田 学) ( 寺田 学) ( 寺田 学) ( 寺田 学) ( 寺田 学) Python engineer Python engineer Python engineer Python engineer Python engineer Python engineer Board Chair of PyCon JP (Japan) Board Chair of PyCon JP (Japan) Board Chair of PyCon JP (Japan) Board Chair of PyCon JP (Japan) Board Chair of PyCon JP (Japan) Board Chair of PyCon JP (Japan) Plone Foundation Ambassador Plone Foundation Ambassador Plone Foundation Ambassador Plone Foundation Ambassador Plone Foundation Ambassador Plone Foundation Ambassador PSF Contributing member PSF Contributing member PSF Contributing member PSF Contributing member PSF Contributing member PSF Contributing member Member of NVDA Japanese Team Member of NVDA Japanese Team Member of NVDA Japanese Team Member of NVDA Japanese Team Member of NVDA Japanese Team Member of NVDA Japanese Team                               2 / 45
  3. Our company Our company Our company Our company Our company

    Our company CMS Communications Inc. CMS Communications Inc. CMS Communications Inc. CMS Communications Inc. CMS Communications Inc. CMS Communications Inc. Our company is CMScom based in Tokyo, Japan Our company is CMScom based in Tokyo, Japan Our company is CMScom based in Tokyo, Japan Our company is CMScom based in Tokyo, Japan Our company is CMScom based in Tokyo, Japan Our company is CMScom based in Tokyo, Japan We use Plone for building web sites We use Plone for building web sites We use Plone for building web sites We use Plone for building web sites We use Plone for building web sites We use Plone for building web sites Universities, Public institutions Universities, Public institutions Universities, Public institutions Universities, Public institutions Universities, Public institutions Universities, Public institutions Engineering companies Engineering companies Engineering companies Engineering companies Engineering companies Engineering companies Supporting education tools Supporting education tools Supporting education tools Supporting education tools Supporting education tools Supporting education tools Plone Plone Plone Plone Plone Plone, , , , , , Pyramid and Open edX Pyramid and Open edX Pyramid and Open edX Pyramid and Open edX Pyramid and Open edX Pyramid and Open edX                               3 / 45
  4. Question Question Question Question Question Question Do you use Do

    you use Do you use Do you use Do you use Do you use Python 2 Python 2 Python 2 Python 2 Python 2 Python 2 or or or or or or Python 3 Python 3 Python 3 Python 3 Python 3 Python 3 5 / 45
  5. Question Question Question Question Question Question Do you Do you

    Do you Do you Do you Do you PREFER PREFER PREFER PREFER PREFER PREFER Python 2 Python 2 Python 2 Python 2 Python 2 Python 2 or or or or or or Python 3 Python 3 Python 3 Python 3 Python 3 Python 3 6 / 45
  6. The history of Python by release date The history of

    Python by release date The history of Python by release date The history of Python by release date The history of Python by release date The history of Python by release date 7 / 45
  7. PEP PEP PEP PEP PEP PEP Python Enhancement Proposal Python

    Enhancement Proposal Python Enhancement Proposal Python Enhancement Proposal Python Enhancement Proposal Python Enhancement Proposal https://www.python.org/dev/peps/pep-0001/ https://www.python.org/dev/peps/pep-0001/ https://www.python.org/dev/peps/pep-0001/ https://www.python.org/dev/peps/pep-0001/ https://www.python.org/dev/peps/pep-0001/ https://www.python.org/dev/peps/pep-0001/ PEP stands for Python Enhancement Proposal. A PEP is a design PEP stands for Python Enhancement Proposal. A PEP is a design PEP stands for Python Enhancement Proposal. A PEP is a design PEP stands for Python Enhancement Proposal. A PEP is a design PEP stands for Python Enhancement Proposal. A PEP is a design PEP stands for Python Enhancement Proposal. A PEP is a design document providing information to the Python community, or document providing information to the Python community, or document providing information to the Python community, or document providing information to the Python community, or document providing information to the Python community, or document providing information to the Python community, or describing a new feature for Python or its processes or environment. describing a new feature for Python or its processes or environment. describing a new feature for Python or its processes or environment. describing a new feature for Python or its processes or environment. describing a new feature for Python or its processes or environment. describing a new feature for Python or its processes or environment. The PEP should provide a concise technical speci cation of the The PEP should provide a concise technical speci cation of the The PEP should provide a concise technical speci cation of the The PEP should provide a concise technical speci cation of the The PEP should provide a concise technical speci cation of the The PEP should provide a concise technical speci cation of the feature and a rationale for the feature. feature and a rationale for the feature. feature and a rationale for the feature. feature and a rationale for the feature. feature and a rationale for the feature. feature and a rationale for the feature. 8 / 45
  8. Important changes in each version Important changes in each version

    Important changes in each version Important changes in each version Important changes in each version Important changes in each version between Python 2.4 and Python between Python 2.4 and Python between Python 2.4 and Python between Python 2.4 and Python between Python 2.4 and Python between Python 2.4 and Python 3.6 3.6 3.6 3.6 3.6 3.6 3.7 3.7 3.7 3.7 3.7 3.7 Picking up major changes of each version Picking up major changes of each version Picking up major changes of each version Picking up major changes of each version Picking up major changes of each version Picking up major changes of each version 10 / 45
  9. Important changes in each version Important changes in each version

    Important changes in each version Important changes in each version Important changes in each version Important changes in each version Python 2.4  (released 2004) Python 2.4  (released 2004) Python 2.4  (released 2004) Python 2.4  (released 2004) Python 2.4  (released 2004) Python 2.4  (released 2004) PEP 218: Built-In Set Objects PEP 218: Built-In Set Objects PEP 218: Built-In Set Objects PEP 218: Built-In Set Objects PEP 218: Built-In Set Objects PEP 218: Built-In Set Objects PEP 289: Generator Expressions PEP 289: Generator Expressions PEP 289: Generator Expressions PEP 289: Generator Expressions PEP 289: Generator Expressions PEP 289: Generator Expressions PEP 318: Decorators for Functions and Methods PEP 318: Decorators for Functions and Methods PEP 318: Decorators for Functions and Methods PEP 318: Decorators for Functions and Methods PEP 318: Decorators for Functions and Methods PEP 318: Decorators for Functions and Methods PEP 322: Reverse Iteration PEP 322: Reverse Iteration PEP 322: Reverse Iteration PEP 322: Reverse Iteration PEP 322: Reverse Iteration PEP 322: Reverse Iteration PEP 328: Multi-line Imports PEP 328: Multi-line Imports PEP 328: Multi-line Imports PEP 328: Multi-line Imports PEP 328: Multi-line Imports PEP 328: Multi-line Imports                               11 / 45
  10. Important changes in each version Important changes in each version

    Important changes in each version Important changes in each version Important changes in each version Important changes in each version Python 2.5 (released 2006) Python 2.5 (released 2006) Python 2.5 (released 2006) Python 2.5 (released 2006) Python 2.5 (released 2006) Python 2.5 (released 2006) PEP 308: Conditional Expressions PEP 308: Conditional Expressions PEP 308: Conditional Expressions PEP 308: Conditional Expressions PEP 308: Conditional Expressions PEP 308: Conditional Expressions PEP 328: Absolute and Relative Imports PEP 328: Absolute and Relative Imports PEP 328: Absolute and Relative Imports PEP 328: Absolute and Relative Imports PEP 328: Absolute and Relative Imports PEP 328: Absolute and Relative Imports PEP 341: Uni ed try/except/ nally PEP 341: Uni ed try/except/ nally PEP 341: Uni ed try/except/ nally PEP 341: Uni ed try/except/ nally PEP 341: Uni ed try/except/ nally PEP 341: Uni ed try/except/ nally New Modules / ElementTree, sqlite3 and more package New Modules / ElementTree, sqlite3 and more package New Modules / ElementTree, sqlite3 and more package New Modules / ElementTree, sqlite3 and more package New Modules / ElementTree, sqlite3 and more package New Modules / ElementTree, sqlite3 and more package                         12 / 45
  11. Important changes in each version Important changes in each version

    Important changes in each version Important changes in each version Important changes in each version Important changes in each version Python 2.6 (released 2008) Python 2.6 (released 2008) Python 2.6 (released 2008) Python 2.6 (released 2008) Python 2.6 (released 2008) Python 2.6 (released 2008) PEP 343: The ‘with’ statement PEP 343: The ‘with’ statement PEP 343: The ‘with’ statement PEP 343: The ‘with’ statement PEP 343: The ‘with’ statement PEP 343: The ‘with’ statement PEP 3101: Advanced String Formatting / PEP 3101: Advanced String Formatting / PEP 3101: Advanced String Formatting / PEP 3101: Advanced String Formatting / PEP 3101: Advanced String Formatting / PEP 3101: Advanced String Formatting / str.format str.format str.format str.format str.format str.format PEP 3110: Exception-Handling Changes/ PEP 3110: Exception-Handling Changes/ PEP 3110: Exception-Handling Changes/ PEP 3110: Exception-Handling Changes/ PEP 3110: Exception-Handling Changes/ PEP 3110: Exception-Handling Changes/ except Exception as e except Exception as e except Exception as e except Exception as e except Exception as e except Exception as e PEP 3119: Abstract Base Classes PEP 3119: Abstract Base Classes PEP 3119: Abstract Base Classes PEP 3119: Abstract Base Classes PEP 3119: Abstract Base Classes PEP 3119: Abstract Base Classes PEP 3129: Class Decorators PEP 3129: Class Decorators PEP 3129: Class Decorators PEP 3129: Class Decorators PEP 3129: Class Decorators PEP 3129: Class Decorators                               13 / 45
  12. Important changes in each version Important changes in each version

    Important changes in each version Important changes in each version Important changes in each version Important changes in each version Python 2.7 (released 2010) Python 2.7 (released 2010) Python 2.7 (released 2010) Python 2.7 (released 2010) Python 2.7 (released 2010) Python 2.7 (released 2010) PEP 372: Adding an Ordered Dictionary to collections PEP 372: Adding an Ordered Dictionary to collections PEP 372: Adding an Ordered Dictionary to collections PEP 372: Adding an Ordered Dictionary to collections PEP 372: Adding an Ordered Dictionary to collections PEP 372: Adding an Ordered Dictionary to collections PEP 378: Format Speci er for Thousands Separator PEP 378: Format Speci er for Thousands Separator PEP 378: Format Speci er for Thousands Separator PEP 378: Format Speci er for Thousands Separator PEP 378: Format Speci er for Thousands Separator PEP 378: Format Speci er for Thousands Separator PEP 389: The argparse Module for Parsing Command Lines PEP 389: The argparse Module for Parsing Command Lines PEP 389: The argparse Module for Parsing Command Lines PEP 389: The argparse Module for Parsing Command Lines PEP 389: The argparse Module for Parsing Command Lines PEP 389: The argparse Module for Parsing Command Lines                   14 / 45
  13. Important changes in each version Important changes in each version

    Important changes in each version Important changes in each version Important changes in each version Important changes in each version Python 3.0 - 3.2 (released 2008) Python 3.0 - 3.2 (released 2008) Python 3.0 - 3.2 (released 2008) Python 3.0 - 3.2 (released 2008) Python 3.0 - 3.2 (released 2008) Python 3.0 - 3.2 (released 2008) PEP 3105: print As a Function PEP 3105: print As a Function PEP 3105: print As a Function PEP 3105: print As a Function PEP 3105: print As a Function PEP 3105: print As a Function PEP 3106: Revamping dict.keys(), .values() and .items() PEP 3106: Revamping dict.keys(), .values() and .items() PEP 3106: Revamping dict.keys(), .values() and .items() PEP 3106: Revamping dict.keys(), .values() and .items() PEP 3106: Revamping dict.keys(), .values() and .items() PEP 3106: Revamping dict.keys(), .values() and .items() PEP 238: Changing the Division Operator/ PEP 238: Changing the Division Operator/ PEP 238: Changing the Division Operator/ PEP 238: Changing the Division Operator/ PEP 238: Changing the Division Operator/ PEP 238: Changing the Division Operator/ // // // // // // PEP 274: Dict Comprehensions / PEP 274: Dict Comprehensions / PEP 274: Dict Comprehensions / PEP 274: Dict Comprehensions / PEP 274: Dict Comprehensions / PEP 274: Dict Comprehensions / {k: v for k, v in stuff} {k: v for k, v in stuff} {k: v for k, v in stuff} {k: v for k, v in stuff} {k: v for k, v in stuff} {k: v for k, v in stuff} Set literals and comprehensions Set literals and comprehensions Set literals and comprehensions Set literals and comprehensions Set literals and comprehensions Set literals and comprehensions                               15 / 45
  14. Important changes in each version Important changes in each version

    Important changes in each version Important changes in each version Important changes in each version Important changes in each version Python 3.3 (released 2012) Python 3.3 (released 2012) Python 3.3 (released 2012) Python 3.3 (released 2012) Python 3.3 (released 2012) Python 3.3 (released 2012) PEP 397: Python Launcher for Windows / PEP 397: Python Launcher for Windows / PEP 397: Python Launcher for Windows / PEP 397: Python Launcher for Windows / PEP 397: Python Launcher for Windows / PEP 397: Python Launcher for Windows / py command py command py command py command py command py command PEP 405: Virtual Environments PEP 405: Virtual Environments PEP 405: Virtual Environments PEP 405: Virtual Environments PEP 405: Virtual Environments PEP 405: Virtual Environments PEP 420: Implicit Namespace Packages PEP 420: Implicit Namespace Packages PEP 420: Implicit Namespace Packages PEP 420: Implicit Namespace Packages PEP 420: Implicit Namespace Packages PEP 420: Implicit Namespace Packages PEP 380: Syntax for Delegating to a Subgenerator/ PEP 380: Syntax for Delegating to a Subgenerator/ PEP 380: Syntax for Delegating to a Subgenerator/ PEP 380: Syntax for Delegating to a Subgenerator/ PEP 380: Syntax for Delegating to a Subgenerator/ PEP 380: Syntax for Delegating to a Subgenerator/ yield from yield from yield from yield from yield from yield from PEP 414: Explicit Unicode literals / PEP 414: Explicit Unicode literals / PEP 414: Explicit Unicode literals / PEP 414: Explicit Unicode literals / PEP 414: Explicit Unicode literals / PEP 414: Explicit Unicode literals / u"abc" u"abc" u"abc" u"abc" u"abc" u"abc"                               16 / 45
  15. Important changes in each version Important changes in each version

    Important changes in each version Important changes in each version Important changes in each version Important changes in each version Python 3.4 (released 2014) Python 3.4 (released 2014) Python 3.4 (released 2014) Python 3.4 (released 2014) Python 3.4 (released 2014) Python 3.4 (released 2014) PEP 453: Explicit Bootstrapping of PIP in Python Installations PEP 453: Explicit Bootstrapping of PIP in Python Installations PEP 453: Explicit Bootstrapping of PIP in Python Installations PEP 453: Explicit Bootstrapping of PIP in Python Installations PEP 453: Explicit Bootstrapping of PIP in Python Installations PEP 453: Explicit Bootstrapping of PIP in Python Installations PEP 435: Adding an Enum type to the Python standard library PEP 435: Adding an Enum type to the Python standard library PEP 435: Adding an Enum type to the Python standard library PEP 435: Adding an Enum type to the Python standard library PEP 435: Adding an Enum type to the Python standard library PEP 435: Adding an Enum type to the Python standard library PEP 428: The pathlib module – object-oriented lesystem paths PEP 428: The pathlib module – object-oriented lesystem paths PEP 428: The pathlib module – object-oriented lesystem paths PEP 428: The pathlib module – object-oriented lesystem paths PEP 428: The pathlib module – object-oriented lesystem paths PEP 428: The pathlib module – object-oriented lesystem paths PEP 450: Adding A Statistics Module To The Standard Library PEP 450: Adding A Statistics Module To The Standard Library PEP 450: Adding A Statistics Module To The Standard Library PEP 450: Adding A Statistics Module To The Standard Library PEP 450: Adding A Statistics Module To The Standard Library PEP 450: Adding A Statistics Module To The Standard Library                         17 / 45
  16. Important changes in each version Important changes in each version

    Important changes in each version Important changes in each version Important changes in each version Important changes in each version Python 3.5 (released 2015) Python 3.5 (released 2015) Python 3.5 (released 2015) Python 3.5 (released 2015) Python 3.5 (released 2015) Python 3.5 (released 2015) PEP 492: coroutines with async and await syntax PEP 492: coroutines with async and await syntax PEP 492: coroutines with async and await syntax PEP 492: coroutines with async and await syntax PEP 492: coroutines with async and await syntax PEP 492: coroutines with async and await syntax PEP 465: a new matrix multiplication operator: PEP 465: a new matrix multiplication operator: PEP 465: a new matrix multiplication operator: PEP 465: a new matrix multiplication operator: PEP 465: a new matrix multiplication operator: PEP 465: a new matrix multiplication operator: a @ b a @ b a @ b a @ b a @ b a @ b PEP 484: Type Hints PEP 484: Type Hints PEP 484: Type Hints PEP 484: Type Hints PEP 484: Type Hints PEP 484: Type Hints PEP 441: Improving Python ZIP Application Support PEP 441: Improving Python ZIP Application Support PEP 441: Improving Python ZIP Application Support PEP 441: Improving Python ZIP Application Support PEP 441: Improving Python ZIP Application Support PEP 441: Improving Python ZIP Application Support PEP 471: os.scandir() function a better and faster directory iterator PEP 471: os.scandir() function a better and faster directory iterator PEP 471: os.scandir() function a better and faster directory iterator PEP 471: os.scandir() function a better and faster directory iterator PEP 471: os.scandir() function a better and faster directory iterator PEP 471: os.scandir() function a better and faster directory iterator                               18 / 45
  17. Important changes in each version Important changes in each version

    Important changes in each version Important changes in each version Important changes in each version Important changes in each version Python 3.6 (released 2016) Python 3.6 (released 2016) Python 3.6 (released 2016) Python 3.6 (released 2016) Python 3.6 (released 2016) Python 3.6 (released 2016) PEP 498: formatted string literals: PEP 498: formatted string literals: PEP 498: formatted string literals: PEP 498: formatted string literals: PEP 498: formatted string literals: PEP 498: formatted string literals: f"{func()}" f"{func()}" f"{func()}" f"{func()}" f"{func()}" f"{func()}" PEP 515: underscores in numeric literals PEP 515: underscores in numeric literals PEP 515: underscores in numeric literals PEP 515: underscores in numeric literals PEP 515: underscores in numeric literals PEP 515: underscores in numeric literals PEP 526: syntax for variable annotations PEP 526: syntax for variable annotations PEP 526: syntax for variable annotations PEP 526: syntax for variable annotations PEP 526: syntax for variable annotations PEP 526: syntax for variable annotations PEP 525: asynchronous generators PEP 525: asynchronous generators PEP 525: asynchronous generators PEP 525: asynchronous generators PEP 525: asynchronous generators PEP 525: asynchronous generators PEP 530: asynchronous comprehensions PEP 530: asynchronous comprehensions PEP 530: asynchronous comprehensions PEP 530: asynchronous comprehensions PEP 530: asynchronous comprehensions PEP 530: asynchronous comprehensions PEP 506: Adding A Secrets Module To The Standard Library PEP 506: Adding A Secrets Module To The Standard Library PEP 506: Adding A Secrets Module To The Standard Library PEP 506: Adding A Secrets Module To The Standard Library PEP 506: Adding A Secrets Module To The Standard Library PEP 506: Adding A Secrets Module To The Standard Library                                     19 / 45
  18. Important changes in each version Important changes in each version

    Important changes in each version Important changes in each version Important changes in each version Important changes in each version Python 3.7 (release this month) Python 3.7 (release this month) Python 3.7 (release this month) Python 3.7 (release this month) Python 3.7 (release this month) Python 3.7 (release this month) PEP 553: Built-in breakpoint() PEP 553: Built-in breakpoint() PEP 553: Built-in breakpoint() PEP 553: Built-in breakpoint() PEP 553: Built-in breakpoint() PEP 553: Built-in breakpoint() PEP 557: Data Classes PEP 557: Data Classes PEP 557: Data Classes PEP 557: Data Classes PEP 557: Data Classes PEP 557: Data Classes Modify to impliment ssl package Modify to impliment ssl package Modify to impliment ssl package Modify to impliment ssl package Modify to impliment ssl package Modify to impliment ssl package                   20 / 45
  19. To compare the old style To compare the old style

    To compare the old style To compare the old style To compare the old style To compare the old style with the new style with the new style with the new style with the new style with the new style with the new style for the syntax and the structure for the syntax and the structure for the syntax and the structure for the syntax and the structure for the syntax and the structure for the syntax and the structure There isn’t enough time to cover everything in detail. There isn’t enough time to cover everything in detail. There isn’t enough time to cover everything in detail. There isn’t enough time to cover everything in detail. There isn’t enough time to cover everything in detail. There isn’t enough time to cover everything in detail. 21 / 45
  20. import () Multi-line Imports import () Multi-line Imports import ()

    Multi-line Imports import () Multi-line Imports import () Multi-line Imports import () Multi-line Imports All version All version All version All version All version All version from module_name import func1, func2, \ func3, func4, \ func5, func6 Python 2.4 + Python 2.4 + Python 2.4 + Python 2.4 + Python 2.4 + Python 2.4 + from module_name import (func1, func2, func3, func4, func5, func6) 22 / 45
  21. list Comprehensions (1) list Comprehensions (1) list Comprehensions (1) list

    Comprehensions (1) list Comprehensions (1) list Comprehensions (1) No Comprehensions No Comprehensions No Comprehensions No Comprehensions No Comprehensions No Comprehensions li = [] for i in range(10): li.append(i) Comprehensions Comprehensions Comprehensions Comprehensions Comprehensions Comprehensions li = [i for i in range(10)] 23 / 45
  22. list Comprehensions (2) list Comprehensions (2) list Comprehensions (2) list

    Comprehensions (2) list Comprehensions (2) list Comprehensions (2) No Comprehensions No Comprehensions No Comprehensions No Comprehensions No Comprehensions No Comprehensions li = [] for i in range(10): if i % 2 == 0: li.append(i) Comprehensions Comprehensions Comprehensions Comprehensions Comprehensions Comprehensions li = [i for i in range(10) if i % 2 == 0] 24 / 45
  23. list Comprehensions (3) list Comprehensions (3) list Comprehensions (3) list

    Comprehensions (3) list Comprehensions (3) list Comprehensions (3) No Comprehensions No Comprehensions No Comprehensions No Comprehensions No Comprehensions No Comprehensions li = [] for i in range(10): for j in range(2) li.append(i*j) Comprehensions Comprehensions Comprehensions Comprehensions Comprehensions Comprehensions li = [i*j for i in range(10) for j in range(2)] 25 / 45
  24. Comprehensions Comprehensions Comprehensions Comprehensions Comprehensions Comprehensions list list list list

    list list [i for i in range(10)] # --> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] dict (Python 2.7 + / Python 3.0 +) dict (Python 2.7 + / Python 3.0 +) dict (Python 2.7 + / Python 3.0 +) dict (Python 2.7 + / Python 3.0 +) dict (Python 2.7 + / Python 3.0 +) dict (Python 2.7 + / Python 3.0 +) {str(i): i for i in range(5)} # --> {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4} set (Python 2.7 + / Python 3.0 +) set (Python 2.7 + / Python 3.0 +) set (Python 2.7 + / Python 3.0 +) set (Python 2.7 + / Python 3.0 +) set (Python 2.7 + / Python 3.0 +) set (Python 2.7 + / Python 3.0 +) {i for i in range(10)} # --> {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} 26 / 45
  25. dict Comprehensions dict Comprehensions dict Comprehensions dict Comprehensions dict Comprehensions

    dict Comprehensions Python 2 Python 2 Python 2 Python 2 Python 2 Python 2 dic = dict((str(i), i) for i in range(10)) Python 3 Python 3 Python 3 Python 3 Python 3 Python 3 dic = {str(i): i for i in range(10)} 27 / 45
  26. generator generator generator generator generator generator Generator function Generator function

    Generator function Generator function Generator function Generator function def func(): for i in range(10): if i % 2 == 0: yield i Generator Expressions Generator Expressions Generator Expressions Generator Expressions Generator Expressions Generator Expressions (i for i in range(10) if i % 2 ==0) 28 / 45
  27. Generator Expressions in str.join() Generator Expressions in str.join() Generator Expressions

    in str.join() Generator Expressions in str.join() Generator Expressions in str.join() Generator Expressions in str.join() Python 2.3 Python 2.3 Python 2.3 Python 2.3 Python 2.3 Python 2.3 ",".join([str(i) for i in range(10)]) Python 2.4 + Python 2.4 + Python 2.4 + Python 2.4 + Python 2.4 + Python 2.4 + ",".join(str(i) for i in range(10)) Output Output Output Output Output Output '0,1,2,3,4,5,6,7,8,9' '0,1,2,3,4,5,6,7,8,9' '0,1,2,3,4,5,6,7,8,9' '0,1,2,3,4,5,6,7,8,9' '0,1,2,3,4,5,6,7,8,9' '0,1,2,3,4,5,6,7,8,9' 29 / 45
  28. list.sort vs sorted(list) list.sort vs sorted(list) list.sort vs sorted(list) list.sort

    vs sorted(list) list.sort vs sorted(list) list.sort vs sorted(list) All version (Less memory, but has side effect) All version (Less memory, but has side effect) All version (Less memory, but has side effect) All version (Less memory, but has side effect) All version (Less memory, but has side effect) All version (Less memory, but has side effect) li = [1, 3, 10, 2, 5] li.sort() Python 2.4 + Python 2.4 + Python 2.4 + Python 2.4 + Python 2.4 + Python 2.4 + (No side effect) (No side effect) (No side effect) (No side effect) (No side effect) (No side effect) li = [1, 3, 10, 2, 5] new_li = sorted(li) 30 / 45
  29. Conditional Expressions Conditional Expressions Conditional Expressions Conditional Expressions Conditional Expressions

    Conditional Expressions All version All version All version All version All version All version if condition: x = true_value else: x = false_value Like this Like this Like this Like this Like this Like this x = condition and true_value or false_value Python 2.5+ Python 2.5+ Python 2.5+ Python 2.5+ Python 2.5+ Python 2.5+ x = true_value if condition else false_value 31 / 45
  30. with with with with with with All version All version

    All version All version All version All version f = open("filename.txt") data = f.read() f.close() Python2.6 + Python2.6 + Python2.6 + Python2.6 + Python2.6 + Python2.6 + with open("filename.txt") as f: data = f.read() 32 / 45
  31. %s vs str.format vs f-string %s vs str.format vs f-string

    %s vs str.format vs f-string %s vs str.format vs f-string %s vs str.format vs f-string %s vs str.format vs f-string All version All version All version All version All version All version from datetime import date s = "Today: %s" % date.today() Python 2.6 + Python 2.6 + Python 2.6 + Python 2.6 + Python 2.6 + Python 2.6 + from datetime import date s = "Today: {}".format(date.today()) Python 3.6 + Python 3.6 + Python 3.6 + Python 3.6 + Python 3.6 + Python 3.6 + from datetime import date s = f"Today: {date.today()}" 33 / 45
  32. except Exception, e vs except Exception, e vs except Exception,

    e vs except Exception, e vs except Exception, e vs except Exception, e vs except Exception as e except Exception as e except Exception as e except Exception as e except Exception as e except Exception as e Python 2 Python 2 Python 2 Python 2 Python 2 Python 2 try: 1/0 except Exception, e: return e Python 2.6+ Python 2.6+ Python 2.6+ Python 2.6+ Python 2.6+ Python 2.6+ try: 1/0 except Exception as e: return e 34 / 45
  33. print statement vs print() function print statement vs print() function

    print statement vs print() function print statement vs print() function print statement vs print() function print statement vs print() function Python2 Python2 Python2 Python2 Python2 Python2 print "message" Python3 Python3 Python3 Python3 Python3 Python3 print("message") 35 / 45
  34. / vs // / vs // / vs // /

    vs // / vs // / vs // Python 2 Python 2 Python 2 Python 2 Python 2 Python 2 >>> 5 / 2 2 >>> 5 / 2.0 2.5 Python 3 Python 3 Python 3 Python 3 Python 3 Python 3 >>> 5 / 2 2.5 >>> 5 // 2 2 36 / 45
  35. enum (Python 3.4 +) enum (Python 3.4 +) enum (Python

    3.4 +) enum (Python 3.4 +) enum (Python 3.4 +) enum (Python 3.4 +) Python 3.4 + Python 3.4 + Python 3.4 + Python 3.4 + Python 3.4 + Python 3.4 + import enum class Tast(enum.IntEnum): todo = 1 in_progress = 2 done = 3 @classmethod def get_task_types(cls): return tuple((x.value, x.name) for x in cls) 37 / 45
  36. enum (Python 3.3) enum (Python 3.3) enum (Python 3.3) enum

    (Python 3.3) enum (Python 3.3) enum (Python 3.3) Python 3.3 Python 3.3 Python 3.3 Python 3.3 Python 3.3 Python 3.3 class Task: todo = 1 in_progress = 2 done = 3 task_types = ( (todo, "todo"), (in_progress, "in_progress"), (done, "done")) 38 / 45
  37. @ operator @ operator @ operator @ operator @ operator

    @ operator >>> import numpy as np >>> a = np.array([[1, 2]]) >>> b = np.array([[3], [4]]) Python3.4 Python3.4 Python3.4 Python3.4 Python3.4 Python3.4 >>> np.dot(a, b) array([[11]]) Python3.5+ Python3.5+ Python3.5+ Python3.5+ Python3.5+ Python3.5+ >>> a @ b array([[11]]) 39 / 45
  38. pathlib pathlib pathlib pathlib pathlib pathlib Python 3.5 Python 3.5

    Python 3.5 Python 3.5 Python 3.5 Python 3.5 import os current = os.getcwd() filepath = os.path.join(current, "dir", "filename.txt") with open(filepath) as f: data = f.read() Python 3.6 + Python 3.6 + Python 3.6 + Python 3.6 + Python 3.6 + Python 3.6 + from pathlib import Path p = Path(".") / "dir" / "filename.txt" with p.open() as f: data = f.read() 40 / 45
  39. os.listdir() vs pathlib os.listdir() vs pathlib os.listdir() vs pathlib os.listdir()

    vs pathlib os.listdir() vs pathlib os.listdir() vs pathlib Python 3.5 Python 3.5 Python 3.5 Python 3.5 Python 3.5 Python 3.5 import os for name in os.listdir(PATH): if not name.startswith('.') and os.path.isfile(os.path.join(PATH, nam e)): print(name) Python 3.6 + Python 3.6 + Python 3.6 + Python 3.6 + Python 3.6 + Python 3.6 + from pathlib import Path for entry in Path(PATH).iterdir(): if not entry.name.startswith('.') and entry.is_file(): print(entry.name) 41 / 45
  40. secrets module secrets module secrets module secrets module secrets module

    secrets module Python 3.6 + Python 3.6 + Python 3.6 + Python 3.6 + Python 3.6 + Python 3.6 + from secrets import choice import string alphabet = string.ascii_letters + string.digits password = ''.join(choice(alphabet) for i in range(8)) In particularly, secrets should be used in preference to the default In particularly, secrets should be used in preference to the default In particularly, secrets should be used in preference to the default In particularly, secrets should be used in preference to the default In particularly, secrets should be used in preference to the default In particularly, secrets should be used in preference to the default pseudo-random number generator in the random module, which is pseudo-random number generator in the random module, which is pseudo-random number generator in the random module, which is pseudo-random number generator in the random module, which is pseudo-random number generator in the random module, which is pseudo-random number generator in the random module, which is designed for modelling and simulation, not security or cryptography. designed for modelling and simulation, not security or cryptography. designed for modelling and simulation, not security or cryptography. designed for modelling and simulation, not security or cryptography. designed for modelling and simulation, not security or cryptography. designed for modelling and simulation, not security or cryptography. https://docs.python.org/3/library/secrets.html https://docs.python.org/3/library/secrets.html https://docs.python.org/3/library/secrets.html https://docs.python.org/3/library/secrets.html https://docs.python.org/3/library/secrets.html https://docs.python.org/3/library/secrets.html 42 / 45
  41. Data Classes Data Classes Data Classes Data Classes Data Classes

    Data Classes Python 3.7 + Python 3.7 + Python 3.7 + Python 3.7 + Python 3.7 + Python 3.7 + @dataclass class InventoryItem: name: str unit_price: float quantity_on_hand: int = 0 def total_cost(self) -> float: return self.unit_price * self.quantity_on_hand 43 / 45
  42. Data Classes Data Classes Data Classes Data Classes Data Classes

    Data Classes Python 3.6 Python 3.6 Python 3.6 Python 3.6 Python 3.6 Python 3.6 class InventoryItem: def __init__(self, name: str, unit_price: float, quantity_on_hand: int = 0) -> None: self.name = name self.unit_price = unit_price self.quantity_on_hand = quantity_on_hand .... (some special compare method) def total_cost(self) -> float: return self.unit_price * self.quantity_on_hand 44 / 45