Did you know that Python 🐍 has over 240 modules in the standard library?

Here is a MEGA thread 🧵with a super high level overview of those modules...

... but before that, I challenge you to name as many modules by heart as possible! 💬👇

Let's see who gets more!
To help get a sense for the more popular and used modules, leave a like ♥ on the tweets that contain modules you remember using.

Can you guess what module(s) are the most widely used..? 🤔

(P.S. To learn more about all these cool modules, check docs.python.org/3/library/)
Let's kick things off with

👉 Text Processing Services
🔗 docs.python.org/3/library/text…

1️⃣ string — Common string operations

2️⃣ re — Regular expression operations

3️⃣ difflib — Helpers for computing deltas

4️⃣ textwrap — Text wrapping and filling
5️⃣ unicodedata — Unicode Database

6️⃣ stringprep — Internet String Preparation

7️⃣ readline — GNU readline interface

8️⃣ rlcompleter — Completion function for GNU readline
I think that among these 8, `re` is the most commonly used.

Even though regular expressions can be tricky, they can be really helpful as well!

(By the way, you are probably missing out on `string`'s amazing built-in constants...)

👉 Binary Data Services
🔗 docs.python.org/3/library/bina…

(I'm not gonna lie, every now and then I trip up when working with binary data 😂)

9️⃣ struct — Interpret bytes as packed binary data

1️⃣0️⃣ codecs — Codec registry and base classes
👉 Data Types
🔗 docs.python.org/3/library/data…

1️⃣1️⃣ datetime — Basic date and time types

1️⃣2️⃣ zoneinfo — IANA time zone support

1️⃣3️⃣ calendar — General calendar-related functions
1️⃣4️⃣ collections — Container datatypes

1️⃣5️⃣ collections.abc — Abstract Base Classes for Containers

These two are absolutely amazing!
My suggestion: go take a look at what's in `collections`.

1️⃣6️⃣ heapq — Heap queue algorithm
1️⃣7️⃣ bisect — Array bisection algorithm
👆 I never used this one, but I'm dying to get an opportunity to use it!

1️⃣8️⃣ array — Efficient arrays of numeric values
👆 As far as my experience goes, people just go for NumPy.

1️⃣9️⃣ weakref — Weak references
2️⃣0️⃣ types — Dynamic type creation and names for built-in types

2️⃣1️⃣ copy — Shallow and deep copy operations
👆 Useful when working with mutable objects and you need to create copies.

2️⃣2️⃣ pprint — Data pretty printer

Use @willmcgugan 's `rich` instead! Worth the install.
2️⃣3️⃣ reprlib — Alternate repr() implementation

2️⃣4️⃣ enum — Support for enumerations

2️⃣5️⃣ graphlib — Functionality to operate with graph-like structures
👉 Numeric and Mathematical Modules
🔗 docs.python.org/3/library/nume…

2️⃣6️⃣ numbers — Numeric abstract base classes
👆 Kind of like `collections.abc`, but for numbers.

2️⃣7️⃣ math — Mathematical functions

2️⃣8️⃣ cmath — Mathematical functions for complex numbers
2️⃣9️⃣ decimal — Decimal fixed point and floating point arithmetic
👆 Looking to increase floating point accuracy for some computations? Take a look at this!

3️⃣0️⃣ fractions — Rational numbers
👆 Need arbitrary precision? Maybe this will help you.
3️⃣1️⃣ random — Generate pseudo-random numbers
👆 One of the modules I use the most, has many useful functions...


3️⃣2️⃣ statistics — Mathematical statistics functions
👉 Functional Programming Modules
🔗 docs.python.org/3/library/func…

I am so sad this section only has 3 modules.

The modules in here are absolute gold!

Want to level up your Python 🐍 game?

Learn about what's in here.
3️⃣3️⃣ itertools — Functions creating iterators for efficient looping

`for` loops and the like are the bread and butter of Python programming.

(And many other languages' programming.)

Write Pythonic loops (or replace them altogether) with the tools in here!
3️⃣4️⃣ functools — Higher-order functions and operations on callable objects

If you are working with functions as objects, e.g. working with decorators, then this module has many gold nuggets you'll appreciate!
3️⃣5️⃣ operator — Standard operators as functions

Have you ever found yourself creating tiny lambdas just to wrap a built-in operation? Something like

lambda x, y: x + y

Well, you can stop now. That's what the `operator` module is for!
👉 File and Directory Access
🔗 docs.python.org/3/library/file…

3️⃣6️⃣ pathlib — Object-oriented filesystem paths
👆 Modern alternative to work with paths.

3️⃣7️⃣ os.path — Common pathname manipulations

3️⃣8️⃣ fileinput — Iterate over lines from multiple input streams
3️⃣9️⃣ stat — Interpreting stat() results

4️⃣0️⃣ filecmp — File and Directory Comparisons

4️⃣1️⃣ tempfile — Generate temporary files and directories

4️⃣2️⃣ glob — Unix style pathname pattern expansion
👆 Some unix goodness regardless of your OS 😊
4️⃣3️⃣ fnmatch — Unix filename pattern matching

4️⃣4️⃣ linecache — Random access to text lines

4️⃣5️⃣ shutil — High-level file operations
👆 E.g. to copy files around.
👉 Data Persistence
🔗 docs.python.org/3/library/pers…

Modules to preserve data between script runs.

From pickling variables to a proper DB interface.

4️⃣6️⃣ pickle — Python object serialization

4️⃣7️⃣ copyreg — Register pickle support functions

4️⃣8️⃣ shelve — Python object persistence
4️⃣9️⃣ marshal — Internal Python object serialization

5️⃣0️⃣ dbm — Interfaces to Unix “databases”

5️⃣1️⃣ sqlite3 — DB-API 2.0 interface for SQLite databases
👉 Data Compression and Archiving
🔗 docs.python.org/3/library/arch…

I don't think I ever used any of these 😢

These probably come in handy if you need to do this in bulk/automatically... and in other situations.

Do you have experience with any of the following?
5️⃣2️⃣ zlib — Compression compatible with gzip

5️⃣3️⃣ gzip — Support for gzip files

5️⃣4️⃣ bz2 — Support for bzip2 compression

5️⃣5️⃣ lzma — Compression using the LZMA algorithm

5️⃣6️⃣ zipfile — Work with ZIP archives

5️⃣7️⃣ tarfile — Read and write tar archive files
👉 File Formats
🔗 docs.python.org/3/library/file…

5️⃣8️⃣ csv — CSV File Reading and Writing
👆 Anyone using `csv` files instead of proper data bases sometimes? I'm more of a `json` kind of guy.

5️⃣9️⃣ configparser — Configuration file parser

6️⃣0️⃣ netrc — netrc file processing
6️⃣1️⃣ xdrlib — Encode and decode XDR data

6️⃣2️⃣ plistlib — Generate and parse Apple .plist files
👉 Cryptographic Services
🔗 docs.python.org/3/library/cryp…

Cryptography is such a cool topic, isn't it?

6️⃣3️⃣ hashlib — Secure hashes and message digests

6️⃣4️⃣ hmac — Keyed-Hashing for Message Authentication

6️⃣5️⃣ secrets — Generate secure random numbers for managing secrets
👉 Generic Operating System Services
🔗 docs.python.org/3/library/allo…

6️⃣6️⃣ os — Miscellaneous operating system interfaces

6️⃣7️⃣ io — Core tools for working with streams

6️⃣8️⃣ time — Time access and conversions
👆 The layman's timing functionality!
6️⃣9️⃣ argparse — Parser for command-line options, arguments and sub-commands
👆Great to build command-line interfaces, this one came to replace `optparse` .

Recently I've seen many people use `click` for this type of thing 🤔

7️⃣0️⃣ getopt — C-style parser for command line options
7️⃣1️⃣ logging — Logging facility for Python

7️⃣2️⃣ logging.config — Logging configuration

7️⃣3️⃣ logging.handlers — Logging handlers

Probably all you need to do proper logging in your programs, instead of using `print`s all over the place!
7️⃣4️⃣ getpass — Portable password input

7️⃣5️⃣ curses — Terminal handling for character-cell displays

7️⃣6️⃣ curses.textpad — Text input widget for curses programs

7️⃣7️⃣ curses.ascii — Utilities for ASCII characters

7️⃣8️⃣ curses.panel — A panel stack extension for curses
From all the modules I never used, `curses` must be the one I've seen the most! I should take a look at it, no?

7️⃣9️⃣ platform — Access to underlying platform’s identifying data

8️⃣0️⃣ errno — Standard errno system symbols

8️⃣1️⃣ ctypes — A foreign function library for Python
👉 Concurrent Execution
🔗 docs.python.org/3/library/conc…

All that fun stuff about getting more work done!

One stop shop for concurrent execution, multithreading, multiprocessing, etc...

8️⃣2️⃣ threading — Thread-based parallelism
8️⃣3️⃣ multiprocessing — Process-based parallelism

8️⃣4️⃣ multiprocessing.shared_memory — Provides shared memory for direct access across processes

8️⃣5️⃣ The `concurrent` package
👆👇 Which currently only contains the `futures`:

8️⃣6️⃣ concurrent.futures — Launching parallel tasks
8️⃣7️⃣ subprocess — Subprocess management

8️⃣8️⃣ sched — Event scheduler

8️⃣9️⃣ queue — A synchronized queue class

9️⃣0️⃣ contextvars — Context Variables

9️⃣1️⃣ _thread — Low-level threading API
At this point I'm not even halfway through and I'm already humbled by the sheer amount of things I didn't even know existed, let alone know how to use...
👉 Networking and Interprocess Communication
🔗 docs.python.org/3/library/ipc.…

9️⃣2️⃣ asyncio — Asynchronous I/O
👆 Look ma', I know this one!

9️⃣3️⃣ socket — Low-level networking interface

9️⃣4️⃣ ssl — TLS/SSL wrapper for socket objects
9️⃣5️⃣ select — Waiting for I/O completion

9️⃣6️⃣ selectors — High-level I/O multiplexing

9️⃣7️⃣ asyncore — Asynchronous socket handler

No idea what these are 🤣
9️⃣8️⃣ asynchat — Asynchronous socket command/response handler

9️⃣9️⃣ signal — Set handlers for asynchronous events

1️⃣0️⃣0️⃣ mmap — Memory-mapped file support

Ditto 🤣
👉 Internet Data Handling
🔗 docs.python.org/3/library/netd…

1️⃣0️⃣1️⃣ email — An email and MIME handling package
👆 Who needs Gmail, right?

1️⃣0️⃣2️⃣ json — JSON encoder and decoder
👆 The layman's (human-readable!) database.

1️⃣0️⃣3️⃣ mailcap — Mailcap file handling
1️⃣0️⃣4️⃣ mailbox — Manipulate mailboxes in various formats

1️⃣0️⃣5️⃣ mimetypes — Map filenames to MIME types

1️⃣0️⃣6️⃣ base64 — Base16, Base32, Base64, Base85 Data Encodings
👆 I've found base 64 to be useful for encoding data over the internet.
1️⃣0️⃣7️⃣ binhex — Encode and decode binhex4 files

1️⃣0️⃣8️⃣ binascii — Convert between binary and ASCII

1️⃣0️⃣9️⃣ quopri — Encode and decode MIME quoted-printable data

1️⃣1️⃣0️⃣ uu — Encode and decode uuencode files
👉 Structured Markup Processing Tools
🔗 docs.python.org/3/library/mark…

1️⃣1️⃣1️⃣ html — HyperText Markup Language support

1️⃣1️⃣2️⃣ html.parser — Simple HTML and XHTML parser

1️⃣1️⃣3️⃣ html.entities — Definitions of HTML general entities

BeautifulSoup, a Python web scraper, leverages these.
1️⃣1️⃣4️⃣ XML Processing Modules

1️⃣1️⃣5️⃣ to 1️⃣2️⃣3️⃣ ― All sorts of XML-related facilities. They're just so many!

From working with the DOM, to parsers, to SAX-related stuff.
👉 Internet Protocols and Support
🔗 docs.python.org/3/library/inte…

1️⃣2️⃣4️⃣ webbrowser — Convenient Web-browser controller

1️⃣2️⃣5️⃣ cgi — Common Gateway Interface support

1️⃣2️⃣6️⃣ cgitb — Traceback manager for CGI scripts

1️⃣2️⃣7️⃣ wsgiref — WSGI Utilities and Reference Implementation
All URL-related stuff.

A very well-known external module that is used often is `requests`, take a look at these and also at the `requests` module!

1️⃣2️⃣8️⃣ urllib — URL handling modules

1️⃣2️⃣9️⃣ urllib.request — Extensible library for opening URLs
1️⃣3️⃣0️⃣ urllib.response — Response classes used by urllib

1️⃣3️⃣1️⃣ urllib.parse — Parse URLs into components

1️⃣3️⃣2️⃣ urllib.error — Exception classes raised by urllib.request

1️⃣3️⃣3️⃣ urllib.robotparser — Parser for robots.txt
Now come a series of internet protocols!

1️⃣3️⃣4️⃣ http — HTTP modules

1️⃣3️⃣5️⃣ http.client — HTTP protocol client

1️⃣3️⃣6️⃣ ftplib — FTP protocol client

1️⃣3️⃣7️⃣ poplib — POP3 protocol client
1️⃣3️⃣8️⃣ imaplib — IMAP4 protocol client

1️⃣3️⃣9️⃣ nntplib — NNTP protocol client

1️⃣4️⃣0️⃣ smtplib — SMTP protocol client

1️⃣4️⃣1️⃣ smtpd — SMTP Server
1️⃣4️⃣2️⃣ telnetlib — Telnet client

1️⃣4️⃣3️⃣ uuid — UUID objects according to RFC 4122
👆 UUIDs are those really long unique identifiers, e.g. a8098c1a-f86e-11da-bd1a-00112444be1e

1️⃣4️⃣4️⃣ socketserver — A framework for network servers
1️⃣4️⃣5️⃣ http.server — HTTP servers
👆 Yup, Python comes bundled with a basic http server! Did you know that?

1️⃣4️⃣6️⃣ http.cookies — HTTP state management

1️⃣4️⃣7️⃣ http.cookiejar — Cookie handling for HTTP clients
1️⃣4️⃣8️⃣ xmlrpc — XMLRPC server and client modules

1️⃣4️⃣9️⃣ xmlrpc.client — XML-RPC client access

1️⃣5️⃣0️⃣ xmlrpc.server — Basic XML-RPC servers

1️⃣5️⃣1️⃣ ipaddress — IPv4/IPv6 manipulation library
👉 Multimedia Services
🔗 docs.python.org/3/library/mm.h…

Did you know that Audacity, the audio editing program, is written in Python.

1️⃣5️⃣2️⃣ audioop — Manipulate raw audio data

1️⃣5️⃣3️⃣ aifc — Read and write AIFF and AIFC files

1️⃣5️⃣4️⃣ sunau — Read and write Sun AU files
1️⃣5️⃣5️⃣ wave — Read and write WAV files

1️⃣5️⃣6️⃣ chunk — Read IFF chunked data

1️⃣5️⃣7️⃣ colorsys — Conversions between color systems
👆 E.g. how would you convert an RGB value to HSL? The formulas can get annoying, so having this to import is great!
1️⃣5️⃣8️⃣ imghdr — Determine the type of an image

1️⃣5️⃣9️⃣ sndhdr — Determine type of sound file

1️⃣6️⃣0️⃣ ossaudiodev — Access to OSS-compatible audio devices
👉 Internationalization
🔗 docs.python.org/3/library/i18n…

1️⃣6️⃣1️⃣ gettext — Multilingual internationalization services

1️⃣6️⃣2️⃣ locale — Internationalization services

Making apps for multiple cultures/countries/regions? Start here!
👉 Program Frameworks
🔗 docs.python.org/3/library/fram…

1️⃣6️⃣3️⃣ turtle — Turtle graphics
👆 Starting to learn Python? Start here! Write some code and make some drawings!

1️⃣6️⃣4️⃣ cmd — Support for line-oriented command interpreters

1️⃣6️⃣5️⃣ shlex — Simple lexical analysis
👉 Graphical User Interfaces with Tk
🔗 docs.python.org/3/library/tk.h…

I'm not up to speed on all the modern alternatives to create GUIs, but Python ships with an alternative straight from the shelf.

1️⃣6️⃣6️⃣ tkinter — Python interface to Tcl/Tk
1️⃣6️⃣7️⃣ to 1️⃣7️⃣4️⃣ contains many widgets, themes, extensions, etc, all to work with Tkinter.

And IDLE! Have you heard about it?

IDLE is the basic IDE that comes with Python... And it's written entirely in Python!
👉 Development Tools
🔗 docs.python.org/3/library/deve…

1️⃣7️⃣5️⃣ typing — Support for type hints

A very polarising module, as far as my experience goes!
Some people love typing their code, others hate it!

Personally, I kinda like it! But Python's dynamic nature is great 😊
1️⃣7️⃣6️⃣ pydoc — Documentation generator and online help system

1️⃣7️⃣7️⃣ doctest — Test interactive Python examples

If you comment your code well, you can even automatically generate documentation and even test your code!
Now come some testing-related modules.

There are many non-built-in alternatives; what do you personally use?

1️⃣7️⃣8️⃣ unittest — Unit testing framework

1️⃣7️⃣9️⃣ unittest.mock — mock object library

1️⃣8️⃣0️⃣ unittest.mock — getting started
1️⃣8️⃣1️⃣ test — Regression tests package for Python

1️⃣8️⃣2️⃣ test.support — Utilities for the Python test suite

1️⃣8️⃣3️⃣ test.support.socket_helper — Utilities for socket tests
1️⃣8️⃣4️⃣ test.support.script_helper — Utilities for the Python execution tests

1️⃣8️⃣5️⃣ test.support.bytecode_helper — Support tools for testing correct bytecode generation
👉 Debugging and Profiling
🔗 docs.python.org/3/library/debu…

If you want to improve your Python 🐍 game, stop being like me: don't debug with `print` statements 😂

Learn to use some of these tools!

1️⃣8️⃣6️⃣ bdb — Debugger framework
1️⃣8️⃣7️⃣ faulthandler — Dump the Python traceback

1️⃣8️⃣8️⃣ pdb — The Python Debugger

1️⃣8️⃣9️⃣ timeit — Measure execution time of small code snippets

1️⃣9️⃣0️⃣ trace — Trace or track Python statement execution

1️⃣9️⃣1️⃣ tracemalloc — Trace memory allocations
👉 Software Packaging and Distribution
🔗 docs.python.org/3/library/dist…

1️⃣9️⃣2️⃣ distutils — Building and installing Python modules

1️⃣9️⃣3️⃣ ensurepip — Bootstrapping the pip installer

1️⃣9️⃣4️⃣ venv — Creation of virtual environments

1️⃣9️⃣5️⃣ zipapp — Manage executable Python zip archives
👉 Python Runtime Services
🔗 docs.python.org/3/library/pyth…

1️⃣9️⃣6️⃣ sys — System-specific parameters and functions

1️⃣9️⃣7️⃣ sysconfig — Provide access to Python’s configuration information

1️⃣9️⃣8️⃣ builtins — Built-in objects
👆 This gives you programmatic access to Python's built-ins.
1️⃣9️⃣9️⃣ __main__ — Top-level script environment
👆 If you had asked me, I would not have said that `__main__` counts as a module from the standard library...

But it's in the documentation, and if I remove it, it's going to break my emoji numbering 😂 So I'll leave it.
2️⃣0️⃣0️⃣ warnings — Warning control

2️⃣0️⃣1️⃣ dataclasses — Data Classes
👆 Want to define a class just as glue for some data? Look here!

2️⃣0️⃣2️⃣ contextlib — Utilities for with-statement contexts
2️⃣0️⃣3️⃣ abc — Abstract Base Classes
👆 If you want to implement some interface-like behaviour, this may be the answer!

2️⃣0️⃣4️⃣ atexit — Exit handlers
👆 Run custom Python code right before your program finishes/exits.
2️⃣0️⃣5️⃣ traceback — Print or retrieve a stack traceback

2️⃣0️⃣6️⃣ __future__ — Future statement definitions
👆 time-travelling right in your code!

2️⃣0️⃣7️⃣ gc — Garbage Collector interface

2️⃣0️⃣8️⃣ inspect — Inspect live objects

2️⃣0️⃣9️⃣ site — Site-specific configuration hook
👉 Custom Python Interpreters
🔗 docs.python.org/3/library/cust…

Wanna get crazy and write a Python REPL with additional functionality? 👇

2️⃣1️⃣0️⃣ code — Interpreter base classes

2️⃣1️⃣1️⃣ codeop — Compile Python code
👉 Importing Modules
🔗 docs.python.org/3/library/modu…

To import code dynamically from non-obvious places or to customise the import process, this is your go-to place.

2️⃣1️⃣2️⃣ zipimport — Import modules from Zip archives

2️⃣1️⃣3️⃣ pkgutil — Package extension utility
2️⃣1️⃣4️⃣ modulefinder — Find modules used by a script

2️⃣1️⃣5️⃣ runpy — Locating and executing Python modules

2️⃣1️⃣6️⃣ importlib — The implementation of import
👉 Python Language Services
🔗 docs.python.org/3/library/lang…

2️⃣1️⃣7️⃣ parser — Access Python parse trees

2️⃣1️⃣8️⃣ ast — Abstract Syntax Trees
👆 Great if you want to manipulate Python code. By using the abstract syntax tree, you can actually work on the *structure* of the code.
2️⃣1️⃣9️⃣ symtable — Access to the compiler’s symbol tables

2️⃣2️⃣0️⃣ symbol — Constants used with Python parse trees

2️⃣2️⃣1️⃣ token — Constants used with Python parse trees

2️⃣2️⃣2️⃣ keyword — Testing for Python keywords

2️⃣2️⃣3️⃣ tokenize — Tokenizer for Python source
2️⃣2️⃣4️⃣ tabnanny — Detection of ambiguous indentation
👆 This, right here, ladies and gentlemen, wins the prize for the module with the funniest name!

2️⃣2️⃣5️⃣ pyclbr — Python module browser support
👆 No idea what this is 😂
2️⃣2️⃣6️⃣ py_compile — Compile Python source files

2️⃣2️⃣7️⃣ compileall — Byte-compile Python libraries

2️⃣2️⃣8️⃣ dis — Disassembler for Python bytecode

2️⃣2️⃣9️⃣ pickletools — Tools for pickle developers
Are you still browsing this thread? Wow!

We are almost done, keep going.
👉 Miscellaneous Services
🔗 docs.python.org/3/library/misc…

2️⃣3️⃣0️⃣ formatter — Generic output formatting
👉 MS Windows Specific Services
🔗 docs.python.org/3/library/wind…

2️⃣3️⃣1️⃣ msilib — Read and write Microsoft Installer files

2️⃣3️⃣2️⃣ msvcrt — Useful routines from the MS VC++ runtime

2️⃣3️⃣3️⃣ winreg — Windows registry access

2️⃣3️⃣4️⃣ winsound — Sound-playing interface for Windows
👉 Unix Specific Services
🔗 docs.python.org/3/library/unix…

This gives access to facilities/utilities that are unique to (some) Unix systems.

2️⃣3️⃣5️⃣ posix — The most common POSIX system calls

2️⃣3️⃣6️⃣ pwd — The password database

2️⃣3️⃣7️⃣ spwd — The shadow password database
2️⃣3️⃣8️⃣ grp — The group database

2️⃣3️⃣9️⃣ crypt — Function to check Unix passwords

2️⃣4️⃣0️⃣ termios — POSIX style tty control

2️⃣4️⃣1️⃣ tty — Terminal control functions

2️⃣4️⃣2️⃣ pty — Pseudo-terminal utilities

2️⃣4️⃣3️⃣ fcntl — The fcntl and ioctl system calls
2️⃣4️⃣4️⃣ pipes — Interface to shell pipelines

2️⃣4️⃣5️⃣ resource — Resource usage information

2️⃣4️⃣6️⃣ nis — Interface to Sun’s NIS (Yellow Pages)

2️⃣4️⃣7️⃣ syslog — Unix syslog library routines
👉 Superseded Modules
🔗 docs.python.org/3/library/supe…

These modules are here for backward compatibility.

2️⃣4️⃣8️⃣ optparse — Parser for command line options
👆 Use `argparse` instead.

2️⃣4️⃣9️⃣ imp — Access the import internals
👆 Use `importlib` instead.
Then there's also a couple of platform-specific modules; you can read about them here:
🔗 docs.python.org/3/library/undo…

This is the end 🚧🚧 of the thread!

This was very high-level, you can find all the details in the documentation: docs.python.org/3/library/
Don't worry about me, most of this thread was generated programmatically, so I didn't type all of this by hand! 😅

Did you know all of these modules beforehand?

Of course not! 🤣

If you've read this far, say _something_ in the comments, just so that I don't feel too bad 😆

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Rodrigo 🐍📝

Rodrigo 🐍📝 Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @mathsppblog

17 Sep
🤔 Do you ever write some Python 🐍, look at it, and get the feeling there MUST be a better ✨ way to do that..?

I get that ALL the time! 😅

That's why I started researching and writing articles to help me (and you!) write better code.

Here's all 28 of them (so far!) 👇🧵
I have also been compiling all the articles into an eBook 📖, that I am "selling" for free 💸.

Why?

I decided to make it free because that's how I learned Python: from free books and articles, available online.

Here's the link to the free book:

gum.co/pydonts
01 - Pydon't disrespect the Zen of Python

If you type `import this` in a Python REPL, you will be presented with the Zen of Python, a document with some guidelines that you can follow when writing code.

It's not a religion!

Just some guidelines :)

mathspp.com/blog/pydonts/p…
Read 32 tweets
9 Sep
Have you ever had a hard time debugging 🐞 a Python 🐍 script?

If you say you haven't, you're lying! 😆

Let me show you something that you're gonna love me for!

Run your script with the `-i` flag!

By doing so, you get a REPL when the script finishes, EVEN with errors 💣

1/4
Let me show you an example.

Below is a piece of code I wrote in a script called `foo`.

If you look closely, you'll see I'm generating 100 random integers...

All of them between 0 and... 0! So I'll generate 100 zeroes (my bug).

Later, a 0/0 division will throw an error.

2/4
When I run the code with the `-i` flag, the terminal shows the error and immediately opens a REPL!

From within the REPL I can inspect my variables and figure out what went wrong!

Isn't this amazing ⁉

3/4
Read 5 tweets
8 Sep
Have you ever tried to optimise your Python 🐍 code, but weren't able to tell which alternative implementation was faster?

Let me teach you how to use the `timeit.timeit` method to measure execution time of small code snippets.

A thread 👇🧵
If you have a snippet of code you want to time, you can use the command line to measure its execution time.

For example, how long does it take for Python 🐍 to add up the first 10,000 integers?

Using the command `python -m timeit`, I get that it takes 189 microseconds.
The execution of `python -m timeit` reports some more info:

“2000 loops, best of 5: ...”

I'll explain what that means, but first let me show you how to use `timeit` in your code.

(Oh, btw, there's a MUCH better way to add consecutive integers! 👇)
Read 23 tweets
8 Sep
Did you know that Python 🐍 supports keyword argument unpacking?

What's that?

If you have a function that accepts a series of arguments: `arg1`, `arg2`, ...

And if you have a dictionary with keys named `arg1`, `arg2`, ...

Then you can use the dict's values as arguments: Image
The `**` in front of `kwargs` does the unpacking in the function call.

`my_func(kwargs)` – this calls `my_func` with a single argument, the `kwargs` variable (a dictionary in the image above)

...
`my_func(**kwargs)` – this calls `my_func` with one named argument for each (key, value) pair in the dictionary.

E.g., in the above,

`my_func(**kwargs)` unpacks to `my_func(a=73, b="hey!", c=(1, 10))`.
Read 4 tweets
7 Sep
@svpino Hey Santiago! I'd just like to clarify something for the readers of this thread:

The formula

P(A or B or C) = P(A) + P(B) + P(C) - P(A and B and C) is NOT ‼ true in general.

The result works here because the events are mutually exclusive and P(A and B and C) = 0

1/
@svpino But the actual general formula is

P(A or B or C) = P(A) + P(B) + P(C) - P(A and B) - P(B and C) - P(A and C) + P(A and B and C)

You can read up on the inclusion-exclusion principle, which explains this: 🔗 en.wikipedia.org/wiki/Inclusion…

This can also be explained visually:

2/
@svpino Checking the overlaps below 👇

The ellipsis (representing the events) can overlap all over the place, and we CANNOT overcount the overlaps!

So, we need to remove the pairwise overlaps:

A and B
A and C
B and C

But when we do that, we over-subtract the final intersection,

3/
Read 4 tweets
2 Sep
Have you ever seen anything like this in Python 🐍 loops??

I just saw this being used for the first time, ever, and I decided to figure out what the heck this was...

And so here goes the explanation 👇🧵

1/
Foreword:

Python has many amazing capabilities to deal with `for` loops, among which you can count the `zip` and `enumerate` built-ins.

This little thread assumes you are familiar with both.

Read these if you need:
🔗 mathspp.com/blog/pydonts/e…
🔗 mathspp.com/blog/pydonts/z…

2/
When using `zip` and `enumerate` in your `for` loops, something you do often is to unpack the values you are iterating over.

Unpacking, in and out of itself, is an amazing feature of Python, and you can read about it in the linked article if needed.

mathspp.com/blog/pydonts/d…

3/
Read 10 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(