I'm learning Python and I decided to create a repository where I could put Python script samples with standard Python syntax, structures and statements examples just to be able to quickly recap how to use this or that feature of the language.
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.
How to Use The Repo
Each Python script in the 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:
- Find the topic you're 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 and lint the code to see if it work and is written correctly.
Table of Contents
- 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
- The
if
statement -
The
for
statement (andrange()
function) - The
while
statement - The
try
statements - The
break
statement - The
continue
statement
- The
-
Functions
-
Function Definition (
def
andreturn
statements) - Default Argument Values
- Keyword Arguments
- Arbitrary Argument Lists
-
Unpacking Argument Lists (
*
and**
statements) -
Lambda Expressions (
lambda
statement) - Documentation Strings
- Function Annotations
-
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 (
I hope you find this repository helpful!
Top comments (4)
This is very helpful... Am just starting out on Python
Nice to hear that!
really nice !!!!!
This is great, I was looking for a repo with Python examples for a long time! I don't use Python daily and this is the perfect way to easily refresh the concepts.