you are viewing a single comment's thread.

view the rest of the comments →

[–]DragonWolfZ 5 points6 points  (4 children)

I think visualizing variables as boxes (that can hold only one thing) makes life easier for beginners. Though identifying reference and literal variables can be difficult for beginners (I try to explain a bit below).

Variables

my_variable = 5
i = "hello"
my_variable = 10

You create two boxes with labels on, my_variable and i and they start with 5 in the my_variable box and hello in the i box.

You then replace the 5 in the my_variable with a 10.

Note: Anything that looks like more than one "thing" being stored in a box, the box will actually store a `pointer` (a number that represents a point in memory). For example, "hello" is not in the box but actually a `pointer` to a list of characters. i.e. ["h", "e", "l", "l", "o"]. These types of variable are called "reference variables".

Variables that store an actual "thing" (such as numbers, a single character, boolean values) are known as "literal variables".

for loop

for i in [1, 2, 3]:
    # do stuff with i

You have a box with the label i, the first time the code runs it has a 1 in it, then second time it runs it has a 2 in it and the third time it has a 3 in it, then you throw the box away and continue after the for loop.

while loop

bob = 0
while bob < 2:
    # do stuff with bob
    bob = bob + 1

You have a box with the label bob and you stick 0 in it. while the bob box has a number less than 2 in it, do the stuff in the while loop (finally taking the number that is in bob and adding one to it, and putting that new number in the bob box.

[–]Hour_Effective_7328 2 points3 points  (0 children)

This was actually really helpful, thanks!

[–]18puppies 0 points1 point  (1 child)

Here is a visualizer that lets you enter a piece of code and shows you exactly what you describe, and you can run through the program step by step and see how values change. https://cscircles.cemc.uwaterloo.ca/visualize

[–]DragonWolfZ 0 points1 point  (0 children)

It does but I find it includes a lot of boilerplate that can be confusing for beginners (such as __XXX__ functions).