Tasks for colleges to learn python
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/
Use git to backup and share your project with others. Atlassian company has awesome git explanation: https://www.atlassian.com/git
- What is git about: https://www.atlassian.com/git/tutorials/what-is-version-control
- Basic commands: https://www.atlassian.com/git/tutorials/setting-up-a-repository
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 filetest.py
on line 4
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:
- https://docs.python-guide.org/dev/virtualenvs/#lower-level-virtualenv
- https://virtualenv.pypa.io/en/latest/
Commands:
virtualenv venv
source ./venv/bin/activate
TODO
pip
is the package installer for Python.
Links:
Commands:
pip install -r requirements.txt
TODO
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:
- See the first 2:30 minutes from this video: Jupyter Notebook Tutorial: Introduction, Setup, and Walkthrough about
- How To Use Jupyter Notebooks with youtube video
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 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.
Your first task is to create a hello word script in python. So your script has to print "hello word".
Hints:
- print function: https://www.w3schools.com/python/ref_func_print.asp
- execute the script: https://askubuntu.com/questions/244378/running-python-file-in-terminal
Read file ./files/everything-is-awesome.txt
and output the first row on the screen.
Hints:
- read file: https://www.w3schools.com/python/python_file_open.asp
- types of opening files: https://www.w3schools.com/python/python_file_handling.asp
Read file ./files/everything-is-awesome.txt
and print count of all lines in the file.
Hint:
- create an integer variable and set it to zero: https://www.w3schools.com/python/python_numbers.asp
- loop through the line of the file and each increase the variable by one: https://www.w3schools.com/python/python_file_open.asp
Split Everything Is AWESOME!!!
and print each word on separate line.
Hints:
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:
- Regular expressions
- Regular expression tester with syntax highlighting (awesome tool!!!): https://regexr.com/
- Python regex documentation: https://docs.python.org/3/library/re.html
- Python & regex in w3schools: https://www.w3schools.com/python/python_regex.asp
- length of an array: https://www.w3schools.com/python/ref_func_len.asp
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'))
Change the example to find all users with surname Doe
.
Print surnames to output, one surname per line. ElementTree class has limitations in xpath functionality.
Hint:
- Select surname elements with xpath. Output will containe lines like
<surname>Puckett</surname>
. - Take a look on ElementTree.tostring documentation: https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.tostring
- Set tostring parameter to
method="text"
.