trekhleb / learn-python
- пятница, 31 августа 2018 г. в 00:15:54
Python
📚 Playground and cheatsheet for learning Python
This is a collection of Python scripts that are split by topics and contain code examples with explanations, different use cases and links to further readings.
It is a playground because you may change or add the code to see how it works and test it out using assertions. It also allows you to lint the code you've wrote and check if it fits to Python code style guide. Altogether it might make your learning process to be more interactive and it might help you to keep code quality pretty high from very beginning.
It is a cheatsheet because you may get back to these code examples once you want to recap the syntax of standard Python statements and constructions. Also because the code is full of assertions you'll be able to see expected functions/statements output right away without launching them.
Each Python script in this repository has the following structure:
"""Lists <--- Name of the topic here
# @see: https://www.learnpython.org/en/Lists <-- Link to further readings goes here
Here might go more detailed explanation of the current topic (i.e. general info about Lists).
"""
def test_list_type():
"""Explanation of sub-topic goes here.
Each file contains test functions that illustrate sub-topics (i.e. lists type, lists methods).
"""
# Here is an example of how to build a list. <-- Comments here explain the action
squares = [1, 4, 9, 16, 25]
# Lists can be indexed and sliced.
# Indexing returns the item.
assert squares[0] == 1 # <-- Assertions here illustrate the result.
# Slicing returns a new list.
assert squares[-3:] == [9, 16, 25] # <-- Assertions here illustrate the result.So normally you might want to do the following:
+, -, *, /, //, %, **)&, |, ^, >>, <<, ~)=, +=, -=, /=, //= etc.)==, !=, >, <, >=, <=)and, or, not)is, is not)in, not in)if statementfor statement (and range() function)while statementtry statementsbreak statementcontinue statementdef and return statements)* and ** statements)lambda statement)try statement)raise statement)with statement)pass statementyield statement)json library)glob library)re library)math, random, statistics libraries)datetime library)zlib library)Installing Python
Make sure that you have Python3 installed on your machine.
You might want to use venv standard Python library to create virtual environments and have Python, pip and all dependent packages to be installed and served from the local project directory to avoid messing with system wide packages and their versions.
Depending on your installation you might have access to Python3 interpreter either by
running python or python3. The same goes for pip package manager - it may be accessible either
by running pip or pip3.
You may check your Python version by running:
python --versionNote that in this repository whenever you see python it will be assumed that it is Python 3.
Installing dependencies
Install all dependencies that are required for the project by running:
pip install -r requirements.txtTests are made using pytest framework.
You may add new tests for yourself by adding files and functions with test_ prefix
(i.e. test_topic.py with def test_sub_topic() function inside).
To run all the tests please execute the following command from the project root folder:
pytestTo run specific tests please execute:
pytest ./path/to/the/test_file.pyLinting is done using pylint library.
To check if the code is written with respect to PEP 8 style guide please run:
pylint ./src/In case if linter will detect error (i.e. missing-docstring) you may want to read more about
specific error by running:
pylint --help-msg=missing-docstring