/learning-python

Tasks for colleges to learn python

Tasks for colleges to learn python

Tools

Editor

Edit your code in editor like PyCharm (Community edition) or IntelliJ Idea (Community edition) because of context documentation and code completion.

Code completion. Shortcut ctrl + space.

source: https://realpython.com/python-ides-code-editors-guide/

Context documentation. Shortcut ctrl + q.

source: https://realpython.com/python-ides-code-editors-guide/

Git

Use git to backup and share your project with others. Atlassian company has awesome git explanation: https://www.atlassian.com/git

Python

Reading exceptions

It's important to orient in reading python exceptions. See this example

/home/develop/learning-python/venv/bin/python /home/develop/learning-python/test.py
Traceback (most recent call last):
  File "/usr/lib/python3.5/xml/etree/ElementPath.py", line 263, in iterfind
    selector = _cache[cache_key]
KeyError: ('//record/surname', None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/develop/learning-python/test.py", line 4, in <module>
    filtered = root.findall('//record/surname')
  File "/usr/lib/python3.5/xml/etree/ElementPath.py", line 304, in findall
    return list(iterfind(elem, path, namespaces))
  File "/usr/lib/python3.5/xml/etree/ElementPath.py", line 268, in iterfind
    raise SyntaxError("cannot use absolute path on element")
SyntaxError: cannot use absolute path on element

Key information is:

  • KeyError: ('//record/surname', None) traslated: there is something wrong with //record/surname
  • SyntaxError: cannot use absolute path on element translated: wrong syntax, detail is 'cannot use absolute path on element'. You can google it.
  • File "/home/develop/learning-python/test.py", line 4, in <module> translated: the error happend in file test.py on line 4

Virtualenv

virtualenv is a tool to create isolated Python environments. virtualenv creates a folder which contains all the necessary executables to use the packages that a Python project would need.

Links:

Commands:

  • virtualenv venv
  • source ./venv/bin/activate

TODO

Pip

pip is the package installer for Python.

Links:

Commands:

  • pip install -r requirements.txt

TODO

Jupyter Notebook

The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain

  • text (markdown)
  • live code
  • visualizations

There is pretty nice introduction on youtube:

Python libraries

Pandas

Pandas is a library for data manipulation and analysis. In particular, it offers data structures and operations for manipulating numerical tables and time series.

Video of 10-minute tour of pandas

NumPy

NumPy is a library adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.

Basic

Basic 01 - Hello world

Your first task is to create a hello word script in python. So your script has to print "hello word".

Hints:

Basic 02 - Read file

Read file ./files/everything-is-awesome.txt and output the first row on the screen.

Hints:

Basic 03 - Count lines

Read file ./files/everything-is-awesome.txt and print count of all lines in the file.

Hint:

Basic 04 - split string

Split Everything Is AWESOME!!! and print each word on separate line.

Hints:

Basic 05 - regex

Regular expressions are patterns used to match character combinations in strings.

Read file ./files/everything-is-awesome.txt and count occurrence of word is (ignore case).

Hints:

XML

There are several approaches to XML programming in Python. We will start with XML analysis using python ElementTree and XPath.

Learn about XPath:

Core example:

import xml.etree.ElementTree as ET

# Parse XML file and get its root element
root = ET.parse('./files/people.xml').getroot()

# Filter using XPath. 
filtered = root.findall('.//record[firstname="John"]')

for element in filtered:
    print(ET.tostring(element).decode('utf-8'))

XML 01 - Find surnames

Change the example to find all users with surname Doe.

XML 02 - List surnames only

Print surnames to output, one surname per line. ElementTree class has limitations in xpath functionality.

Hint: