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 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
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.
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š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
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:2019/tieto-ostrava-jaro:install:5", "title": "Základní rozdíly mezi Python 2 a Python 3", "html": "\n \n \n\n <h2>Základní rozdíly mezi Python 2 a Python 3</h2>\n<h3>Funkce print()</h3>\n<p>Jedná se o pravděpodobně nejviditelnější změnu mezi Python 2 a Python 3. Příkaz <em>print</em> z Python 2 byl v Python 3 nahrazen funkcí <em>python()</em>.</p>\n<p><strong>Python 2</strong>\n</p>\n<div class=\"highlight\"><pre><code>print 'Hello, World!'\nprint('Hello, World!')\nprint "text", ; print 'print more text on the same line'</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('Hello, World!')\n\nprint("some text,", end="")\nprint(' print more text on the same line')</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 'Hello, World!'</code></pre></div><div class=\"highlight\"><pre><code>File "<ipython-input-3-139a7c5835bd>", line 1\n print 'Hello, World!'\n ^\nSyntaxError: invalid syntax</code></pre></div><h3>Dělení celými čísly</h3>\n<p>Dělení celými čísly se chová rúzným způsobem v Python 2 a v Python 3.</p>\n<p><strong>Python 2</strong></p>\n<div class=\"highlight\"><pre><code>print '3 / 2 =', 3 / 2\nprint '3 // 2 =', 3 // 2\nprint '3 / 2.0 =', 3 / 2.0\nprint '3 // 2.0 =', 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('3 / 2 =', 3 / 2)\nprint('3 // 2 =', 3 // 2)\nprint('3 / 2.0 =', 3 / 2.0)\nprint('3 // 2.0 =', 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á 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 <em>byte</em>.</p>\n<h3>Vyvolání výjimky</h3>\n<p>V Python 2 jsou možné dvě možnosti jak vyvolat výjimku. Python 3 umožňuje jen jednu.</p>\n<p><strong>Python 2</strong></p>\n<div class=\"highlight\"><pre><code>raise IOError, "file error"</code></pre></div><div class=\"highlight\"><pre><code>raise IOError, "file error"\nTraceback (most recent call last):\n File "<stdin>", line 1, in <module>\nIOError: file error</code></pre></div><div class=\"highlight\"><pre><code>raise IOError("file error")</code></pre></div><div class=\"highlight\"><pre><code>raise IOError("file error")\nTraceback (most recent call last):\n File "<stdin>", line 1, in <module>\nIOError: file error</code></pre></div><p><strong>Python 3</strong></p>\n<p>Následující způsob zápisu v Python 3 nefunguje:</p>\n<div class=\"highlight\"><pre><code>raise IOError, "file error"</code></pre></div><div class=\"highlight\"><pre><code>raise IOError, "file error"\n File "<stdin>", line 1\n raise IOError, "file error"\n ^\nSyntaxError: invalid syntax</code></pre></div><p>Jediný správný zápis je tento:</p>\n<div class=\"highlight\"><pre><code>raise IOError("file error")</code></pre></div><div class=\"highlight\"><pre><code>raise IOError("file error")\nTraceback (most recent call last):\n File "<stdin>", line 1, in <module>\nOSError: file error</code></pre></div><h3>Ošetřování výjimek</h3>\n<p>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.</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, '--> our error message'</code></pre></div><div class=\"highlight\"><pre><code>name 'let_us_cause_a_NameError' is not defined --> 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, '--> our error message')</code></pre></div><div class=\"highlight\"><pre><code>name 'let_us_cause_a_NameError' is not defined --> our error message</code></pre></div><h3>Uživatelské vstupy</h3>\n<p>V Pythonu 2 existují dva způsoby, jak získat vstup od uživatele. Funkce <em>input()</em> a funkce <em>raw_input()</em>. Problém ve funkci <em>input()</em> je, že může být potenciálně nebezpečná, protože nevrací vž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 "help", "copyright", "credits" or "license" for more information.\n\n>>> my_input = input('enter a number: ')\n\nenter a number: 123\n\n>>> type(my_input)\n<type 'int'>\n\n>>> my_input = raw_input('enter a number: ')\n\nenter a number: 123\n\n>>> type(my_input)\n<type 'str'></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 "help", "copyright", "credits" or "license" for more information.\n>>> my_input = input('enter a number: ')\nenter a number: 123\n>>> type(my_input)\n<class 'str'></code></pre></div>\n\n\n " } } }