Python Intro
Python is a popular scripting language. It is well capable and user friendly. It ships with REPL (Read-Evaluate-Print-Loop) that allows users to inject code in on the fly. A small device , like the BrainPad, runs a special version of Python called MicroPython https://www.micropython.org/.
Prerequisite
This lesson assumes you have completed the Python System Setup.
Objective
This lesson is a quick introduction to the language with links to individual sub-lessons. The goal at the end of this lesson is to write a program that counts 0 to 7. And then we’ll modify the program to print 0 to 7 but skip number 4.
Case Sensitive
Python is case-sensitive like most coding languages, but what does that mean?! This means the language cares greatly on how things are spelled. For example, Print() and print() are not the same. The first one is the Print() function we have used earlier, but the second one print() is a built in function in Python that prints to the standard output. When using Thonny, the standard output is the Shell window.
Comments
A comment is a group of words or sentences we can add to our code that don’t affect how it runs. The computer will ignore the comment and run the code only. We use comments to tell us what’s happening in the code we’ve written or how something is done so others can understand the code we’re creating. Python uses # to start a comment and everything after that # will be ignored.
To make comments easier to spot by our eyes, they are usually colored differently.
Comments can also be useful for testing different sections of code by commenting and uncommenting lines and viewing the results.
# This is a comment.
# Comments are useful to remind us of what our code is doing.
# The computer will ignore comments completely.
Print("Hello")
# Print("Goodbye) # This line of code won't run unless we uncomment it.
Variables
A variable is a spot in the computer memory to help the program in memorizing something. We can give the variable any name, but the name can’t contain spaces, can’t start with a number, and can’t contain a symbol. Say we have a variable to keep track of a count and the count now is 5.
count = 5
Print(count)
Can you guess why Print() showed the value of count, which is 5, instead of printing the word count?
count = 5
Print(count)
Let’s make a program that takes two numbers and then adds them up. The two numbers will be stored in variables a and b.
a = 5
b = 4
Print("The sum of a and b is:")
Print(a+b)
The Variables lesson covers further details.
Space vs Tab
Python uses indentation, or white space, to know when blocks start and end. This makes the white space at the beginning of the line very important. That white space can be 4 spaces or a tab. This is where things can get tricky. While it is okay to use either or, it is not okay to mix between spaces and tabs in the same blocks. But since a tab is considered white space and a space is also considered white space, a person looking at the code will not see the difference.
When using an editor, like Thonny, the editor will highlight tabs for the user to see better.

Another benefit of using an editor like Thonny is that it knows about blocks and helps with adding the white space. Since using 4 spaces is recommended over using tabs, pressing the tab-key on the keyboard will result in 4 spaces, not a tab in Thonny.
If you do not understand this section, just remember that you do not want to see any white space that is highlighted with darker areas. If you do, delete that “tab”; and then the next time you hit the tab key, Thonny will just add 4 spaces. You can also hit the space key 4 times!
When necessary, and only for demonstration purposes, we will use the → symbol to show spaces. However, when you write the code, you will add a space for each → you see in the lessons. The space added (indentation) before the lines inside the block are very important as this is how Python knows where blocks start and where they end. Each line with the same indentation is a continuation of the block. The last line with no indentation is the end of the block.
count = 0
while count<8:
→→→→Print(count)
→→→→# Still in the block
→→→→count = count + 1
# block end
count = 0
while count<8:
Print(count)
# Still in the block
count = count + 1
# block end
In summary, a block is at the indented line(s). An indent is a tab, or 4 spaces. As a recommendation as you are learning how to code, use 4 spaces instead of tab because hitting the tab key on Thonny will automatically add 4 spaces.
Loops
So far, all of our programs have executed a small program and then terminated. However, most programs will continue to run, handling specific tasks repeatedly. This happens in a loop that executes a specific block over and over.
To indicate a block, Python uses indentation. In this example, the block is a while loop that will run as long as the variable count is less than 8. Inside our block, we increment count by 1 in every loop. We do so by taking the count value, add 1 to it and then assign the result back into count. So, if count was 3 before that line, we add one to it making it 4 and then assign it back to the same variable itself.
count = 0
while count<8:
→→→→Print(count)
→→→→# Still in the block
→→→→count = count + 1
# block end
Learn more at the Loops Lesson.
Operators
Operators are used for assigning values, like when we used = symbol, and for evaluating/comparing two values, like when we used the > symbol. There are other operators to help us mathematically, like the + symbol, and others to help with logic checks.
Learn more at the Operators Lesson.
Flow Control
The while loop used earlier automatically checked for a condition and kept looping as long as that condition was true, which was count is less than 8 (count<8). But what if we want to check for something without using a while loop? The answer is exactly what you may have guessed, an if statement! You simply have a block, like the loop, but in this case the block will run only once if a condition is true.
This code will print count if count does not equal to 4. In most programing languages, “not” is the ! symbol. And the != means “does not equal”.
if count != 4:
→→→→Print(count)
Let’s now combine the earlier while loop with the if statement, say we want to print numbers 0 to 7, but this time we want to skip 4! Meaning, we will Print() the number if count does not equal 4.
count = 0
while count<8 :
→→→→if count != 4:
→→→→→→→→Print(count)
→→→→count = count + 1
In this example, the first indentation in the inner block belongs to the while loop, and second indentation belongs to the if statement. Note how the if statement is inside the while loop. This is called nesting, and in this example it’s where two indentations are needed (which is one line in this case and it is at Print(count)). Remember that an indentation is 4 spaces. So we need 2 indentations, resulting in 8 spaces.
Here are three examples side-by-side to help you see the “inner block”. The if statement where used will only affect the Print line of code and not the following line that increments the count.
count = 0
while count<8 :
→→→→Print(count)
→→→→count = count + 1
if count != 4:
→→→→Print(count)
count = 0
while count<8 :
→→→→if count != 4:
→→→→→→→→Print(count)
→→→→count = count + 1
The results? All numbers show except for 4.
Learn more at the Flow Control Lesson.
Functions
Functions are tasks a computer has learned to execute. When someone asks you to sleep, you wouldn’t just lay down in place and fall asleep. The “sleep” function (task) will internally handle many tasks, like go to bedroom, turn lights off, lay in bed, shut eyes…etc. However, instead of commanding all the above separately, you simply combine it all into “sleep”.
You can create your own functions that handle multiple tasks, and you can also use some of the built-in functions that we have pre-created for you. Our favorite is Print(). By the way, we will always add () to indicate a function.
Print("Hello World!")
Go ahead and try this code. You can change it to print your name, and even add multiple lines.
Print("Hello, my name is:")
Print("BrainPad")
Print() can also print numbers or the results of a mathematical calculation. The first line will print 55 and the second will print 10.
Print(55)
Print(5+5)
The Functions Lesson covers functions in further details.
What’s Next?
This lesson links to individual lessons that cover each topic in further detail.
Once you understand the core concepts of Python, you can start with other Lesson Plans covering Robotics, Games, and Circuits.
BrainStorm
Computers are not as smart as we may assume. They run instructions step-by-step. But if so, how does a computer play music while surfing the web and checking emails? They do so by running billions of instructions per second. It feels like things are happening simultaneously, but it’s just a very fast simple machine.
How do computers compare to our brains? Do you think we process one instruction at a time? How about feelings? Computers always have an answer, but how come we are sometimes unsure? How do we make decisions that we feel confident about, but then we regret those decisions later? Would a computer regret a decision? What is “regret” to a computer if all it knows is a yes or no?