Changes between Version 28 and Version 29 of ControlSystems/SoftwareTeam/IntroToPython


Ignore:
Timestamp:
Sep 4, 2017, 5:58:11 PM (8 years ago)
Author:
cdelgigante
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/IntroToPython

    v28 v29  
    5252In 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".
    5353
    54 == IF-THEN-ELSE ==
    55 If you want to do more, we need to introduce control structures.  Take the program below, it uses an IF-THEN-ELSE control structure to ask the program to make a decision, and run code based on that decision.
     54If you want to do more, we need to introduce control structures. 
     55
     56== If-Then-Else ==
     57Take the program below, it uses an If-Then-Else control structure to ask the program to make a decision, and run code based on that decision.  You'll use these all the time.
    5658
    5759{{{
     
    6668print("Hope you had a good time")
    6769}}}
    68 This program uses a IF-THEN-ELSE statement to tell the computer to make a decision in your program, and you'll use them all the time.  ''Go [https://www.tutorialspoint.com/execute_python3_online.php here] and run the code above.''  It should print:
     70
     71''Go [https://www.tutorialspoint.com/execute_python3_online.php here] and run the code above.''  It should print:
    6972
    7073{{{
     
    8487 1. 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.
    8588
    86 Now, in the editor change the line with the if ''expression'' to
     89Now, in the editor replace the line with the if ''expression'' with
    8790{{{
    8891if location == 'Poughkeepsie, NY':
    8992}}}
     93
    9094and run it again.   What happens?   It should print "Go away, we're out eating tacos".  Why?  If I was in Poughkeepsie, I'd be out eating tacos too.  Yummmm, tacos.
    9195
     
    107111Yep, nothing was output. Nada.  In this case, we used variables which were numbers, and a greater-than expression to compare them.  Since the condition was ''False'', nothing was printed.
    108112
    109 (Also note the text that starts with a # sign.  The pound sign and everything after it on that line is called a ''comment'', and its a way to put some notes in the code that the program doesn't run.  The team wants you to put comments in your code a lot--it makes the code easier to read and understand)
     113(Also note the text in the code that starts with a # sign.  The pound sign and everything after it on that line is called a ''comment'', and its a way to put some notes in the code that the program doesn't run.  The robotics team wants you to put comments in your code a lot--it makes the code easier to read and understand.   And we like it when we can understand you.)
    110114 
    111 Here's a couple other control structures that you should know:
     115Now for a couple of other control structures that you should know...
    112116
    113117== For Loop ==
    114 The For loop control structure runs code repeatly repeats over a variable.  Try running this one:
     118The For loop control structure runs code repeats over a variable.  Try running this one:
    115119{{{
    116120fruits = ['banana', 'apple', 'mango']
     
    132136Note that the "Current fruit..." print statement is indented.   Just like in a IF-THEN-ELSE control structure, you can run a block of of multiple lines of code in a for loop, just indent all the lines.
    133137
    134 FOR loops are handy if you want to avoid repeating yourself in code, and do the same thing over and over again.  The less code you write, the less chance you can make a mistake.  Not to mention its easier to read.
     138For loops are handy if you want to avoid repeating yourself in code, and do the same thing over and over again.  The less code you write, the less chance you can make a mistake.  Not to mention its easier to read.
     139
    135140
    136141== While Loop ==
     
    138143A While loop is the more charming cousin of the FOR loop, this one repeats as long as its condition is ''True''.  If the condition is false when it gets to the While loop, the While loop wont run at all. 
    139144
     145{{{
     146x=0
     147while (x<100):
     148   x=x+1
     149print(x)
     150}}}
     151
    140152----
    141153[[[wiki:ProgrammingReserve Programming]Archive | Programming Archive]]