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 the code that you write, while 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 using 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".

For example: let's take a look at this simple max function in Python:

max function

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

Look at how similar these two functions are! The code is practically identical, line by line, only with Python's syntax.

You will see Python code side by side wit,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.