Bob Belderbos | @bbelderbos@fosstodon.org Profile picture
Apr 14, 2022 โ€ข 22 tweets โ€ข 6 min read โ€ข Read on X
20 cool things you can do with #Python's built-in functions ๐Ÿ๐Ÿงต
1. Make a dictionary from two lists:
2. Get a counter when looping through a sequence:
3. Check if a condition holds true for any or all items in a sequence:
4. Print things:
5. Summing values:
6. Uniquify a list:
7. Reverse a list:
8. Make a range of integers with a step:
9. Get a quotient and remainder, for example to divide seconds into minutes + seconds:
10. Name and apply a slice:
11. Use dir() together with a list comprehension to only list "public" methods (and attributes) of an object:
12. Get the min and max of a sequence:
13. Get user input:
14. Check for, get and set attributes:
15. Call a method on the parent class:
16. Use next() to retrieve the next value from a generator:
17. Make values absolute:
18. Sort values, by key, reversed, and in-place:
19. Round numbers:
20. See the representation / string of an object:
Python docs:
docs.python.org/3/library/funcโ€ฆ

@PyBites YouTube training:

โ€ข โ€ข โ€ข

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

Keep Current with Bob Belderbos | @bbelderbos@fosstodon.org

Bob Belderbos | @bbelderbos@fosstodon.org 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 @bbelderbos

Aug 24, 2023
๐Ÿš€ 10 useful things you can do more easily with pathlib than os in #Python ...

Check out this thread to see how to write more readable, simple and beautiful code with `pathlib` ...
1๏ธโƒฃ Creating/Joining Paths:

os:
p = os.path.join('folder', 'file.txt')

pathlib:
p = Path('folder') / "file.txt"
2๏ธโƒฃ Getting Home Dir:

os:
home = os.path.expanduser("~")

pathlib:
home = Path.home()
Read 13 tweets
Aug 24, 2023
Customizing Class Creation with __init_subclass__ ๐Ÿ’ก

In #Python, the __init_subclass__() method is a powerful and lesser-known tool for customizing class creation behavior in subclasses.

Example: class Plugin:     plugins = []      def __init_subclass__(cls, **kwargs):         super().__init_subclass__(**kwargs)         cls.plugins.append(cls)  class ConcretePlugin(Plugin):     pass  class AnotherPlugin(Plugin):     pass  print(Plugin.plugins)   # Output: [<class '__main__.ConcretePlugin'>, <class '__main__.AnotherPlugin'>]
In the provided code:

๐Ÿ“˜ Plugin is a base class.

๐Ÿš€ Whenever a subclass of Plugin is defined, __init_subclass__ of Plugin is automatically called.

๐Ÿ” Inside the __init_subclass__ method, the newly defined subclass (cls in this context) is appended to the plugins list.
โœ… So, after defining the ConcretePlugin and AnotherPlugin subclasses, the plugins list contains references to these two classes.

This technique can be useful to enforce certain constraints, add class-level attributes, or perform any setup tasks ๐Ÿ’ก ๐Ÿ’ช
Read 5 tweets
Apr 13, 2022
When you build up a #Python string use a list over string concatenation (+=).

See stackoverflow.com/a/3055541:
> Strings are immutable and can't be changed in place. To alter one, a new representation needs to be created.

So that happens repeatedly here = slower.
Btw instead of loop + append, I could also have used a list comprehension inside the .join()
Also this probably only becomes a problem for bigger data. I post it here for awareness, but writing readable / maintainable code should be your first focus.
Read 4 tweets
Dec 1, 2021
Don't get stuck in tutorial paralysis, the best way to learn #Python and software development is to build real world applications ๐Ÿงต
You will hit so many issues, you'll have to constantly look things up, Stack Overflow becomes your new best friend and that is ok. It will turn you into a good developer.
Because it's not about memorizing things (even though you still will with increased practice), it's about becoming a good problem solver with a spark of creativity.
Read 14 tweets
Aug 29, 2020
5 tips that will improve the design and quality of your #software:

1. Write shorter functions (methods) that do just one thing ("single responsibility principle").

2. Decrease the number of decision points (aka "cyclomatic complexity") per unit (function, method, etc).
...
...
3. Avoid duplication at all cost!

4. Keep your interfaces small (e.g. the number of arguments a function or class receives).

5. Separate concerns by making your code more modular.
What have all these tips in common?

They lead to more robust and moldable software:

1. Components become more loosely coupled and are therefor easier to reuse, test and extend (support new use cases).
...
Read 5 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!

:(