<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.itcollege.ee/index.php?action=history&amp;feed=atom&amp;title=Advanced_Python_%28Fall_2017%29%2Flecture1</id>
	<title>Advanced Python (Fall 2017)/lecture1 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.itcollege.ee/index.php?action=history&amp;feed=atom&amp;title=Advanced_Python_%28Fall_2017%29%2Flecture1"/>
	<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Advanced_Python_(Fall_2017)/lecture1&amp;action=history"/>
	<updated>2026-06-21T22:57:06Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=Advanced_Python_(Fall_2017)/lecture1&amp;diff=124087&amp;oldid=prev</id>
		<title>Eroman: Created page with &quot;= Lecture 1 =  == When to use what python version ==  Use python 3 if you:  are starting a new project, that you know will only be used internally, or used once, or used by pe...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=Advanced_Python_(Fall_2017)/lecture1&amp;diff=124087&amp;oldid=prev"/>
		<updated>2017-09-08T20:23:29Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;= Lecture 1 =  == When to use what python version ==  Use python 3 if you:  are starting a new project, that you know will only be used internally, or used once, or used by pe...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= Lecture 1 =&lt;br /&gt;
&lt;br /&gt;
== When to use what python version ==&lt;br /&gt;
&lt;br /&gt;
Use python 3 if you:&lt;br /&gt;
&lt;br /&gt;
are starting a new project, that you know will only be used internally, or used once, or used by people you know will use python 3.&lt;br /&gt;
&lt;br /&gt;
When to python 2:&lt;br /&gt;
&lt;br /&gt;
you are using an existing python 2 code base. or there is a python2 only package and there are no alternatives.&lt;br /&gt;
&lt;br /&gt;
When to write code that will work on both python 2 and 3:&lt;br /&gt;
&lt;br /&gt;
You are making a public package that may be used by people using either version. Or you are adding to a python2 project, and you want it to be forward compatible.&lt;br /&gt;
&lt;br /&gt;
I recommend that you write python 3 code or write code that is compatible with both versions.&lt;br /&gt;
&lt;br /&gt;
== Python 2 and 3 differences ==&lt;br /&gt;
&lt;br /&gt;
There are many small differences between python 2 and python 3. here is a comprhensive list: http://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html&lt;br /&gt;
&lt;br /&gt;
=== Strings ===&lt;br /&gt;
&lt;br /&gt;
In python 3, all strings are unicode. python 2 strings are not any specific encoding. They are actually a list of bytes.&lt;br /&gt;
&lt;br /&gt;
This can cause problems when you try to use special characters. the following code will not work on python 2, but will work on python 3.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;quot;Hello {}&amp;quot;.format(u&amp;quot;Kõrvits&amp;quot;)&amp;lt;/source&amp;gt;&lt;br /&gt;
on python 2, you will get this&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;In [1]: &amp;amp;quot;Hello {}&amp;amp;quot;.format(u&amp;amp;quot;Kõrvits&amp;amp;quot;)&lt;br /&gt;
---------------------------------------------------------------------------&lt;br /&gt;
UnicodeEncodeError                        Traceback (most recent call last)&lt;br /&gt;
&amp;amp;lt;ipython-input-1-462f402d17a8&amp;amp;gt; in &amp;amp;lt;module&amp;amp;gt;()&lt;br /&gt;
----&amp;amp;gt; 1 &amp;amp;quot;Hello {}&amp;amp;quot;.format(u&amp;amp;quot;Kõrvits&amp;amp;quot;)&lt;br /&gt;
&lt;br /&gt;
UnicodeEncodeError: &amp;#039;ascii&amp;#039; codec can&amp;#039;t encode character u&amp;#039;\xf5&amp;#039; in position 1: ordinal not in range(128)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
To make a string explicitly unicode typed, put a &amp;#039;u&amp;#039; before a string literal&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;u&amp;#039;hi&amp;#039;&amp;lt;/pre&amp;gt;&lt;br /&gt;
or import&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;from __future__ import unicode_literals&amp;lt;/pre&amp;gt;&lt;br /&gt;
which will make all string literals unicode.&lt;br /&gt;
&lt;br /&gt;
== Python Style ==&lt;br /&gt;
&lt;br /&gt;
python has a very strict and standarized and widely style guide. It is referred to as &amp;amp;quot;PEP8&amp;amp;quot;. PEP is the Python Enhancement Project, and 8 is the number of the proposal. This proposal, PEP8, contains the style rules for python code. Read it here: https://www.python.org/dev/peps/pep-0008/&lt;br /&gt;
&lt;br /&gt;
it should be followed.&lt;br /&gt;
&lt;br /&gt;
=== Checking the style guide ===&lt;br /&gt;
&lt;br /&gt;
use a package called &amp;#039;flake8&amp;#039;. It is quite sensible out of the box. It does not check naming, or all style rules. It checks spacing and syntax.&lt;br /&gt;
&lt;br /&gt;
to install &amp;#039;pip install flake8&amp;#039;&lt;br /&gt;
&lt;br /&gt;
to run &amp;#039;python -m flake8 PATH&amp;#039;&lt;br /&gt;
&lt;br /&gt;
You may also use &amp;#039;PyLint&amp;#039;, which is much more robust and checks more things, including naming and documentation. It has to be configured because it is often too strict.&lt;br /&gt;
&lt;br /&gt;
There is also &amp;#039;autopep8&amp;#039;, which reformats your code to follow PEP8. ## Task 1&lt;br /&gt;
&lt;br /&gt;
Make bad legacy python code styled correctly and run on both python 2 and 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;# coding: utf-8&lt;br /&gt;
&lt;br /&gt;
nAMES = [u&amp;#039;Võsandi&amp;#039;,u&amp;#039;hyvä&amp;#039;];&lt;br /&gt;
for Name in nAMES:&lt;br /&gt;
    if &amp;#039;y&amp;#039; in Name:&lt;br /&gt;
        print(&amp;#039;{} is Finnish&amp;#039;.format(Name));&lt;br /&gt;
    else:&lt;br /&gt;
        print(&amp;#039;{} is Estonian&amp;#039;.format(Name));&amp;lt;/source&amp;gt;&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;# coding: utf-8&lt;br /&gt;
&lt;br /&gt;
from __future__ import unicode_literals&lt;br /&gt;
from __future__ import print_function&lt;br /&gt;
NAMES = [&amp;#039;Võsandi&amp;#039;, &amp;#039;hyvä&amp;#039;]&lt;br /&gt;
&lt;br /&gt;
for name in NAMES:&lt;br /&gt;
    if &amp;#039;y&amp;#039; in name:&lt;br /&gt;
        print(&amp;#039;{} is Finnish&amp;#039;.format(name))&lt;br /&gt;
    else:&lt;br /&gt;
        print(&amp;#039;{} is Estonian&amp;#039;.format(name))&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Eroman</name></author>
	</entry>
</feed>