Section 2.1 First steps
Python is a popular progrmming language that works well on both microcontrollers and "regular computers".
By tradition, the first program we write in any language simply prints out the message "Hello, world!" Below is an example of how to do this in Python.
Try running the code here in the textbook, and in your IDE (Thonny). Change to code to print out your name. What happens if you delete the parenthesis? Or the quotation marks?
Python can do math:
3 + 3 7 - 1 5 * 2 8 / 4
Try the examples above in your IDE. What if you have multiple operations on one line? Which operators have precedence? Try using parenthesis to enforce a particular precedence.
Challenge: Python has some other useful math operators. Try them and see if you can figure out how they work. What does the // operator do? What about the % operator? What about the ** operator?
We can also print numbers, or the results of computations.
print(3.14159) print(3 + 3)
To build larger programs, we will need a way to save values and use them later. We do this using variables. To create a variable in Python, we use the assignment operator (=) with a name on the left and a value on the right. When the line of code runs, the value will be stored in memory. Once a variable has been defined, we can use it later in the program just by referring to its name.
Note that this isn't mathematical equality; the assignment just sets the variable to hold a particular value when it runs that line of code. For example:
Mathematically x = x+2 makes no sense, but that's not what Python is doing. Instead, it computes the value x + 2 and puts that result into x (overwriting whatever was there before). You can click the "Show CodeLens" button to see the code execute step-by step.
While Python allows us to type code one line at a time in the REPL (aka shell or command window), we usually want to collect many lines of code into a program that will execute from top to bottom. This file is called a script.
For example, here is a script that finds the area of a circle given its radius:
radius = 3 area = radius * radius * 3.14159 print(area)
Try running this script in your IDE.
Write a script that converts a temperature in Farenheit into Celsius.
If we have to do this a lot, it would be nice to wrap it all up in a tidy bundle, called a function.
def circleArea(radius): area = radius * radius * 3.14159 return area
Once we've defined a function, we can call it by using its name, followed by the parameters in parenthesis:
a = circleArea(5) print(a)
A conditional statement allows your program to make a choice:
x = 3 if x > 5: print("x is big") else: print("x is not so big")
Write a function which determines whether a number is positive.
Challenge: Write a function that returns the larger of the two inputs passed to it.
A while loop executes a block of code as long as a condition is true.
while count != 10: print(count) count = count + 1
Write a program that counts from 10 down to 0, and then prints "Blastoff!"
Write a program that checks whether a number is a power of 2.