Hacker News .hnnew | past | comments | ask | show | jobs | submitlogin

> "Python is a programming language that is easy to get started with, good for beginners"

I couldn't disagree more. In my experience, Python is a terribly difficult language for anyone who doesn't already have a solid grasp on common programming concepts. Its syntax is easy to read, but it's chuck-full of features and optimized for experienced developers, not beginners.

For example, consider a simple counting loop in C...

  for(int i = 0; i < 5; i++) {
    printf("%d\n", i);
  }
That code is a little obtuse, but what it does is very simple. The `for` loop requires three statements - initialization, condition, and iterator. The initialization statement (int i = 0) is only called at the beginning of the loop and is used to define our variable (in this case, i).

Next the `for` loop executes the condition statement. If the condition evaluates to true, it executes the body of the loop, then the iterator statement, then jumps back to the condition and tries again. If the condition evaluates to false, it moves on to the next line of code after the `for` loop.

In this case, our loop initializes i as 0. The condition `i < 5` means the loop will execute the body until i >= 5. The body simply prints the value of i and, after the body is executed each time, the loop executes our iterator statement which increments i by 1.

So long as you understand variables, conditions, arithmetic operators, and the `printf()` function you can figure out a `for` loop in C. Now consider the same thing in Python...

  for i in range(5):
    print(i)
That code is pretty easy to read, but what does it do? The `for` loop is used to iterate over an "iterable" object, which is a collection of multiple values. The body of the loop is executed for each value in the collection and assigns i a value from the collection for each iteration. In this case, our collection is output by the call to `range(5)`. The body of our loop simply prints the value of i.

What's an object? An object is a thing with attributes (variable within the object) and methods (functions within the object). Objects can be values of variables just like any primitive value.

What does range do? The builtin function `range` outputs a generator which yields values from the minimum (default 0) to the maximum (in this case 5) at a given step (default 1). The minimum is inclusive but the maximum is not so in this case it will yield 0, 1, 2, 3, and 4.

What's a generator? First we need to define "iterator". An iterator is an object which helps to iterate over a collection of values. All iterators have a special `__next__()` method which returns the next value in the collection. When there are no more values left to iterate over in the collection, the `__next__()` method raises a `StopIteration` exception. A generator is a type of iterator which mimics having a collection but instead of holding a collection of values in memory and iterating over them, it generates each value one-at-a-time only when the `__next__()` method is called. In our case, the generator starts at 0 for the first value and adds 1 to the output each time `__next__()` is called. When it reaches 5, instead of returning it raises the `StopIteration` exception.

What's an exception? An exception is a mechanism for interrupting execution of code. Instead of continuing to the next line of code, an exception causes the program to exit the call stack one level at a time until it either reaches the top or is caught by something. In our case, the `for` loop catches that `StopIteration` exception for us and uses that to know when to stop. Hopefully you know what the call stack is by now.

Yeah, a novice could probably ignore most of this stuff and just take for granted that the body of the loop will get executed for each value in the range 0..n (where n is the value given to `range()`). But this is just one example. Python requires novices to take a lot of things for granted from the start. Its syntax is mostly simple and elegant, but in terms of functionality it hurls developers into the deep end and they often have a hard time staying afloat if they haven't already learned the basics with other languages.

...that said, I'm rather fond of Python and think it's a great fit for programmable calculators for a variety of other reasons. "Good for beginners" just isn't one of them.



You forgot to explain how the CPython interpreter works, how the operating system works, as well as how a computer works, how a modern super-scalar, out-of-order multi-core microprocessor works and the physics behind silicon-based integrated circuits and electronics.

Abstractions are a good thing. When I write that for loop in C, I don't need to worry how the compiler will translate this into an abstract syntax tree, how that tree gets transformed into machine code nor how the microprocessor will interpret and execute it. I just think in terms of the abstract C machine model unless I need to peek under, usually for performance reasons.

If you truly believe in first principles for teaching programming languages, there's Forth, where a minimal implementation is basically a dozen or so of native primitives and an indirect threaded code core, leaving everything else to the programmer (disclaimer: I've never programmed in Forth nor bootstrapped an interpreter). Or Brainfuck if you don't mind a Turing tarpit, but I'd think twice before introducing that as a beginner's first programming language.


That's not fair. `range()` is a function which outputs something and understanding what that something is is imperative to understanding what that Python code does. I didn't start talking about the internals of `range()` or how the language is interpreted/compiled because none of that is necessary to understanding what the code does.

I'm not just sharing my personal thoughts here. I know half a dozen people who tried and failed to start learning programming with Python. Some of them moved onto other languages and had a better time while others gave up on the skill entirely. This is just what I've learned from discussing the issues they had. I have no reason to disparage Python. It's my favorite language and by far the one I use most.


And yet you've described how "range(n)" is implemented in exquisite details, whereas the caller only cares about its interface.

A beginner would further only care about its observable effects, which here would be at best something like "that returns something that's like a tuple containing the integers from 0 to n-1" and at worst "it makes my for loop run n times". Which is actually a fairly reasonable interpretation as long as the beginner doesn't try to concatenate a range with a tuple.

When I write a for loop in Python, I don't think about StopIteration and stuff, I just want to iterate something. Only if I were to implement a custom generator or iterable would I worry about that.


> And yet you've described how "range(n)" is implemented in exquisite details, whereas the caller only cares about its interface.

I didn't not say anything about how `range()` is implemented. I only described exactly what you said the caller should care about: the interface. I briefly described the inputs and outputs, then I went into detail about how the output is used by the `for` loop.

> A beginner would further only care about its observable effects, which here would be at best something like "that returns something that's like a tuple containing the integers from 0 to n-1"

In which case you need to describe what a tuple is and how a for loop uses it. In which case we're back to talking about iterators. All `for` loops in Python use iterators. There is no getting away from that.

> When I write a for loop in Python, I don't think about StopIteration and stuff, I just want to iterate something. Only if I were to implement a custom generator or iterable would I worry about that.

Same for me. That's because we already know how this all works and we know what parts we actually need to consider to implement a solution. That hardest part of getting into programming is building a fundamental understanding of how features work and how you can use those features to solve problems. And Python is filled to the brim with relatively complex features designed to help experienced programmers solve complex problems quickly and easily.

Like I said in my original post, you can gloss over details if you want. But understanding iterators is essential to understanding how `for` loops work in Python and if you don't understand how a `for` loop works, you'll struggle with understanding how and when to use it in future applications.




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: