Are you a master of Python 🐍 sequence slicing?

Here is a short thread 🧡 covering four idiomatic slicing patterns that you could get used to.

These four patterns are great because they have a clear interpretation of what they do.

Here we go πŸ‘‡πŸ§΅
🐍 s[n:]

If `n β‰₯ 0`, then `s[n:]` means

πŸ‘‰ β€œSkip the first `n` elements.”

⚠ If `n` is 0, then we skip 0 elements, which is another way of saying we get all of them.
That's the same as `s[:]`.
🐍 s[-n:]

If `n > 0`, then `s[-n:]` means

πŸ‘‰ β€œThe last `n` elements.”

⚠ -0 and 0 are the same, so `s[-0:]` gets all the sequence, like above.
🐍 s[:n]

If `n β‰₯ 0`, then `s[:n]` means

πŸ‘‰ β€œThe first `n` elements.”

⚠ If `n` is 0, the last 0 elements means no elements at all, and we get an empty sequence.
🐍 s[:-n]

If `n > 0`, then `s[:-n]` means

πŸ‘‰ β€œDrop the last `n` elements.”

⚠ Again, careful with 0 and -0! s[:-0] returns an empty sequence, because of the case above.
Here are some β€œexercises” to make sure you understood how these sequence slicing patterns really work:
⚠ There's an interesting caveat to slicing that you should be aware of.

Slicing works, even if the indices used are too large for the sequence in question.

For example, `s[10:]` means β€œskip the first 10 elements”.

If `s` has only 5 elements, then we get an empty sequence:
⚠⚠ The β€œcaveat of the caveat” is that this does NOT change the interpretation of the slicing patterns we just saw.

If I have 5 elements and ignore the first 10, of course there is nothing left to look at.

It's just important to note that slicing does not do any length checks.
There is much more to sequence slicing than this short thread covered!

Do you want to learn even more? πŸ’ͺπŸ₯‡

If you want to learn even more, check out the article from which I drew this content:

mathspp.com/blog/pydonts/i…
I hope this thread was useful!

If it was, follow me (@mathsppblog) for more content like this and retweet the first thread so that everyone can learn proper slicing patterns!

Got any thoughts? πŸ‘‡πŸ’¬

See you around πŸ‘‹

β€’ β€’ β€’

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

18 Sep
Are you as curious (and nerdy πŸ€“) as I am? πŸ€”

If you are, you are going to love what I'm about to show you 🀯

Let's build a Python 🐍 quine: a program that prints itself!

This thread walks you through the process of building the quine you can see here.

πŸ‘‡πŸ§΅
If we want a program that _prints_ itself, we need to start with a print statement:

```
print()
```

When we run this program, a single newline gets printed as output.

But we want a program that prints itself, right?

So let's try putting the program in the `print`.
For that, we try opening " inside the `print` and we start typing the program, itself, inside the string:

```
print("print()")
```

This outputs

```
print()
```

But now our program grew, so we need to keep up with it inside the string.
Read 11 tweets
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

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!

:(