Take your Python ๐ skills to the next level ๐!
6 subscribed
May 18, 2023 โข 11 tweets โข 4 min read
I know `print` is the first Python ๐ย function you learned! ๐
And yet, you don't know this about `print` ๐
What you know for sure is that `print` will take an object and it will print it on the screen.
That's the basic functionality it provides:
May 17, 2023 โข 10 tweets โข 4 min read
I'll tell you the story of a person that had the wrong nameโฆ
And how to prevent that in Python ๐ย with properties ๐.
๐
John Doe was a regular guy and when he was born, he was inserted into the government's database of people.
They created a new `Person` and added John's details:
May 14, 2023 โข 6 tweets โข 2 min read
Opening a file to read/write is a common task in Python ๐.
Here is how to do it right! ๐
๐
Python has a built-in `open` that takes a file path and opens that file.
Then, you have to specify whether you want to open the file to read, write, or append.
But this isn't half of the story!
May 13, 2023 โข 6 tweets โข 2 min read
The Python ๐ย built-in `round` is great. ๐
Here are some tips on it. ๐
The purpose of `round` is toโฆ round numbers!
It rounds numbers to the closest integer.
These are some simple examples:
May 12, 2023 โข 7 tweets โข 3 min read
Error handling in Python ๐ย made simple. ๐
๐
The keyword `try` is used before code that might fail.
So, if you know something can raise an error, you can write it inside a `try` statement:
May 11, 2023 โข 13 tweets โข 5 min read
Every Python ๐ย programmer uses the ICPO rule. ๐
Even if you don't know what it is.
Let me tell you what the ICPO rule is and what it does ๐
The ICPO rule is what determines how attribute and method lookup work.
More precisely, it is the ICPO rule that tells Python where to look for attributes and methods in your objects.
This is relevant because everything in Python is an object.
May 10, 2023 โข 10 tweets โข 4 min read
If you write Python ๐ย you need to know about `enumerate`.
It is one of those tools you will use every day. ๐
Let me show you how it works ๐
The point of `enumerate` is to give you access to indices and elements while you are iterating over something.
Thus, `enumerate` is commonly used inside a `for` loop:
May 9, 2023 โข 16 tweets โข 6 min read
Have you mastered Python's ๐ย `zip` built-in?
It is one of the many powerful tools that Python provides us with to supercharge our `for` loops! ๐
Let me teach you all about it ๐
If I could only tell you one thing about `zip` is that it's useful when you need to put two sequences together.
Or even three.
Or more!
But usually, you'll use it with just two.
Apr 28, 2023 โข 6 tweets โข 2 min read
I've been doing Python ๐ย for +11 years. ๐
Here's a roadmap to learn 6 major topics you can't miss:
๐ Logic (Booleans, operations, De Morgan's laws)
๐ Graphs
๐ Data structures & algorithms
๐ Number bases and modular arithmetic
๐ Divide & conquer strategies
๐ Greedy strategies
These topics give you a very solid foundation of programming.
Apr 27, 2023 โข 9 tweets โข 4 min read
The Python ๐ย module datetime is your best friend if you want to work with dates and times. ๐
Here are some of the cool things it does:
โฐย My first tip has to do with how to import the module.
The module is called `datetime`.
Inside it, there are names like `date`, `time`, and `datetime`.
Most people think you don't need maths to write Python ๐ code.
They are wrong.
Here is an 1880's tool that will help you write code like never before: ๐
This thread uses ideas and examples from the course Logic II by @brilliantorg.
It's such a USEFUL course for programmers because you need logic to write code. ๐ง
I'll show you the connection. ๐
Tell me, have you ever written an if statement with 3+ conditions?
Apr 19, 2023 โข 7 tweets โข 3 min read
If you write Python ๐ย decorators you need to know about `functools.wraps`! ๐
If you don't use it, you are doing decorators wrong! ๐คท
๐ย `functools.wraps` is a decorator that you should always use when you write other decorators.
Learning about it will make you 10x more productive*! ๐
If you want to work with files, you need to know about it!
Let me show 5 of the most useful tools in this module ๐
(*Actual numbers may vary. ๐คก)
When using the module `pathlib`, most of the time you will want to use the class `Path`.
That class will let you work with files, paths, and directories, in your operating system.
So, let us get started!
Apr 3, 2023 โข 10 tweets โข 3 min read
How do you time your Python ๐ย code?
Many say that Python is slowโฆ ๐
But we'll use the module timeit to show Python can be quite fast! ๐
The module timeit provides a function timeit that you can use to time pieces of code.
The simplest use case is to determine what piece of code is faster among a couple of alternatives.
For example, from the 3 options below, which one do you think will be faster?
But you don't need advanced maths for them to make sense.
Read this. ๐
You'll wish you had done it sooner. ๐
This whole thread was inspired by a course on @brilliantorg, a platform that teaches from first principles.
They break down concepts into interactive, bite-sized lessons (like the dots above).
But now, I want to show you how to work with number bases in a very intuitive way!
Mar 27, 2023 โข 8 tweets โข 3 min read
Generators are a really cool Python ๐ย tool.
Let me show you why you should bother learning them! ๐
A short thread: ๐
I have a CSV file that lists all โpurchasesโ of my free book โPydon'tsโ.
I want to go over that data to find the people who paid money for the book.
Then, I want to do something with that data.
I could write the code below ๐
But what's an issue with it?
Mar 17, 2023 โข 22 tweets โข 7 min read
Python ๐ย decorators are often considered an โadvancedโ topic.
But they don't need to be hard.
Let me explain ๐ย how decorators are an elegant and powerful tool. ๐
The first thing you need to understand is WHY decorators matter.
A decorator is a tool that you use to add a feature to another function.
For example, you can add caching to a function with a caching decorator.
But the, why not implement that in the function itself?
Mar 8, 2023 โข 8 tweets โข 4 min read
As a Python ๐ย programmer, I spend my day inside my IDE.
After all, it's my job!
Here are 6 VS Code @code shortcuts that save me HOURS every week. ๐
A thread with video examples. ๐ @code Before I start, I will show the key combinations for VS Code, but these actions are common and your IDE is likely to have a similar shortcut.
Let's jump right into this!
Mar 6, 2023 โข 15 tweets โข 3 min read
Every. Single. Programmer. Needs to know how to debug. ๐ซ๐
The problem is that debugging is kind of an art.
Here is a messy thread with tips to become a debugging artist. ๐
First thing I do when I find a bug is read the error message.
Sounds obvious?
Well, maybe. ๐คท
I read the error message (instead of freaking out) and open the file where the error is.
Mar 4, 2023 โข 6 tweets โข 3 min read
Python ๐ย developers can use `property` to supercharge their classes. ๐
With `property`, you can write attributes that are computed dynamically.
Properties can also be updated dynamically.
Let me show you how in this thread. ๐
The simple example we are going to use is that of a square.
To create a square, we specify its size.
Then, we define the area of the square as a property.
Mar 2, 2023 โข 7 tweets โข 4 min read
Can you create a read-only attribute in Python ๐?
Yes, you can! ๐
Well, only sort of.
I will show you how to cheat. ๐
Sometimes you want to provide some attributes in a class that shouldn't be modified.
Some languages control this by having:
๐ย getters; and
๐ย setters.
But not Python!
In Python, you cannot prevent the user from doing `john.name = "Charles"`...