r/learnpython • • Mar 01 '21

Python is kicking my butt

I’ve been learning python since early December now.. I seemed to become comfortable with the syntax and was gaining confidence quickly!

Then I moved on to Django and was quickly sent a reality check as the difficulty level increased really fast... I went back to python and started learning classes... this also is extremely hard to grasp..

While learning classes I realized I didn’t know for loops.. dictionaries.. len/range() nearly as much as I thought I did... the confidence I so quickly built is now gone.

I know the journey to web dev isn’t going to be easy at all.. I’m not giving up.. just wanted to get this off my chest.. I’m going back to the basics this week. Wish me luck. Have a nice week!

486 Upvotes

118 comments sorted by

View all comments

6

u/[deleted] Mar 01 '21

len/range() nearly as much as I thought I did...

I don't really get what you think there is to know about len and range (which are two separate functions that don't really relate to each other except coincidentally.) len is especially simple - it returns the number of values contained in a collection. Easy!

range is a little subtle and its documentation is wrapped up with tuple and list so it's not especially clear:

https://docs.python.org/3/library/stdtypes.html#typesseq

but the long and the short of it is that it's an iterator over sequences of integers, and the most common and easiest to understand way to specify a sequence of integers is as a sequence between 0 and some other value. (For various good but tedious reasons, the interval is specified as half-open, and thus includes zero but not the end term.)

7

u/[deleted] Mar 01 '21

Maybe it's those times when len and range are used together, like

for i in range(len(input_list)):
    pass

5

u/tagapagtuos Mar 01 '21

If that's the case, then I would say that doing some HackerRank would help, since that's common in there.

Regardless, OP shouldn't really sweat it since range(len(...)) is usually a symptom of un-Pythonic code imo.

1

u/roylennigan Mar 01 '21

is that because of the ability in python to iterate over items in a sequence, like:

for item in sequence:
    x = item

without having to create an index for that sequence?

But then what if I want to do something like:

for i in range(len(A)):
    B[i] = A[i] - A[i + 1]

3

u/tagapagtuos Mar 02 '21

Good question.

The reason why you don't what to iterate over the range of length of A is that if i is the last element of A, then A[i + 1] will raise an IndexError. Also if B[i] also has risks of raising NameError or IndexError.

Depending on the nature of the problem, I would do sometihng like:

B = []
for x, y in zip(A, A[1:]):
    B.append(x - y)

2

u/roylennigan Mar 02 '21

That makes sense, thanks. I mostly make plots for dsp concepts, so a lot of fitting one "number line" to another and plotting them. Most of the time, the data at the end of a vector isn't as important, so I can just drop it and not worry about the array going out of bounds.