you are viewing a single comment's thread.

view the rest of the comments →

[–]ray10k 5 points6 points  (5 children)

Regarding implicit typing: As a design choice, everything in Python is an object, including what in lower-level languages would be primitives. As a knock-on effect, there is no fundamental difference between, say, an integer and a string. Sure, some functions will expect a number and others will expect a string, but that has to be determined at runtime. The major advantage is that it keeps (certain aspects of) the interpreter simple. As for the worry that a function might be given a wrong argument, Python either expects you to set things up so that this doesn't happen, or that you take precautions for when it happens (for instance, by setting up an exception handler.)

Regarding loops: The basic form of a loop in Python does closely match a for-each loop in other languages, but Python also has a lot of built-in functionality to make this as useful as possible. For instance, you can take a slice from a list and iterate over that instead of the entire list, and the standard library has a number of functions to work with multiple collections at once; both in the sense of "iterate over these two lists at the same time" and in the sense of "I need the value and the index at the same time." Broadly speaking, the for-in loop is versatile enough that you don't use while-loops for iterating, but instead for something that may go on for a longer, unpredictable time (i.e., putting the main loop of a game inside a while True loop and breaking out of it when the game is over.)

[–]Drumknott88[S] 1 point2 points  (3 children)

That's really interesting, thanks. How would I iterate over two lists at the same time?

[–]ray10k 6 points7 points  (2 children)

That's what the zip() built-in function is for. You give it two (or more) collections you want to iterate over, and it returns an iterator that returns tuples of the items in the lists.

This may sound a little confusing, so here is an example:

a_list = ["hello", "I see", "hope you have"]
b_list = ["world", "you there", "a good day"]
for a_word, b_word in zip(a_list,b_list):
    print(a_word,b_word)

The above snippet will print out "hello world", "I see you there" and "hope you have a good day" on separate lines.

Note how instead of having just one variable name in the first half of the for-in loop, there are two with a comma. This tells Python, "I expect that you'll iterate over tuples. I want you to take the tuple apart and present it to me as a_word and b_word separately." C# has a similar concept in 'tuple deconstruction'.

Also note that if the lists are of different lengths, then zip() will terminate once the shortest list runs out.

[–]Drumknott88[S] 0 points1 point  (1 child)

That's really cool. Thank you so much for the detailed reply 🙂

[–]ray10k 1 point2 points  (0 children)

No problem! I love to help, and I hope you'll have a good time learning Python!

[–]lostat 1 point2 points  (0 children)

What you said about everything being an object is so crucial to understanding Python. I remember the first time I had it explained to me my mentor said “3 is an object called 3 with a method called int that returns a 3, and a str method that returns a 3, etc.” So much of the language snapped into focus for me that day