Learning Python 3 with the Linkbot/Hello, World
Lesson Information **To Be Added** Vocabulary: Python Editor: a program used to create a block of text that will be sent to Python and interpreted. In this book we recommend IDLE 3. Line: a single sentence of programming code. Lines of code can be used to measure the size of a program and compare it to other programs. Function: a type of procedure or routine, a set of instructions to follow. Argument: data provided to a function as input. This is like the domain in algebra. String: a sequence of characters representing either a constant or a variable. Function Call: Necessary Materials and Resources: Computer Science Teachers Association Standards: L1:3.CT.4:Recognize that software is created to control computer operations Common Core Math Practice Standards: CCSS.MATH.PRACTICE.MP5 Use appropriate tools strategically.
What you should know
Once you've completed this chapter, you will know how to edit programs in a Python Editor, IDLE 3, or some other text editor, save them, and run them.
Since the beginning of programming tutorials, "Hello World!" has been the first foray into creating something on the screen, here is how to do it with Python:
If you are using the command line to run this program, then type it in a text editor, save it as
hello.py and run it with
Otherwise go into your Python Editor, create a new file (Ctrl + N for IDLE), and create the program as in section Creating and Running Programs.
When this program is run here's what it prints:
Remember, whenever you see code as an example, it's a good idea for you to type it in and run it. Learning by doing can be a powerful tool! It is worth the time it takes.
Here is a modified 'Hello World' using the Linkbot to say hello. Comments are added to help you understand what the code is doing step by step.
import barobo #loads 'barobo' module import time #loads 'time' module dongle = barobo.Dongle() #uses commands from 'barobo' to connect Linkbot dongle.connect() robotID = input('Enter Linkbot ID: ') #prompts user for Linkbot ID robot = dongle.getLinkbot(robotID) print ( 'Hello Linkbot Programmer' ) #prints greeting robot.setBuzzerFrequency(1047) #calls for pizo buzzer to make a tone time.sleep(0.25) #sets duration of tone robot.setBuzzerFrequency(1567) #changes tone time.sleep(0.5) robot.setBuzzerFrequency(0) #turns off tone.
Feel free to chance the frequency of the buzzer to change the tone, and the duration of time.sleep to see what that does to the greeting.
Now here is are a few more program:
print("I pledge allegiance to the flag") print("of the United States of America,") print("and to the republic, for which it stands,")
When you run this program it prints out:
I pledge allegiance to the flag of the United States of America, and to the republic, for which it stands,
When the computer runs this program it first sees the line (like one sentence of code):
print("I pledge allegiance to the flag")
so the computer prints:
I pledge allegiance to the flag
Then the computer goes down to the next line and sees:
print("of the United States of America,")
So the computer prints to the screen:
of the United States of America,
The computer keeps looking at each line, follows the command and then goes on to the next line. The computer keeps running commands until it reaches the end of the program.
Controlling a Linkbot LED Color
Linkbots are equipped with a multi-color LED, which is basically just a light that can change colors. Lets try a simple program to control the LED color on a Linkbot. To get started, follow these steps:
- Turn on your Linkbot by holding down the power button until the Linkbot flashes a bright red light.
- After about 5 seconds, the Linkbot will beep and show a blue light. Your Linkbot is now on!
- Take a Micro-USB cable and connect the Linkbot to your computer.
- Open your text editor and try the following code:https://en.wikibooks.org/wiki/Learning_Python_3_with_the_Barobo_Linkbot
import barobo myDongle = barobo.Dongle() myDongle.connect() myLinkbot = myDongle.getLinkbot() myLinkbot.setLEDColor(0, 255, 0)
Don't worry too much if you don't understand what's going on in the first four lines of this program. All of those concepts will be covered in later chapters. For now, just know that if you want to control a Barobo Linkbot, those four lines (or similar lines) are necessary.
When you run this program, you should notice that your Linkbot turned green! In the code that you wrote, there were three numbers: 0, 255, and 0. These three numbers determine the brightness of the red, green, and blue LEDs inside the linkbot, where 0 denotes the lowest brightness setting and 255 the maximum setting. If you want to try making your Linkbot a brilliant purple color, try setting the color numbers to "255, 0, 255". This tells the Linkbot to turn on its red and blue LED's to their maximum brightness. When the red light blends with the blue light, it appears purple!
Now is probably a good time to give you a bit of an explanation of what is happening - and a little bit of programming terminology.
What we were doing above was using a function called
there is one argument, which is
"Hello, World!". Note that this argument is a group of characters enclosed in double quotes (""). This is commonly referred to as a string of characters, or string, for short. Another example of a string is
"Jack and Jill went up a hill". The combination of a function and parentheses with the arguments is a function call.
A function and its arguments are one type of statement that python has, so
is an example of a statement. Basically, you can think of a statement as a single line in a program.
That's probably more than enough terminology for now.
Here is another program:
print("2 + 2 is", 2 + 2) print("3 * 4 is", 3 * 4) print("100 - 1 is", 100 - 1) print("(33 + 2) / 5 + 11.5 is", (33 + 2) / 5 + 11.5)
And here is the output when the program is run:
2 + 2 is 4 3 * 4 is 12 100 - 1 is 99 (33 + 2) / 5 + 11.5 is 18.5
As you can see, Python can turn your thousand-dollar computer into a five-dollar calculator.
In this example, the print function is followed by two arguments, with each of the arguments separated by a comma. So with the first line of the program
print("2 + 2 is", 2 + 2)
The first argument is the string
"2 + 2 is" and the second argument is the mathematical expression
2 + 2, which is commonly referred to as an expression.
What is important to note is that a string is printed as is (without the enclosing double quotes), but an expression is evaluated, or converted to its actual value.
Python has seven basic operations for numbers:
Notice that there are two ways to do division, one that returns the repeating decimal, and the other that can get the remainder and the whole number. The order of operations is the same as in math:
/, integer division
//, and remainder
So use parentheses to structure your formulas when needed.
Talking to humans (and other intelligent beings)
Often in programming you are doing something complicated and may not in the future remember what you did. When this happens the program should probably be commented. A comment is a note to you and other programmers explaining what is happening. For example:
# Not quite PI, but a credible simulation print(22 / 7)
Notice that the comment starts with a hash:
#. Comments are used to communicate with others who read the program and your future self to make clear what is complicated.
Note that any text can follow a comment, and that when the program is run, the text after the
# through to the end of that line is ignored. The
# does not have to be at the beginning of a new line:
# Output PI on the screen print(22 / 7) # Well, just a good approximation
Each chapter (eventually) will contain examples of the programming features introduced in the chapter. You should at least look over them and see if you understand them. If you don't, you may want to type them in and see what happens. Mess around with them, change them and see what happens.
print("Something's rotten in the state of Denmark.") print(" -- Shakespeare")
Something's rotten in the state of Denmark. -- Shakespeare
# This is not quite true outside of USA # and is based on my dim memories of my younger years print("Firstish Grade") print("1 + 1 =", 1 + 1) print("2 + 4 =", 2 + 4) print("5 - 2 =", 5 - 2) print() print("Thirdish Grade") print("243 - 23 =", 243 - 23) print("12 * 4 =", 12 * 4) print("12 / 3 =", 12 / 3) print("13 / 3 =", 13 // 3, "R", 13 % 3) print() print("Junior High") print("123.56 - 62.12 =", 123.56 - 62.12) print("(4 + 3) * 2 =", (4 + 3) * 2) print("4 + 3 * 2 =", 4 + 3 * 2) print("3 ** 2 =", 3 ** 2)
Firstish Grade 1 + 1 = 2 2 + 4 = 6 5 - 2 = 3 Thirdish Grade 243 - 23 = 220 12 * 4 = 48 12 / 3 = 4 13 / 3 = 4 R 1 Junior High 123.56 - 62.12 = 61.44 (4 + 3) * 2 = 14 4 + 3 * 2 = 10 3 ** 2 = 9
- Write a program that prints your full name and your birthday as separate strings.
- Write a program that shows the use of all 7 math functions.
1. Write a program that prints your full name and your birthday as separate strings.
print("Ada Lovelace", "born on", "November 27, 1852")
print("Albert Einstein", "born on", "14 March 1879")
print(("John Smith"), ("born on"), ("14 March 1879"))
2. Write a program that shows the use of all 7 math functions.
print("5**5 = ", 5**5) print("6*7 = ", 6*7) print("56/8 = ", 56/8) print("14//6 = ", 14//6) print("14%6 = ", 14%6) print("5+6 = ", 5+6) print("9-0 = ", 9-0)
- Here is a great list of the famous "Hello, world!" program in many programming languages. Just so that you know how simple Python can be...