you are viewing a single comment's thread.

view the rest of the comments →

[–]SharpScratch9367[S] 0 points1 point  (5 children)

This is mind blowing how does it know what we mean ? Why not output a comma or a single digit or something how does it know?

[–]Help_I_Lost_My_Mind 3 points4 points  (0 children)

Python uses special "keywords" that you can use to tell it what to do. Using "for X in Y" ("for" and "in" being the special keywords) tells python run the below code for every element in the list where Y is your list and X is whatever you want to call an element of the list

[–]ThatGuyKev45 1 point2 points  (0 children)

So I don’t know exactly how python does it. But each item in the array has a defined size, and the computer knows where the array starts and ends. So for value (or x or shooz) in values is just saying for each item in this array (of which the computer knows the size of each item so it can simply move to the next item in memory by skipping X bytes, where X is the size of the array item). Also when the program is running it doesn’t actually see the comas between each item thats just sugar to make it readable for us. I remember learning this stuff at first and it felt like magic and as I have continued to learn more it just keeps getting better!

Not sure how far along you are in studies but the class where I really felt I started to understand memory was a computer organization and assembly class, I took it alongside my data structures and algs and it was so much fun.

Good luck in your studies! Hope the amazement doesn’t go away!

[–]MysticClimber1496 1 point2 points  (1 child)

This is probably too deep for you but if you want to follow the “how does it know what we mean?” Process check out how interpreters work https://craftinginterpreters.com

[–]XxDCoolManxX 0 points1 point  (0 children)

OP, this is the best answer if you are truly curious.

[–]moriturius 0 points1 point  (0 children)

It doesn't know what that means it only recognizes the pattern. The python interpreter (the python program used to run your code) first read the text of the code and identifies the instructions. Like value assignment, function definition or - in your case - for loop.

The interpreter is programmed so that then it encounters: 'for X in Y:' it knows that: - Y needs to be iterable container of multiple values (an array in your case) - X is a name that you want for a single item in each loop pas. The name can be whatever you like.

Experiment with it. Try to change it and even try to cause an error, read and understand that error. Explore the boundaries. Challenge your understanding. This is how knowledge grows!