Intro to Python
Python is the language of choice for the experiments we’ll be running with the Kits.
As an introduction, this article will cover some of the basics so we can hit the ground running!
Python is a high-level, interpreted language designed for scripting. It supports Object Oriented Programming - which is a handy framework that we’ll be using quite a bit.
The high-level part means that Python reads closer to English than machine instructions and abstracts away many of the low-level hardware intricacies. The interpreted part means that Python code is executed line-by-line by the computer as it’s being run - as opposed to languages like C where it is first compiled before it can be run.
How we’ll use Python
The experiments will provide code that works out of the box that can be directly copied to the Omega and run. We’ll also discuss more interesting sections of the code in detail.
Python Syntax Overview
import time
# print ('This line will not be printed')
greeting = 'Hello world!'
print (greeting)
for count in range(0,10):
if (count % 2 == 0):
print ('Tick')
else:
print ('Tock')
print (count)
time.sleep(0.5)
The above is a block of Python code with all the basic building blocks of the language. Let’s go through it bit by bit.
Variables
The equals sign (=
) assigns the string ‘Hello world!’ to the variable named greeting
.
All variables in Python are created this way with the assignment operator.
Functions
Function statements
Functions in Python have two parts: a function name and a list of arguments that are sent to it.
Name | Argument |
---|---|
‘just a function’ |
The number of arguments that a function takes can be zero. Some functions return a value that you can assign to a variable.
Logic
The if/else
structure is used to evaluate variables and make decisions based on them. All indented lines after the if
statement will be executed if the condition is met. The first un-indented line after the if
statement ends the statement. For else
, all indented lines after the else
line will be executed when the condition of the if statement is unmet. So in the code above, only one option gets executed (either ‘Tick’ or ‘Tock’) when the interpreter gets to this part. Which one depends on the value of count
.
Extra evaluation statements can be inserted as an elif
block like so:
This adds a third option - all indented code after the elif
statement will be executed if:
- The
if
condition is unmet - AND the
elif
condition is met
Now one of three options will be executed, printing either ‘Tick’, ‘Tock’, or ‘Tack’ depending on value of count
.
Looping
# For-loop with a counter variable
for count in range(0,10):
print (count)
# While-loop - checks condition first, then starts the loop
while (count <= 10):
print (count)
The for-loop can iterate over any list, executing all indented code after the for
statement as many time as the number of elements in the list. The range function returns a list of integers in the given range. So the for-loop above will run 10 times, same as the example up top. However this loop will print the count of each cycle instead of ‘Tick’ and ‘Tock’.
The while loop checks a single condition every loop, so it’s useful for infinite loops and checking unique conditions. Since count
doesn’t change during the while loop, it will run forever assuming count
is no greater than 10, continuously printing an unchanging value of count
.
Using Libraries
The import
statement above adds all the functionality of the time
standard library to be available in your program.
Calling a function included in the library is done using the .
notation - time.sleep()
will call the sleep()
function in the time
library.
Learning More about Python
Python is a very popular language, so there’s a tremendous amount of tutorials out there. If you’re still unsure of how parts of our code work, we recommend taking a look at the guides over at the Python Wiki for programmers or non-programmers.
Comments
Comments in Python start with a
#
. Any text after the hash in the same line will be ignored by the interpreter.