Python vs JavaScript language features

A for loop is a little neater imo, plus a for loop means the i variable is contained within the local scope of the loop (also never use var).

2 Likes

I split this post to a new topic, so we don’t get so off-topic.

1 Like

Ahhh. That’s better…

Unfortunately I don’t know how to “move to a new topic”

1 Like

IIRC that’s TL4+ only, which is why you couldn’t do it.

1 Like

I don’t think there’s anything above TL4.

Mods and Admins are above TL4.

1 Like

There’s mod (me :smile:), admin and staff

2 Likes

There’s no TL5 for sure;

Unless you consider Mods TL5, that’s true.

2 Likes

Please keep related to Python vs. JS

3 Likes

That is not really a fair comparison, as range is not required for a loop.


for (let i = 1; i < 10; i += 2) console.log(i) // How do you know when each of these are executed?

Or

i = 1
while i < 10:
  i += 2
  print(i)
3 Likes

Number two yes. The for syntax looks just weird in this case.

1 Like

That’s true, but I was saying that python’s equivalent of the for (init; condition; increment) syntax is range(starting, max, increment).

while loops are pretty much standard across all languages

1 Like

Because in python loops suffer from memory leak. Something that should not happen.

1 Like

What does that mean?

1 Like

It means that variables defined/used in a for loop are not deleted when the form loop ends
If you runs this code

for x in range(0, 5):
    pass

print(x)

In python instead of giving error, it prints 4. This is memory leaking. If you do loops where lot of data is assigned and used in the loop, the memory is not freed once the loop end.

5 Likes

What about this?

for x in range(0, 5):
  pass

del x
1 Like

Sure it works, but it should not be needed to delete x and 99% of people do not …

6 Likes

That’s also why you should use let over var:

for (var i = 0; i < 5; i++) console.log(i);

console.log(i); // 5
for (let i = 0; i < 5; i++) console.log(i);

console.log(i); // ReferenceError: i is not defined
4 Likes

For as much as i dislike JS, it is better in this respect than python,

4 Likes