100751 Views
79190 Views
45286 Views
43314 Views
39973 Views
33349 Views
NextCloud
Buddy Jr.
Robot Arms
TherePi - Music Making with a Raspberry Pi
Motion Controlled Arm
Electric Trombone
Building a Robot Arm with Raspberry Pi and PCA9685
Building User Authentication for Static Sites with FastAPI
Mastering Pydantic for Robust Data Validation
Mastering Markdown for Documentation with Jekyll
Introduction to Rust
OctaPi
KevsRobots Learning Platform
40% Percent Complete
By Kevin McAleer, 3 Minutes
Lets create a simple program that will display the message ‘Hello World’1.
First, here is the code, type this into your MicroPython editor:
print ("Hello World!")
When we run this, we will see the following printed to our screen (also refered to as the console):
Hello World!
To run this in Thonny, click the green run button:
run
As you can see this is a very simple piece of code. The word print is a special word that Python understands. Python expects a line of text between the brackets, inside of the speech marks.
print
Notice that the print command is in lowercase; typing Print or PRINT will result in an error message.
Print
PRINT
We call this case sensitive; we need to ensure that the commands we type are in the correct case for Python to work properly.
Congratulations, you’re now a MicroPython programmer!
Note The pound (or hash) symbol # means the reset of the line is a comment MicroPython ignores comments, so you can type useful reminders or notes to others here: # a single line comment For comments that span multiple lines, you can use 3 double quotations: """ This is a multi - line comment """
The pound (or hash) symbol # means the reset of the line is a comment MicroPython ignores comments, so you can type useful reminders or notes to others here:
#
# a single line comment
For comments that span multiple lines, you can use 3 double quotations:
""" This is a multi - line comment """
But why do we use the word print? It doesn’t come out of my printer?
Back in the early days of computing there were no screens, just a form of electronic typewriter that would print out the results of programs running on the attached computer. These terminals were called teleprinters or teletype machines which is why serial devices in Unix have the prefix TTY. A lot of computing stems from these early days and still remains in some form.
teleprinters
teletype
TTY
We put the hello world message in speech marks so that the computer knows where our message starts and ends. The speech marks can be either ' ' single or " " double quotes.
hello world
starts
ends
' ' single
" " double
Quotes In MicroPython you can use double " " or single quotes ' ' when working with text strings, the preferred method is to use double quotes: read the Black style guide for more background on this. Double quotes anticipate the need for apostrophies within text so it makes sense to use them by default.
In MicroPython you can use double " " or single quotes ' ' when working with text strings, the preferred method is to use double quotes: read the Black style guide for more background on this. Double quotes anticipate the need for apostrophies within text so it makes sense to use them by default.
" "
' '
We use the brackets ( ) to tell the computer that we want to run a function (a named block of code), the function named print.
( )
function
Also notice that the commands are in lowercase - no capital letters, this is because MicroPython (and Python) are case sensitive meaning the words print, Print and PRINT are not all the same.
lowercase
case sensitive
The print function is built-in to MicroPython, it already knows how to print things, so we don’t have to include any extra commands to make that work
This is a tradition that many programming language tutorials use to help you understand how to write a simple program. ↩
< Previous Next >