DEV Community

Cover image for Intro to Python: Day 17 - Making Dumb Mistakes with Lists []
James Hubert
James Hubert

Posted on

Intro to Python: Day 17 - Making Dumb Mistakes with Lists []

Hi there ๐Ÿ‘‹ I'm a New York City based web developer documenting my journey with React, React Native, and Python for all to see. Please follow my dev.to profile or my twitter for updates and feel free to reach out if you have questions. Thanks for your support!

One of the things I've found most helpful about Python so far is the error messages. Whenever your program crashes, you get a full traceback message of all the places in the code where the program failed.

If you scroll to the bottom of the traceback, you'll see the first place in the code it failed- which is the first bug it encountered that needs to be fixed. Python will also try to print out an exception that indicates what kind of bug it encountered.

Today I encountered an IndexError while testing my program. Unlike Javascript, which will allow you to create a list by explicitly adding elements at a given index, Python is more strict, and will only allow this if the index already exists. This means that unlike in Javascript, the following Python code will crash:

my_list = []
my_list[0] = "apple"
print(my_list[0])

# console will print:
# ERROR!
# Traceback (most recent call last):
#  ...
# IndexError: list assignment index out of range
Enter fullscreen mode Exit fullscreen mode

This is because although the my_list list exists, there is no index 0 on it, so in Python we cannot simply add something there if it doesn't exist.

Javascript is more forgiving because it's made for working with browsers and the web. It does its best to help you. Python will not, but that's ok, because it's more frequently used for backend computations and more precise programs.

If you like projects like this and want to stay up to date with more, check out my Twitter @stonestwebdev, I follow back! See you tomorrow for another project.

Top comments (1)

Collapse
 
overflow profile image
overFlow

I havenโ€™t even read yet !! I jus wanna say you got me at โ€œdumb mistakesโ€. I got so excited.

I just finished reading : aNd itโ€™s short and sweet.
Please show us the right way.
Thanx

Awesomeness ๐Ÿ˜Ž