Let's learn some more Python syntax, using what we know in Snap!.

Declaring Variables

In Snap!, we created variables by using the "Make a Variable" button. In Python, we don't explicitly declare a variable in code. Rather, variables are created automatically when they are assigned to a certain value:

x = 0 is the same as x = 0

We can change the value of x by setting it equal to a different value:

x = 5 is the same as x = 5

Or by using different operators:

x = 5 + 3 is the same as x = 5 + 3

x = 5 * 3 is the same as x = 5 * 3

x = 5 mod 3 is the same as x = 5 % 3

Just like in Snap!, you can use multiple operators at the same time:
x = 5 + 4 * 3 is the same as x = 5 + 4 * 3

Python automatically uses order of operations to evaluate expressions with multiple operators. In the example above, Python will first evaluate 4 * 3, which equals 12, and then evaluate 5 + 12. In the example above, x equals 17. If you want to change the order in Python, use parentheses:

x = (5 + 4) * 3 is the same as x = (5 + 4) * 3

Another important block we used with variables was the change by 1 block. In Python, it is done a little differently. Notice that change by 1 and x = x + 1 are equivalent. Python follows the structure of this second block to change the value of a variable.

x = x + 8 and change x to 8 are the same as x = x + 8
(Quick Tip:
x = x + 8 is the same as x += 8)

Here's a quick summary of many of the useful operators in Python shown side by side with their Snap! equivalent. We can see that Python operators like greater than or equal to >= can save us a lot of time when writing our code, since in Snap! we would have needed to drag out multiple blocks.

Function Snap! Python
Addition x + y x + y
Subtraction x - y x - y
Multiplication x * y x * y
Division x / y x / y
Modulo x mod y x % y
Less Than x < y x < y
Greater Than x > y x > y
Equals x = y x == y
Not Equal x != y
Not not x
Greater Than or Equal To x >= y
Less Than or Equal To x <= y

Be careful! Notice that in Python, = is used to assign variables, and == is used to check the equality of values. Make sure you remember the differences between Snap! and Python syntax.