Mike Driscoll Profile picture
Nov 7 β€’ 5 tweets β€’ 3 min read
#Python includes several different ways to sort things. One handy way to sort is to use the built-in `sorted()` function!

Let's learn about that today!

#python_builtins_by_driscollis

πŸ§΅πŸπŸ‘‡
The `sorted()` function takes in an iterable and a couple of optional arguments.

In this example, you pass in a Python list and sort it from smallest to largest.

Then you set the optional `reverse` argument to True and sort from largest to smallest! 🐍πŸ”₯
The `sorted()` function also takes an optional `key` parameter. This parameter is a function that is used to modify the sorting behavior.

In this example, you use `str.lower()` to sort the words of a sentence. It will compare all the words in lowercase while sorting.
This last example shows how to sort a list of tuples. You can use the optional `key` parameter to sort by different items within the tuple using a `lambda`

In this case, you sort by student age!

Note: This example comes from the Python docs: docs.python.org/3/howto/sortin…
Thanks for learning about how to use Python's `sorted()` function with me.

Follow me for more great tips on #Python!

β€’ β€’ β€’

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

Keep Current with Mike Driscoll

Mike Driscoll 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 @driscollis

Nov 4
Python dataclasses were added in 3.7. This module provides a decorator and functions for automatically adding generated special methods such as __init__() and __repr__() to user-defined classes.

The original definition can be found here:

python.org/dev/peps/pep-0…

πŸ§΅πŸπŸ‘‡
Here is a fairly simple example of creating a dataclass in #Python Image
If you need to add a #Python list or dictionary field to a dataclass, then you will need to import the `field()` function from the dataclass module

Then specify the appropriate `default_factory` to create the field Image
Read 4 tweets
Oct 27
#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
When creating a Windows executable with PyInstaller or py2exe, avoid using the single-file option.

If you use the single-file option, Windows Defender may think you are creating malware and delete it or flag it.

You can sign your executables to prevent this
Read 7 tweets
Oct 25
Python dataclasses were added in 3.7. This module provides a decorator and functions for automatically adding generated special methods such as __init__() and __repr__() to user-defined classes.

The original definition can be found here:

python.org/dev/peps/pep-0…

πŸ§΅πŸπŸ‘‡
Here is a fairly simple example of creating a dataclass in #Python Image
If you need to add a #Python list or dictionary field to a dataclass, then you will need to import the `field()` function from the dataclass module

Then specify the appropriate `default_factory` to create the field Image
Read 4 tweets
Oct 12
One of #Python's many built-in functions is called `id()`. You can use `id()` to return the β€œidentity” of an object.

The id is an integer which is guaranteed to be unique and constant for this object during its lifetime.

#python_builtins_by_driscollis

πŸ§΅πŸπŸ‘‡
CPython implementation detail for ids: This is the address of the object in memory.

Identity and equality are NOT the same in Python!

For example, the two lists in the following example equal each other (i.e. have the same contents), but their id is different!
In CPython, there is an optimization where the first 256 integers have the same identity

This can be proven with Python's `id()` function.

So in this case, the identity and the equality are the same
Read 5 tweets
Oct 11
Today we are going to talk about the basics of unit testing in #Python

πŸ§΅πŸπŸ‘‡
The Python programming language includes built-in packages for testing:

🐍 doctest
🐍 unittest
I already did a thread on Python's `doctest`:

Read 13 tweets
Sep 18
Did you know that #Python supports arrays? No, I'm not talking about Python lists. I'm talking about the array module! 🐍🀯

docs.python.org/3/library/arra…

πŸ§΅πŸ‘‡
When you create an array object in #Python, the first argument has to be one of the following strings (AKA typecode):

b, B, u, h, H, i, I, l, L, q, Q, f or d

The second argument is optional, but must be a list
Here is an example of using #Python's array module:
Read 4 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

Don't want to be a Premium member but still want to support us?

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

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(