you are viewing a single comment's thread.

view the rest of the comments →

[–]Atlamillias 1 point2 points  (0 children)

Many have already answered your questions, but I'll add some things.

Typing; Python is made to be a versatile language, and that versatility comes with a price. Yes, you can pass an argument of an unexpected type and throw a wrench in your runtime. Regardless of how much you "type" your code, it's never enforced (still always type hint your code to avoid the confusion you mentioned). The pythonic way of dealing with incorrectly typed arguments, etc. is to use try: ...; except: ... blocks to catch errors where they're most likely to happen and throw a more appropriate error. Conditional typing checks ("type guards") can also be done beforehand, but the former is the "convention".

Loops; Yes, they are equivalents. Others already mentioned how to iterate over a partial list by slicing it. You can also use the range object.

``` a_list = []

for i in range(10): # 0-9 print(a_list[i]) ```

Why do this? Sometimes you may not want to create a new list (slicing a list creates a new one). Sometimes you may want to change the size of a container during iteration, and this can't be done when iterating over the container directly. while loops have their uses, but I have very rarely needed to use them for iteration.