Python slicing out of bounds never crashes

Indexing data[100] on a 3-element list crashes with IndexError, but slicing data[100:] returns an empty list with no error. Python silently clamps slice indices to the valid range. You can slice with absurdly large numbers and Python just gives you what it can.

#pythonweirdestlogic #pythonweirdbehavior #pythongotcha #pythontrick #pythonmindblown #pythonunexpected #pythonwtf #pythonhiddenfeature #pythonco...

https://www.youtube.com/watch?v=FgPkpyahr4k

Python slicing out of bounds never crashes #pythonlistbounds

YouTube

This Python function ignores try and except and always returns finally

When try, except, and finally all have return statements, finally always wins. Python guarantees finally blocks execute no matter what. The return in finally overwrites whatever try was about to return. Three return statements, only one actually returns.

#pythonweirdestlogic #pythonweirdbehavior #pythongotcha #pythontrick #pythonmindblown #pythonunexpected #pythonwtf #pythonhi...

https://www.youtube.com/watch?v=MDOqaOzK940

This Python function ignores try and except and always returns finally #pythongotcha

YouTube

Important PSA For @jack !!!

`os.truncate(path, length)` truncates THE FILE, not the path string 😳 (with no warning)

Thank goodness you were in a venv!

😅

#Python #PythonGotcha #PythonGotchas #Oops #OhNo

#PythonGotcha 🧵

It is often useful to make a #copy of a given list before performing operations that would mutate the elements.

When you make a shallow copy of an existing list, you create a new pointer to a new list object that points to the same old elements. (It saves memory.)

On the other hand, if you make a deep copy, then you create a completely new copy of the original list.

In other words: https://realpython.com/python-mutable-vs-immutable-types/#making-copies-of-lists

#learning #objects #python #dev #CS #DataScience #memory #dataDev

Python's Mutable vs Immutable Types: What's the Difference? – Real Python

In this tutorial, you'll learn how Python mutable and immutable data types work internally and how you can take advantage of mutability or immutability to power your code.