Changes between Version 43 and Version 44 of ControlSystems/SoftwareTeam/IntroToPython


Ignore:
Timestamp:
Sep 4, 2017, 6:56:17 PM (8 years ago)
Author:
cdelgigante
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/IntroToPython

    v43 v44  
    1515Python, like every programming language, has a structure.  Here are the big parts:
    1616
    17  * ''Variables'':  Things you assign to and read from.  Like in Algebra.  A="Dump Truck" sets the      variable A to the string "Dump Truck".   You can set them to numbers to or fancy things like lists.  They're basically the nouns.
    18  * ''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.
     17 * ''Variables'':  Things you assign to and read from.  Like in Algebra. `a="Dump Truck"` sets the      variable A to the string "Dump Truck".   You can set them to numbers to or fancy things like lists.  They're basically the nouns.
    1918 * ''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.
     19 * ''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.  Some functions are built in to Python, but you can create your own.
    2020 * ''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.
    2121
     
    2727print(a)
    2828}}}
    29 
    3029This tells the computer to print, you guessed it, "Hello World"
    3130
     
    4544Hello World!
    4645}}}
    47 
    4846The program told the computer to print the value of variable a on the screen.
    4947
    5048And, boom, now you're a programmer.  The rest of learning how to code is just more details.  This is the end of Lesson 1.
    5149
    52 = Lesson 2: Making decisions =
    53 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".
     50= Lesson 2: Making Decisions =
     51In 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 (shows on the screen the value of its argument `a`, where `a` is assigned the value "Hello World".
    5452
    55 If you want to do more, we need to introduce control structures.
     53If you want to do more in a program and actually make decisions in your code, we need to introduce control structures.
    5654
    5755== If-Then-Else ==
     
    6967print("Hope you had a good time")
    7068}}}
    71 
    7269''Go [https://www.tutorialspoint.com/execute_python3_online.php here] and run the code above.''  It should print:
    7370
     
    7976Our ours are 11:00am-11:52am
    8077                                                                                                                                                                                                                         
    81 Hope you had a good time                                                                                                                                                                                                                               
     78Hope you had a good time                                                                                                                                                                                                                             
    8279}}}
    83 
    8480Three things to note about this program:
    8581
     
    9389if location == 'Poughkeepsie, NY':
    9490}}}
    95 
    9691and 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.
    9792
     
    105100   print("Whohoo! Dinner's on you!")
    106101}}}
    107 
    108102''Go [https://www.tutorialspoint.com/execute_python3_online.php here] and run the code above.''  It should print:
    109103
     
    111105   
    112106}}}
    113 
    114107Yep, 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.
    115108
     
    159152Oh no, I'm tired, so I'm not going to tell you the answer for this one. You should be seeing a pattern by now.  You tell me---what should it print?    Count starts out at 0, and as long as count is less than 9 it will run the code in the block, then it will print "Good bye!".   What else does it print out?
    160153
    161 == Bringing it all Together... ==
     154== Here a Control Structure, There a Control Structure, Everywhere a Control Structure... ==
    162155Lastly, you can combine all three of these in any sort of combination, and nest them.  This is how you do real work in a program.   For example, try running this one:
    163156