Version 17 (modified by 8 years ago) (diff) | ,
---|
Prerequisites: Control Systems Overview
Lesson 1: Getting Started
If you want to tell a computer to do something, you have to communicate with the computer in a programming language. When we say language, we mean it. A language has words and grammar and everything else, just like French.
Computers, despite what you may think, are dumb. They do only exactly what you tell them to, and don't guess or interpret. If say to your Mom or Dad "Hey, can you bring me that thing?", there's a (slim) chance they may bring you the right "thing". A computer wont. Ever. Just a cold, unfeeling "ERROR". A programming language is very precise, and to tell a computer what you want, you have to be precise too. In this training session, we'll introduce programming to you, and make some things work. You know, those things.
Hang on, it gets harder from here.
(A quick note...If you know all this already and are getting bored...CONGRATULATIONS! Here's a link to some cute corgi puppy videos as your reward. Then, come back and dive into a later section. This is all self-paced.)
We'll be starting with a programming language called Python, specifically Python 3. There are a lot of programming languages. Some are created because they're better for solving certain problems, others because people are bored and want to try something new. Python has been around for a while, and its pretty popular because its easy to use. The team uses Java to control the robot but that language is a little trickier. We'll get to that one later once you get a handle on the basics.
Python, like every programming language, has a structure. Here are the big parts:
- Variables: Things you assign to and read from. Like in Algebra. A="Dump Truck" sets the variable A to the string "Dump Truck". They're basically the nouns.
- Functions: These do things. They take nouns as arguments, do something, and can return nouns. In redCar = paintCarRed(blueCar), paintCarRed is a function. They're the verbs, they do things, and they take variables and return variables.
- Control Structures: These are instructions like IF this is true THEN do that…. These are like grammar rules, like use active voice, or use punctuation.
- Libraries: A lot of other people have written functions to do things. A lot of things. When these things are bundled together they're called libraries. You can tell a computer to use a library and save your self a lot of typing.
Much like in English, you blend all of these together to create stories, or programs. Both have a beginning middle and end. So lets write the first programming story that all programmers start with: "Hello World"
a="Hello World" print(a)
This tells the computer to print, you guessed it, "Hello World"
Now you try it: Do the following:
- Highlight all the text in the central area and hit back space (this'll we'll clear out all the sample code.)
- Then highlight the code above, right click and click {{{Copy}}.
- Go here. This site is our code editor, and its pretty cool. You can put code in the center section, and click execute to run it. The lower window shows the results.
- Highlight all the code already in the center window.
- Right click and pick {{{Paste}}}. This should replace everything in the window with the contents above
- Click {{{Execute}}} right above the center window.
(From now on, we'll call this step "Go here and run the code above". (Just come back to these steps if you forget how to do this).
After a bit, you'll be rewarded with the message in the bottom window:
Hello World!
And, boom, now you're a programmer. The rest is just more details. This is the end of Lesson 1.
Lesson 2: More Things To Do
In lesson 1, we had you create a program, "Hello World". Now, "Hello World" is not too exciting if you want to program robots, but its an important first step. We showed you how to run a function (or verb) print
, which does something (show on the screen the value of its argument a
, where a
is assigned the value "Hello World".
If you want to do more, we need to introduce control structures. Take the program below.
location = 'HERE' print("Welcome to our store\n") if location == 'HERE': print("We're here! Come on in\n") print("Our ours are 11:00am-11:52am\n") else: print("Go away, we're out eating tacos\n") print("Hope you had a good time\n")
It should print:
Welcome to our store We're here! Come on in Our ours are 11:00am-11:52am Hope you had a good time
Three things to note:
- The double equals in location=='HERE' on the first line. It is called a condition. This is a equals comparison. Other kinds of comparisons in python are listed here. Also note on that line 'HERE' is in quotes. That's to indicate its a string (a text value) not some variable.
- Spacing and indents in Python are important. the line "We're here..." and the line below it are indented to indicate its a part of a statement block to run when the condition evaluates to True. The second block on the else is run when the condition to False, and only contains one line. If you want to do stuff outside of the IF-THEN, don't indent it like the first and last print statements.
- You'll see that a few of the print statements end with a {{
n}}. This is called a escape sequence, and its shorthand for a newline. When the computer sees it in a print statement, it will add an extra line to the output. See here for other escape sequences you can add.
Now, change the condition to Location=='Poughkeepsie, NY'. It should print "Go away, we're out eating tacos". What happens? Why? If I was in Poughkeepsie, I'd be out eating tacos too. Yummmm, tacos.
This control struction evaluates a *expression* (location=='HERE') and if its true, then do the THEN part, otherwise do the ELSE part. This is called the IF-THEN-ELSE structure, and it’s the most basic control structure.
Here's a couple other control structures that you should know
For Loop: This one repeats over a variable. It's good for counting try running this one:;
For (Potato
While: The more charming cousin of the for loop, this one repeats
Attachments (3)
- 1B6AA343-B7D4-49F7-88DA-2AB89A7A4E72-5482-00000452F115F905_tmp.png (42.6 KB) - added by 8 years ago.
-
tumblr_nzwkbcWeYP1qhjjeao1_500.gif (81.7 KB) - added by 8 years ago.
Nyan cat
-
deepthoughtcomputer-619-386.png (298.9 KB) - added by 8 years ago.
Deep Thought
Download all attachments as: .zip