An Introduction to Python

Snap! is a graphical programming language ("click and drag", "drag and drop", "snapping" blocks together). Most other programming languages, such as Python, are text based, which means that you must type out your code. In this lab, you will get to practice writing Python code in the interactive interpreter and in a text file. An interpreter immediately executes each line of code that you write (every time you press 'return'). A text file allows you to write all of your code in one place and then run it later on. We'll talk more about the differences between these two formats later in the lab.


Syntax refers to the way in which code must be formatted for a machine to execute it predictably and properly. In English, syntax refers to the grammar rules that are used to properly structure sentences, such as the use of nouns and verbs, or the use of punctation. In Snap!, syntax is very simple, determined by things like the shapes of blocks. Snap! and all other languages enforce syntax to prevent basic errors when we run our code.


In text languages, syntax is a little bit more involved, where each language has its own set of rules. However, learning this syntax isn't too difficult! Python has syntax rules for arranging code that are different from those found in Snap!. The major objectives of this lab are to get you to practice writing in Python's syntax and to be aware of how to approach problems in a new way, while still implementing background knowledge of computer science concepts. Everything that you've learned in Snap! can be applied to Python - algorithms, functions, recursion, abstraction and more are all techniques that you will use regardless of the language you are programming in. This application of computer science is called "computational thinking".

Let's compare a `max` function in Python and Snap!


max function

def max(x, y):
    if (x > y):
        return x
    else:
        return y
                    

Notice how similar the two functions are! The code is practically identical, except for the syntax differences of each language.


You will see Python code side by side with Snap! code throughout this lab because, although you may not believe it, anything that can be built in snap can also be created in Python (and vice versa). This process is called codification and allows us to map one language to another, as long as we make sure to translate according to each languages' unique syntax.