Základní rozdíly mezi Python 2 a Python 3

Funkce print()

Jedná se o pravděpodobně nejviditelnější změnu mezi Python 2 a Python 3. Příkaz print z Python 2 byl v Python 3 nahrazen funkcí python().

Python 2

print 'Hello, World!'
print('Hello, World!')
print "text", ; print 'print more text on the same line'
Hello, World!
Hello, World!
text print more text on the same line

Python 3

print('Hello, World!')

print("some text,", end="")
print(' print more text on the same line')
Hello, World!
some text, print more text on the same line
print 'Hello, World!'
File "<ipython-input-3-139a7c5835bd>", line 1
    print 'Hello, World!'
                        ^
SyntaxError: invalid syntax

Dělení celými čísly

Dělení celými čísly se chová rúzným způsobem v Python 2 a v Python 3.

Python 2

print '3 / 2 =', 3 / 2
print '3 // 2 =', 3 // 2
print '3 / 2.0 =', 3 / 2.0
print '3 // 2.0 =', 3 // 2.0
3 / 2 = 1
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0

Python 3

print('3 / 2 =', 3 / 2)
print('3 // 2 =', 3 // 2)
print('3 / 2.0 =', 3 / 2.0)
print('3 // 2.0 =', 3 // 2.0)
3 / 2 = 1.5
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0

Podpora Unicode

Python 2 má separátní typy pro str(), jedná se o ASCII znaky a dále pak unicode(). V Python 3 jsou všechny řetězce str() standardně Unicode znaky. Dále pak Python 3 zavádí podporu byte.

Vyvolání výjimky

V Python 2 jsou možné dvě možnosti jak vyvolat výjimku. Python 3 umožňuje jen jednu.

Python 2

raise IOError, "file error"
raise IOError, "file error"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: file error
raise IOError("file error")
raise IOError("file error")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: file error

Python 3

Následující způsob zápisu v Python 3 nefunguje:

raise IOError, "file error"
raise IOError, "file error"
  File "<stdin>", line 1
    raise IOError, "file error"
                 ^
SyntaxError: invalid syntax

Jediný správný zápis je tento:

raise IOError("file error")
raise IOError("file error")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: file error

Ošetřování výjimek

Oštřování výjimek se také změnilo. Podívej se, jak se ošetřují výjimky v Python 2 a v Python 3.

Python 2

try:
    let_us_cause_a_NameError
except NameError, err:
    print err, '--> our error message'
name 'let_us_cause_a_NameError' is not defined --> our error message

Python 3

try:
    let_us_cause_a_NameError
except NameError as err:
    print(err, '--> our error message')
name 'let_us_cause_a_NameError' is not defined --> our error message

Uživatelské vstupy

V Pythonu 2 existují dva způsoby, jak získat vstup od uživatele. Funkce input() a funkce raw_input(). Problém ve funkci input() je, že může být potenciálně nebezpečná, protože nevrací vždy typ string.

Python 2

Python 2.7.6
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> my_input = input('enter a number: ')

enter a number: 123

>>> type(my_input)
<type 'int'>

>>> my_input = raw_input('enter a number: ')

enter a number: 123

>>> type(my_input)
<type 'str'>

Python 3

Python 3.7.0 (default, Aug 24 2018, 20:34:01)
[Clang 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> my_input = input('enter a number: ')
enter a number: 123
>>> type(my_input)
<class 'str'>
{
  "data": {
    "sessionMaterial": {
      "id": "session-material:2018/tieto:install:5",
      "title": "Základní rozdíly mezi Python 2 a Python 3",
      "html": "\n          \n    \n\n    <h2>Z&#xE1;kladn&#xED; rozd&#xED;ly mezi Python 2 a Python 3</h2>\n<h3>Funkce print()</h3>\n<p>Jedn&#xE1; se o pravd&#x11B;podobn&#x11B; nejviditeln&#x11B;j&#x161;&#xED; zm&#x11B;nu mezi Python 2 a Python 3. P&#x159;&#xED;kaz <em>print</em> z Python 2 byl v Python 3 nahrazen funkc&#xED; <em>python()</em>.</p>\n<p><strong>Python 2</strong>\n</p>\n<div class=\"highlight\"><pre><code>print &apos;Hello, World!&apos;\nprint(&apos;Hello, World!&apos;)\nprint &quot;text&quot;, ; print &apos;print more text on the same line&apos;</code></pre></div><div class=\"highlight\"><pre><code>Hello, World!\nHello, World!\ntext print more text on the same line</code></pre></div><p><strong>Python 3</strong></p>\n<div class=\"highlight\"><pre><code>print(&apos;Hello, World!&apos;)\n\nprint(&quot;some text,&quot;, end=&quot;&quot;)\nprint(&apos; print more text on the same line&apos;)</code></pre></div><div class=\"highlight\"><pre><code>Hello, World!\nsome text, print more text on the same line</code></pre></div><div class=\"highlight\"><pre><code>print &apos;Hello, World!&apos;</code></pre></div><div class=\"highlight\"><pre><code>File &quot;&lt;ipython-input-3-139a7c5835bd&gt;&quot;, line 1\n    print &apos;Hello, World!&apos;\n                        ^\nSyntaxError: invalid syntax</code></pre></div><h3>D&#x11B;len&#xED; cel&#xFD;mi &#x10D;&#xED;sly</h3>\n<p>D&#x11B;len&#xED; cel&#xFD;mi &#x10D;&#xED;sly se chov&#xE1; r&#xFA;zn&#xFD;m zp&#x16F;sobem v Python 2 a v Python 3.</p>\n<p><strong>Python 2</strong></p>\n<div class=\"highlight\"><pre><code>print &apos;3 / 2 =&apos;, 3 / 2\nprint &apos;3 // 2 =&apos;, 3 // 2\nprint &apos;3 / 2.0 =&apos;, 3 / 2.0\nprint &apos;3 // 2.0 =&apos;, 3 // 2.0</code></pre></div><div class=\"highlight\"><pre><code>3 / 2 = 1\n3 // 2 = 1\n3 / 2.0 = 1.5\n3 // 2.0 = 1.0</code></pre></div><p><strong>Python 3</strong></p>\n<div class=\"highlight\"><pre><code>print(&apos;3 / 2 =&apos;, 3 / 2)\nprint(&apos;3 // 2 =&apos;, 3 // 2)\nprint(&apos;3 / 2.0 =&apos;, 3 / 2.0)\nprint(&apos;3 // 2.0 =&apos;, 3 // 2.0)</code></pre></div><div class=\"highlight\"><pre><code>3 / 2 = 1.5\n3 // 2 = 1\n3 / 2.0 = 1.5\n3 // 2.0 = 1.0</code></pre></div><h3>Podpora Unicode</h3>\n<p>Python 2 m&#xE1; separ&#xE1;tn&#xED; typy pro str(), jedn&#xE1; se o ASCII znaky a d&#xE1;le pak unicode(). V Python 3 jsou v&#x161;echny &#x159;et&#x11B;zce str() standardn&#x11B; Unicode znaky. D&#xE1;le pak Python 3 zav&#xE1;d&#xED; podporu <em>byte</em>.</p>\n<h3>Vyvol&#xE1;n&#xED; v&#xFD;jimky</h3>\n<p>V Python 2 jsou mo&#x17E;n&#xE9; dv&#x11B; mo&#x17E;nosti jak vyvolat v&#xFD;jimku. Python 3 umo&#x17E;&#x148;uje jen jednu.</p>\n<p><strong>Python 2</strong></p>\n<div class=\"highlight\"><pre><code>raise IOError, &quot;file error&quot;</code></pre></div><div class=\"highlight\"><pre><code>raise IOError, &quot;file error&quot;\nTraceback (most recent call last):\n  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;\nIOError: file error</code></pre></div><div class=\"highlight\"><pre><code>raise IOError(&quot;file error&quot;)</code></pre></div><div class=\"highlight\"><pre><code>raise IOError(&quot;file error&quot;)\nTraceback (most recent call last):\n  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;\nIOError: file error</code></pre></div><p><strong>Python 3</strong></p>\n<p>N&#xE1;sleduj&#xED;c&#xED; zp&#x16F;sob z&#xE1;pisu v Python 3 nefunguje:</p>\n<div class=\"highlight\"><pre><code>raise IOError, &quot;file error&quot;</code></pre></div><div class=\"highlight\"><pre><code>raise IOError, &quot;file error&quot;\n  File &quot;&lt;stdin&gt;&quot;, line 1\n    raise IOError, &quot;file error&quot;\n                 ^\nSyntaxError: invalid syntax</code></pre></div><p>Jedin&#xFD; spr&#xE1;vn&#xFD; z&#xE1;pis je tento:</p>\n<div class=\"highlight\"><pre><code>raise IOError(&quot;file error&quot;)</code></pre></div><div class=\"highlight\"><pre><code>raise IOError(&quot;file error&quot;)\nTraceback (most recent call last):\n  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;\nOSError: file error</code></pre></div><h3>O&#x161;et&#x159;ov&#xE1;n&#xED; v&#xFD;jimek</h3>\n<p>O&#x161;t&#x159;ov&#xE1;n&#xED; v&#xFD;jimek se tak&#xE9; zm&#x11B;nilo. Pod&#xED;vej se, jak se o&#x161;et&#x159;uj&#xED; v&#xFD;jimky v Python 2 a v Python 3.</p>\n<p><strong>Python 2</strong></p>\n<div class=\"highlight\"><pre><code>try:\n    let_us_cause_a_NameError\nexcept NameError, err:\n    print err, &apos;--&gt; our error message&apos;</code></pre></div><div class=\"highlight\"><pre><code>name &apos;let_us_cause_a_NameError&apos; is not defined --&gt; our error message</code></pre></div><p><strong>Python 3</strong></p>\n<div class=\"highlight\"><pre><code>try:\n    let_us_cause_a_NameError\nexcept NameError as err:\n    print(err, &apos;--&gt; our error message&apos;)</code></pre></div><div class=\"highlight\"><pre><code>name &apos;let_us_cause_a_NameError&apos; is not defined --&gt; our error message</code></pre></div><h3>U&#x17E;ivatelsk&#xE9; vstupy</h3>\n<p>V Pythonu 2 existuj&#xED; dva zp&#x16F;soby, jak z&#xED;skat vstup od u&#x17E;ivatele. Funkce <em>input()</em> a funkce <em>raw_input()</em>. Probl&#xE9;m ve funkci <em>input()</em> je, &#x17E;e m&#x16F;&#x17E;e b&#xFD;t potenci&#xE1;ln&#x11B; nebezpe&#x10D;n&#xE1;, proto&#x17E;e nevrac&#xED; v&#x17E;dy typ <em>string</em>.</p>\n<p><strong>Python 2</strong></p>\n<div class=\"highlight\"><pre><code>Python 2.7.6\n[GCC 4.0.1 (Apple Inc. build 5493)] on darwin\nType &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.\n\n&gt;&gt;&gt; my_input = input(&apos;enter a number: &apos;)\n\nenter a number: 123\n\n&gt;&gt;&gt; type(my_input)\n&lt;type &apos;int&apos;&gt;\n\n&gt;&gt;&gt; my_input = raw_input(&apos;enter a number: &apos;)\n\nenter a number: 123\n\n&gt;&gt;&gt; type(my_input)\n&lt;type &apos;str&apos;&gt;</code></pre></div><p><strong>Python 3</strong></p>\n<div class=\"highlight\"><pre><code>Python 3.7.0 (default, Aug 24 2018, 20:34:01)\n[Clang 9.0.0 (clang-900.0.39.2)] on darwin\nType &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.\n&gt;&gt;&gt; my_input = input(&apos;enter a number: &apos;)\nenter a number: 123\n&gt;&gt;&gt; type(my_input)\n&lt;class &apos;str&apos;&gt;</code></pre></div>\n\n\n        "
    }
  }
}