What module would you use if you wanted to parse command-line arguments to your Python script?
The argparse
module. For example:
import argparse
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers (default: find the max)')
args = parser.parse_args()
What module would you use if you wanted to manually trigger a garbage collection to free up memory?
The gc
module. For example:
import gc
gc.collect()
What module would you use if you wanted to terminate a script prematurely?
The sys
module. For example:
import sys
sys.exit()
What module would you use if you wanted to get a list of all Mondays in a month?
The calendar
module. For example:
>>> import calendar
>>> year = 2023
>>> month = 4
>>> ndays = calendar.monthrange(year, month)[1]
>>> mondays = [
>>> day
>>> for day in range(1, ndays + 1)
>>> if calendar.weekday(year, month, day) == calendar.MONDAY
>>> ]
>>> print(mondays)
[3, 10, 17, 24]
What module would you use if you wanted to analyze a script to find the set of modules imported by it?
The modulefinder
module. For example:
from modulefinder import ModuleFinder
finder = ModuleFinder()
finder.run_script('your_script.py')
print('Loaded modules:', finder.modules)
What module would you use if you wanted to determine the execution time of a small code snippet?
The timeit
module. For example:
import timeit
timeit.timeit('char in text', setup='text = "sample text"; char = "e"', number=10000)
What module would you use if you wanted to define a named, unique set of values to represent states, types, settings, etc.?
The enum
module. For example:
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
What module would you use if you wanted to verify that a function returns expected results for given inputs?
The unittest
module. For example:
import unittest
class TestSum(unittest.TestCase):
def test_sum(self):
self.assertEqual(sum([1, 2, 3]), 6, "Should be 6")
if __name__ == '__main__':
unittest.main()
What module would you use if you wanted to hint that a variable could either be an integer or None?
The typing
module. For example:
from typing import Optional
number: Optional[int] = None
What module would you use if you wanted to securely hash passwords for storage in a database?
The hashlib
module. For example:
import hashlib
password = 'secret_password'.encode()
hashed_password = hashlib.sha256(password).hexdigest()
However, rolling your own solution for password storage is generally discouraged. Check the OWASP guidelines if that's what you want to do it.
The Python standard library is big, but how big is it? Did you know you can do low-level network communications with socket
? And measure the execution time of code with timeit
? Have you ever analyzed the syntax tree of a Python code with ast
or drawn a plot with turtle
? It even has a full sqlite3
implementation!
Get a grasp of all the functionality just one import
away.
The pathlib library offers the most convenient way of managing files and file paths in Python. Learn its full power and master its API.
If you have ever read Peter Norvig's code from his pytudes, you will agree that his code is odd, surprisingly clear and insultingly concise.
In this deck, we have collected dozens of tricks in Pytudes so you can come closer to his genius.
Learn the concepts and words of Python: type hints, context managers, methods, comprehensions, generic functions…
Knowing the words is a first step towards a complete mental model of Python.
Built-in functions are always available in Python without needing to import them.
We are talking about print
, range
, enumerate
, but also isinstance
, map
and staticmethod
.
The list is short, but holds huge power.
Get daily reminders and build the habit of reviewing your cards.
Keep your streak and learn bit by bit.
Feel your knowledge growing and be amazed by your progress.
Check my Youtube channel to see how the site and the decks are built.
Fill your email and start reviewing cards.
Every day you will receive a new email with a link to your daily session.
Can you really learn Python without writing code?
Of course not. Python.cards is not a replacement for coding, it's a shortcut to level up your Python skills. If you want to improve your overall programming skills, check out my Software Design School.
Are you planning to support other languages?
Yes. I'm working on Rust.cards, following the same philosophy as Python.cards.
What spaced repetition algorithm does Python.cards use?
We use the classic Anki algorithm, which is a modified version of SM-2. We have actually open sourced our implementation of the algorithm as the Python module simple-spaced-repetition. You can find the code on Github.