Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
📚 Playground and cheatsheet for learning Python. Collection of Python scripts that are split by topics and contain code examples with explanations.
License
trekhleb/learn-python
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
🇺🇦 UKRAINEIS BEING ATTACKED BY RUSSIAN ARMY. CIVILIANS ARE GETTING KILLED. RESIDENTIAL AREAS ARE GETTING BOMBED.
- Help Ukraine via:
- More info onwar.ukraine.ua andMFA of Ukraine
This is a collection of Python scripts that are split bytopics and containcode examples with explanations, different use cases and links to further readings.
Read this in:Português,Español,Traditional Chinese.
It is aplayground because you may change or add the code to see how it worksandtest it out using assertions. It also allows youtolint 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 keepcode quality pretty high from very beginning.
It is acheatsheet because you may get back to these code examples once you want to recap thesyntax ofstandard Python statements and constructions. Also because thecode is full of assertions you'll be able to see expected functions/statements output right awaywithout launching them.
You might also be interested in 🤖Interactive Machine Learning Experiments
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 hereHere might go more detailed explanation of the current topic (i.e. general info about Lists)."""deftest_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 actionsquares= [1,4,9,16,25]# Lists can be indexed and sliced.# Indexing returns the item.assertsquares[0]==1# <-- Assertions here illustrate the result.# Slicing returns a new list.assertsquares[-3:]== [9,16,25]# <-- Assertions here illustrate the result.
So normally you might want to do the following:
- Find the topic you want to learn or recap.
- Read comments and/or documentation that is linked in each script's docstring (as in example above).
- Look at code examples and assertions to see usage examples and expected output.
- Change code or add new assertions to see how things work.
- Run tests andlint the code to see if it work and iswritten correctly.
- Getting Started
- Operators
- Arithmetic Operators (
+
,-
,*
,/
,//
,%
,**
) - Bitwise Operators (
&
,|
,^
,>>
,<<
,~
) - Assignment Operators (
=
,+=
,-=
,/=
,//=
etc.) - Comparison Operator (
==
,!=
,>
,<
,>=
,<=
) - Logical Operators (
and
,or
,not
) - Identity Operators (
is
,is not
) - Membership Operators (
in
,not in
)
- Arithmetic Operators (
- Data Types
- Numbers (including booleans)
- Strings and their methods
- Lists and their methods (including list comprehensions)
- Tuples
- Sets and their methods
- Dictionaries
- Type Casting
- Control Flow
- Functions
- Function Definition (
def
andreturn
statements) - Scopes of Variables Inside Functions (
global
andnonlocal
statements) - Default Argument Values
- Keyword Arguments
- Arbitrary Argument Lists
- Unpacking Argument Lists (
*
and**
statements) - Lambda Expressions (
lambda
statement) - Documentation Strings
- Function Annotations
- Function Decorators
- Function Definition (
- Classes
- Modules
- Errors and Exceptions
- Handling Exceptions (
try
statement) - Raising Exceptions (
raise
statement)
- Handling Exceptions (
- Files
- Reading and Writing (
with
statement) - Methods of File Objects
- Reading and Writing (
- Additions
- The
pass
statement - Generators (
yield
statement)
- The
- Brief Tour of the Standard Libraries
- Serialization (
json
library) - File Wildcards (
glob
library) - String Pattern Matching (
re
library) - Mathematics (
math
,random
,statistics
libraries) - Dates and Times (
datetime
library) - Data Compression (
zlib
library)
- Serialization (
- User input
- Terminal input (
input
statement)
- Terminal input (
Installing Python
Make sure that you havePython3 installed on your machine.
You might want to usevenv standard Python libraryto create virtual environments and have Python, pip and all dependent packages to be installed andserved from the local project directory to avoid messing with system wide packages and theirversions.
Depending on your installation you might have access to Python3 interpreter either byrunningpython
orpython3
. The same goes for pip package manager - it may be accessible eitherby runningpip
orpip3
.
You may check your Python version by running:
python --version
Note that in this repository whenever you seepython
it will be assumed that it is Python3.
Installing dependencies
Install all dependencies that are required for the project by running:
pip install -r requirements.txt
Tests are made usingpytest framework.
You may add new tests for yourself by adding files and functions withtest_
prefix(i.e.test_topic.py
withdef test_sub_topic()
function inside).
To run all the tests please execute the following command from the project root folder:
pytest
To run specific tests please execute:
pytest ./path/to/the/test_file.py
Linting is done usingpylint andflake8 libraries.
To check if the code is written with respecttoPEP 8 style guide please run:
pylint ./src/
In case if linter will detect error (i.e.missing-docstring
) you may want to read more aboutspecific error by running:
pylint --help-msg=missing-docstring
To check if the code is written with respecttoPEP 8 style guide please run:
flake8 ./src
Or if you want to have more detailed output you may run:
flake8 ./src --statistics --show-source --count
About
📚 Playground and cheatsheet for learning Python. Collection of Python scripts that are split by topics and contain code examples with explanations.
Topics
Resources
License
Code of conduct
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.