Mike Driscoll Profile picture
I tweet about everything #Python Writing about Python @mousevspython @realpython Teaching at @TeachMePy Author of multiple books - https://t.co/MdP25zw5zQ
6 subscribers
Feb 1 β€’ 8 tweets β€’ 3 min read
Did you know that #Python supports SQLite in the standard library? That's right. You don't need to install anything! πŸπŸš€

Let's talk a little about that!

πŸ§΅πŸ‘‡ If you have a pre-existing SQLite database file, you can use #Python's `sqlite3` module to connect to it

Here's how: Image
Nov 21, 2023 β€’ 9 tweets β€’ 3 min read
Today is a great day to talk about #Python lambdas!

Let's learn about them in a thread

πŸπŸ§΅πŸ‘‡ A Python lambda is a one-line anonymous function.

Rule #1 is do NOT assign a lambda to a variable.

When you do that, it's no longer an anonymous function. You should just create a regular function at that point Image
Mar 23, 2023 β€’ 4 tweets β€’ 2 min read
Want to create a copy of a #Python list? Use Python's `copy()` method!

Note: Watch out if your list contains lists of dictionaries. In those cases, you might be better off using copy.deepcopy() But be careful! If your list contains a mutable object, like another list or a dictionary, you may encounter some unexpected behavior.

In the following example, you `copy()` the list. Then you modify the nested dictionary in the copy, but that also changes the original list!
Mar 22, 2023 β€’ 10 tweets β€’ 5 min read
#Python generators aren't talked about enough.

So let's talk about them today!

πŸ§΅πŸπŸ‘‡ According to the #Python wiki, "Generator functions allow you to declare a function that behaves like an iterator"

One of their best use cases is to chunk through large data sets a piece at a time, which prevents you from running out of memory.

wiki.python.org/moin/Generators
Mar 21, 2023 β€’ 4 tweets β€’ 2 min read
My 11th #Python book is now available! 🐍🀯

Test your Python skills and knowledge with over 100 PYthon quizzes. Answers are included if you get stuck!

Available on Amazon, Leanpub, and Gumroad (see following tweets) Get my 11th #Python book, The Python Quiz book on Amazon in the following formats:

🐍 Kindle
🐍 Paperback

amazon.com/dp/B0BVT8DZV1
Mar 17, 2023 β€’ 6 tweets β€’ 2 min read
Starting in Python 3.7, the `breakpoint()` built-in function was added

It is defined in PEP 553 and simplifies adding a breakpoint to your code

#python_builtins_by_driscollis

πŸ§΅πŸπŸ‘‡ You can read about the `breakpoint()` function for Python here:

python.org/dev/peps/pep-0…
Mar 16, 2023 β€’ 7 tweets β€’ 2 min read
#Python has a TON of different ways to create executables!

Here are some of the packages you can use:

🐍 PyInstaller
🐍 Py2Exe
🐍 cx_freeze
🐍 Briefcase
🐍 Nuitka
🐍 py2app

πŸ§΅πŸπŸ‘‡ If you want to create a Mac executable, you can use any of these:

🐍 PyInstaller
🐍 Briefcase
🐍 py2app
🐍 Nuitka
Mar 11, 2023 β€’ 9 tweets β€’ 4 min read
Did you know #Python has a regular expressions module in its standard library? It is called `re`

Let's talk a little about it today!

πŸ§΅πŸπŸ‘‡ Regular expressions or RegEx is a tiny language that is used primarily for matching strings or substrings.

Here is an example in #Python where you use RegEx to search for a string that starts with "a" and ends in "f". The middle letters must be "b-f"
Mar 10, 2023 β€’ 6 tweets β€’ 3 min read
Today we are going to talk about environment variables with #Python!

This will be a thread, so prepare yourself!

πŸ§΅πŸπŸ‘‡ Environment variables are variables that are available to all your programs and saved in memory by your operating system.

You can get a listing of your environment variables using #Python's `os.environ` attribute:

🐍πŸ”₯ Image
Feb 8, 2023 β€’ 17 tweets β€’ 6 min read
Logging in #Python is a popular topic and it's a really useful one to understand.

Today we are going to learn more about that!

πŸ§΅πŸπŸ‘‡ Python includes the `logging` module as a part of its standard library.

You can get started using it quite easily:
Feb 6, 2023 β€’ 5 tweets β€’ 2 min read
If you are a Windows user, #Python has a few special libraries just for you.

Once such module is `winsound`

Let's learn more in this mini-thread!

πŸ§΅πŸπŸ‘‡ #Python's `winsound` module is a sound-playing interface for Windows.

Here's a link to the documentation:

docs.python.org/3/library/wins…
Feb 6, 2023 β€’ 5 tweets β€’ 3 min read
You can sort #Python lists in a couple of different ways. Find out how in this thread!

πŸ§΅πŸ‘‡ #Python lists come with a `sort()` method that makes sorting easy! Image
Jan 20, 2023 β€’ 15 tweets β€’ 7 min read
Today is a great day to learn about exception handling in #Python!

Let's find out how they work!

πŸ§΅πŸπŸ‘‡ #Python handles exceptions using the `try/except` keywords.

You can optionally add `finally` and `else` to the mix too!

Here's an example that uses all of them:
Jan 20, 2023 β€’ 4 tweets β€’ 2 min read
You can use Pycryptodome to encrypt a file and decrypt it.

In this example, you use RSA encryption to encrypt a file with #Python Now let's decrypt the file using Pycrytodome! 🐍πŸ”₯

Here's how to do it:
Jan 13, 2023 β€’ 16 tweets β€’ 7 min read
Today I am going to show you how to create 7️⃣ different file types with #Python:

🐍 Text file
🐍 CSV
🐍 XML
🐍 JSON
🐍 Tar file
🐍 PDF
🐍 Excel

Join me for this fun πŸ§΅πŸ‘‡ 1️⃣ Creating a text file with #Python is a breeze! The recommended method is to use Python's `with` statement as it will automatically close the file for you

That means you can create a text file with TWO lines of code if you want to:
Jan 11, 2023 β€’ 10 tweets β€’ 5 min read
#Python 3 has had asynchronous capabilities since 3.4. So let's talk about that today!

πŸ§΅πŸπŸ‘‡ Your first stop when learning about async in #Python is the official documentation.

There you will learn about the following:

🐍 The `asyncio` module
🐍 The `async` and `await` keywords

docs.python.org/3/library/asyn…
Dec 23, 2022 β€’ 10 tweets β€’ 5 min read
#Python has TONS of great packages and frameworks.

In this thread, I am going to highlight just a few of the THOUSANDS that are available to you on pypi.org

πŸπŸ§΅πŸ‘‡ Python has lots of great cross-platform GUI packages:

🐍 PySimpleGUI @PySimpleGUI
🐍 PyQt / PySide
🐍 wxPython
🐍 Tkinter
🐍 @DearPyGui
🐍 EasyGUI
Dec 21, 2022 β€’ 5 tweets β€’ 2 min read
Do you ever need to redirect stdout with #Python?

In this mini-thread, I will show you 3️⃣ different methods to redirect stdout!

πŸ§΅πŸπŸ‘‡ 1️⃣ Re-assign `sys.stdout` to a file handle

In this example, you open a file and reassign `sys.stdout` to that file handle.

Now when you call the `print()` function, it writes to a file instead of stdout!

Don't forget to revert your changes at the end!
Dec 20, 2022 β€’ 8 tweets β€’ 3 min read
#Python has had the concept of context managers for a loooong time!

Let's talk about context managers again!

πŸ§΅πŸπŸ‘‡ The `with` statement, which is the normal way for working with context managers, was added back in Python 2.5!

Here is a pretty common example of using a context manager:
Dec 10, 2022 β€’ 7 tweets β€’ 3 min read
Today I want to talk about #Python's special escape characters

Join me in another fun thread!

πŸ§΅πŸπŸ‘‡ Here is a list of the most common escape characters in #Python

🐍 \n - ASCII Linefeed or Newline
🐍 \t - Horizontal tab
🐍 \r - Carriage return
🐍 \b - Backspace
🐍 \f - Form feed
🐍 \' - Single Quote
🐍 \" - double quote
🐍 \\ -Backslash
🐍 \v -vertical tab
Dec 5, 2022 β€’ 8 tweets β€’ 4 min read
Let's talk about #Python introspection!

Introspection is the examination of Python objects. It gives you a window into your code that lets you learn about Python using Python itself!

Find out how in this mini-thread!

πŸ§΅πŸ‘‡πŸ There are multiple ways to introspect #Python code. You will learn about the following tools you can use for introspection:

🐍 type()
🐍 dir()
🐍 help()
🐍 sys