added unicode section

This commit is contained in:
rasbt 2014-05-24 20:06:31 -04:00
parent 5c4a2e1b3e
commit 24c5642d24
2 changed files with 586 additions and 1 deletions

View File

@ -1754,6 +1754,7 @@ document.write('<a h'+'ref'+'="ma'+'ilto'+':'+e+'">'+'email'+'<\/'+'a'+'>');
<li><p><a href="#future_module">Using the <code>__future__</code> module</a></p></li>
<li><p><a href="#The-print-function">The print function</a></p></li>
<li><p><a href="#Integer-division">Integer division</a></p></li>
<li><p><a href="#Unicode">Unicode</a></p></li>
<li><p><a href="#xrange">xrange</a></p></li>
<li><p><a href="#Raising-exceptions">Raising exceptions</a></p></li>
<li><p><a href="#Handling-exceptions">Handling exceptions</a></p></li>
@ -2250,6 +2251,357 @@ Python 3.4.1
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><br> <br></p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Unicode">Unicode<a class="anchor-link" href="#Unicode">&#182;</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>[<a href="#Sections">back to the section-overview</a>]</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Python 2 has ASCII <code>str()</code> types, separate <code>unicode()</code>, but no <code>byte</code> type.</p>
<p>Now, in Python 3, we finally have Unicode (utf-8) <code>str</code>ings, and 2 byte classes: <code>byte</code> and <code>bytearray</code>s.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="python-2">Python 2</h4>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">
In&nbsp;[2]:
</div>
<div class="inner_cell">
<div class="input_area">
<div class="highlight"><pre><span class="k">print</span> <span class="s">&#39;Python&#39;</span><span class="p">,</span> <span class="n">python_version</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area"><div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>
Python 2.7.6
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">
In&nbsp;[3]:
</div>
<div class="inner_cell">
<div class="input_area">
<div class="highlight"><pre><span class="k">print</span> <span class="nb">type</span><span class="p">(</span><span class="nb">unicode</span><span class="p">(</span><span class="s">&#39;this is like a python3 str type&#39;</span><span class="p">))</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area"><div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>
&lt;type &apos;unicode&apos;&gt;
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">
In&nbsp;[4]:
</div>
<div class="inner_cell">
<div class="input_area">
<div class="highlight"><pre><span class="k">print</span> <span class="nb">type</span><span class="p">(</span><span class="n">b</span><span class="s">&#39;byte type does not exist&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area"><div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>
&lt;type &apos;str&apos;&gt;
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">
In&nbsp;[5]:
</div>
<div class="inner_cell">
<div class="input_area">
<div class="highlight"><pre><span class="k">print</span> <span class="s">&#39;they are really&#39;</span> <span class="o">+</span> <span class="n">b</span><span class="s">&#39; the same&#39;</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area"><div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>
they are really the same
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">
In&nbsp;[7]:
</div>
<div class="inner_cell">
<div class="input_area">
<div class="highlight"><pre><span class="k">print</span> <span class="nb">type</span><span class="p">(</span><span class="nb">bytearray</span><span class="p">(</span><span class="n">b</span><span class="s">&#39;bytearray oddly does exist though&#39;</span><span class="p">))</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area"><div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>
&lt;type &apos;bytearray&apos;&gt;
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><br></p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="python-3">Python 3</h4>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">
In&nbsp;[6]:
</div>
<div class="inner_cell">
<div class="input_area">
<div class="highlight"><pre><span class="k">print</span><span class="p">(</span><span class="s">&#39;Python&#39;</span><span class="p">,</span> <span class="n">python_version</span><span class="p">())</span>
<span class="k">print</span><span class="p">(</span><span class="s">&#39;strings are now utf-8 </span><span class="se">\u03BC</span><span class="s">nico</span><span class="se">\u0394</span><span class="s">é!&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area"><div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>
Python 3.4.1
strings are now utf-8 μnicoΔé!
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">
In&nbsp;[8]:
</div>
<div class="inner_cell">
<div class="input_area">
<div class="highlight"><pre><span class="k">print</span><span class="p">(</span><span class="s">&#39;Python&#39;</span><span class="p">,</span> <span class="n">python_version</span><span class="p">(),</span> <span class="n">end</span><span class="o">=</span><span class="s">&quot;&quot;</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">&#39; has&#39;</span><span class="p">,</span> <span class="nb">type</span><span class="p">(</span><span class="n">b</span><span class="s">&#39; bytes for storing data&#39;</span><span class="p">))</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area"><div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>
Python 3.4.1 has &lt;class &apos;bytes&apos;&gt;
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">
In&nbsp;[11]:
</div>
<div class="inner_cell">
<div class="input_area">
<div class="highlight"><pre><span class="k">print</span><span class="p">(</span><span class="s">&#39;and Python&#39;</span><span class="p">,</span> <span class="n">python_version</span><span class="p">(),</span> <span class="n">end</span><span class="o">=</span><span class="s">&quot;&quot;</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">&#39; also has&#39;</span><span class="p">,</span> <span class="nb">type</span><span class="p">(</span><span class="nb">bytearray</span><span class="p">(</span><span class="n">b</span><span class="s">&#39;bytearrays&#39;</span><span class="p">)))</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area"><div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>
and Python 3.4.1 also has &lt;class &apos;bytearray&apos;&gt;
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">
In&nbsp;[13]:
</div>
<div class="inner_cell">
<div class="input_area">
<div class="highlight"><pre><span class="s">&#39;note that we cannot add a string&#39;</span> <span class="o">+</span> <span class="n">b</span><span class="s">&#39;bytes for data&#39;</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area"><div class="prompt"></div>
<div class="output_subarea output_text output_pyerr">
<pre>
<span class="ansired">---------------------------------------------------------------------------</span>
<span class="ansired">TypeError</span> Traceback (most recent call last)
<span class="ansigreen">&lt;ipython-input-13-d3e8942ccf81&gt;</span> in <span class="ansicyan">&lt;module&gt;</span><span class="ansiblue">()</span>
<span class="ansigreen">----&gt; 1</span><span class="ansired"> </span><span class="ansiblue">&apos;note that we cannot add a string&apos;</span> <span class="ansiblue">+</span> <span class="ansiblue">b&apos;bytes for data&apos;</span><span class="ansiblue"></span>
<span class="ansired">TypeError</span>: Can&apos;t convert &apos;bytes&apos; object to str implicitly</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:b9ba6aad52c6458ba2d9ee8691fcace515fc01f1029499253de1f48cc664fbc0"
"signature": "sha256:bce64714d9af46abdf20f98e6b5b0b51cd1240612c1dbd99a40d812aea22dcdf"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -79,6 +79,8 @@
"\n",
"- [Integer division](#Integer-division)\n",
"\n",
"- [Unicode](#Unicode)\n",
"\n",
"- [xrange](#xrange)\n",
"\n",
"- [Raising exceptions](#Raising-exceptions)\n",
@ -434,6 +436,237 @@
"<br>"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Unicode"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to the section-overview](#Sections)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python 2 has ASCII `str()` types, separate `unicode()`, but no `byte` type. \n",
"\n",
"Now, in Python 3, we finally have Unicode (utf-8) `str`ings, and 2 byte classes: `byte` and `bytearray`s."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Python 2"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print 'Python', python_version()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Python 2.7.6\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print type(unicode('this is like a python3 str type'))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"<type 'unicode'>\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print type(b'byte type does not exist')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"<type 'str'>\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print 'they are really' + b' the same'"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"they are really the same\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print type(bytearray(b'bytearray oddly does exist though'))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"<type 'bytearray'>\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Python 3"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print('Python', python_version())\n",
"print('strings are now utf-8 \\u03BCnico\\u0394\u00e9!')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Python 3.4.1\n",
"strings are now utf-8 \u03bcnico\u0394\u00e9!\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print('Python', python_version(), end=\"\")\n",
"print(' has', type(b' bytes for storing data'))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Python 3.4.1 has <class 'bytes'>\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print('and Python', python_version(), end=\"\")\n",
"print(' also has', type(bytearray(b'bytearrays')))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"and Python 3.4.1 also has <class 'bytearray'>\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"'note that we cannot add a string' + b'bytes for data'"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "Can't convert 'bytes' object to str implicitly",
"output_type": "pyerr",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-13-d3e8942ccf81>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m'note that we cannot add a string'\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34mb'bytes for data'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: Can't convert 'bytes' object to str implicitly"
]
}
],
"prompt_number": 13
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"<br>"
]
},
{
"cell_type": "heading",
"level": 2,