I was telling a friend that one cool feature from Python is list slice notation

So instead of just posting the link I decided to do a brief explanation.

[5 min]

Python's regular array indexing as in multiple languages is: a[index]

a = [0, 1, 2, 3, 4]
a[0] == 0

1/12🧵
Python has negative indexing too:

a = [0, 1, 2, 3, 4]
a[-1] == 4
a[-2] == 3

2/12🧵
Python also enables creating sub-lists from a list, or a slice:

-> a[start_index:last_index]

a = [0, 1, 2, 3, 4]
a[1:3] == [1, 2]

start_index is inclusive
last_index is exclusive

3/12🧵
→ The results of a slice are a copy not a reference!

a = [0, 1, 2, 3, 4]
b = a[1:3] # b == [1, 2]
b[0] = 10
a == [0, 1, 2, 3, 4]
b == [10, 2]

4/12🧵
Slicing also works with negative index:

a = [0, 1, 2, 3, 4]
a[1:-2] == [1, 2]

5/12🧵
You can (and you SHOULD) omit the first item if it's a zero, or the last if it's the length:

a = [0, 1, 2, 3, 4]
a[:3] == a[0:3] == [0, 1, 2]
‍a[3:] == a[3:len(a)] == [3, 4]
a[:] == a

6/12🧵
Python Slicing has one last optional parameter that is the step:

-> a[start_index:last_index:step]

a = [0, 1, 2, 3, 4]
a[1:4:2] == [1, 3]
a[::2] == [0, 2, 4]

7/12🧵
The stride can be a negative number, and this can be used to invert a list:

a = [0, 1, 2, 3, 4]
a[::-1] == [4, 3, 2, 1, 0]

Tip 💡: avoid using stride and slicing in the same expression, it can be confusing. Prefer breaking in two expressions

8/12🧵
Python Slicing works on lists, strings, bytes and any class that implements __getitem__ and __setitem__

a = "file.zip"
a[1:4] == "ile"
a[:-4] == "file"
a[::-1] == "piz.elif"

9/12🧵
Python Slicing is forgiving for start and end indexes! 👍

a = [0, 1, 2, 3, 4]
a[20:] == []
a[:20] == [0, 1, 2, 3, 4]

10/12🧵
Do you want to learn these concepts? Open your python interpreter or go to Colab and try/experiment with all these lines! (that's why I didn't put images of code this time) 🧠💪

It might take 20 minutes but I guarantee that's well worth your time!

11/12🧵
Slicing is a very important concept in Python and understanding it will make you a better developer and make it easier to understand other people's code!

Do you have any cool tips related to slicing?
Which other languages also have slicing?

12/12🧵

• • •

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

Keep Current with Luiz GUStavo 💉💉⏳

Luiz GUStavo 💉💉⏳ 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 @gusthema

9 Jun
Quick⚡️ Python🐍 trick:
How do you merge two dictionaries?

[55 sec]🤯

1/6🧵
For Python 3.5 and up you can do:

d1 = {"a":1, "b":2}
d2 = {"c":3, "d":4}
d3 = {**d1, **d2}
d3 == {"a": 1, "b": 2, "c":3, "d":4}

Why?

2/6🧵
The ** operator expands the collection, so, you can think it as this:

d1 = {"a":1, "b":2}
d2 = {"c":3, "d":4}
d3 = {**d1, **d2} -> {"a":1, "b":2, "c":3, "d":4}
d3 == {"a": 1, "b": 2, "c":3, "d":4}

3/6🧵
Read 6 tweets
30 May
I've been trying the new TensorFlow Decision Forest (TF-DF) library today and it's very good!

Not only the ease of use but also all the available metadata, documentation and integrations you get!

Let me show you some of the cool things I've learned so far...

[5 min]

1/11🧵
TensorFlow Decision Forests have implemented 3 algorithms:

• CART
• Random Forest
• Gradient Boosted Trees

You can get this list with tfdf.keras.get_all_models()

All of them enable Classification, Regression and Ranking Tasks

2/11🧵
CART or Classification and Regression Trees is a simple decision tree. 🌳

The process divides the dataset in two parts
The first is used to grow the tree while the second is used to prune the tree

This is a good basic algorithm to learn and understand Decision Trees

3/11🧵
Read 11 tweets
29 May
Machine learning goes beyond Deep Learning and Neural Networks

Sometimes a simpler technique might give you better results and be easier to understand

A very versatile algorithm is the Decision Forest
🌴🌲🌳?

What is it and how does it work?
Let me tell you..

[7 min]

1/10🧵
Before understanding a Forest, let's start by what's a Tree

Imagine you have a table of data of characteristics of Felines. With features like size, weight, color, habitat and a column with the labels like lion, tiger, house cat, lynx and so on.

2/10🧵
With some time, you could write a code based on if/else statements that could, for each a row in the table, decide which feline it is

This is exactly what a Decision Tree does
During its training it creates the if/elses

en.wikipedia.org/wiki/Decision_…

3/10🧵
Read 10 tweets
28 May
#GoogleIO2021 was last week and it's a lot of content to read/watch!

I'll post summaries of all the ML talks and help you be up to date

Let's start with my talk with @kempy about how #TFHub and how people are using it on the real world



[5 min]

1/7🧵
Having access to high quality models enables developers to solve their problems!

This makes ML more accessible to everyone and helps the society with it, like:

• Defend forests from illegal hunting
• Studying & protecting whales
• Blocking spam
• Helping farmers

2/7🧵
TensorFlow Hub has models for all the ML domains such as Image, Text, Audio and Video

For image, this tutorial can get you started with Transfer Learning. It does some cool tricks with the data and the final model is ready for on-device deployment

tensorflow.org/hub/tutorials/…

3/7🧵
Read 7 tweets
18 May
Today it's my 6 year Google anniversary!

I started as an android developer 📱🤖 and today I'm an ML developer 🧠🤖

I learned a lot of technical stuff (A LOT!), but there's much more than that.

Let me tell you some of the things I learned...

1/11🧵
Being the big fish 🐳 on a small pound is cool, but you may run out of air.

Starting at this job meant becoming a tiny fish 🐠 in a big pound!

And here is where the growth opportunities are!

2/11🧵
Working with smarter people than you is sometimes very hard for your ego!
There's a lot of impostor syndrome in the beginning.

But that's a big chance to learn what these smarter people do that you can adapt and grow as bigger fish 🦈.

3/11🧵
Read 12 tweets
6 Apr
Sometimes you need to build a Machine Learning model that cannot be expressed with the Sequential API

For these moments, when you need a more complex model, with multiple inputs and outputs or with residual connections, that's when you need the Functional API!

[2.46 min]

1/8🧵
The Functional API is more flexible than the Sequential API.

The easiest way to understand is to visualize the same model created using the Sequential and Functional API

2/8🧵
You can think of the Functional API as a way to create a Directed Acyclic Graph (DAG) of layers while the Sequential API can only create a stack of layers.

Functional is also known as Symbolic or Declarative API

3/8🧵
Read 9 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!

:(