Changes between Version 18 and Version 19 of ControlSystems/SoftwareTeam/IntroToPython


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

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/IntroToPython

    v18 v19  
    1111(A quick note...If you know all this already and are getting bored...CONGRATULATIONS! Here's a '''[https://www.youtube.com/watch?v=x94yTpv0T1k link]''' to some cute corgi puppy videos as your reward. Then, come back and dive into a later section. This is all self-paced.)
    1212
    13 We'll be starting with a programming language called [https://www.python.org/ 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.
     13We'll be starting with a programming language called [https://www.python.org/ 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.
    1414
    1515Python, like every programming language, has a structure.  Here are the big parts:
     
    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
    22 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"
     22Much 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 program that all programmers start with:  "Hello World"
    2323
    2424{{{
     
    2929This tells the computer to print, you guessed it, "Hello World"
    3030
    31 Now you try it: Do the following:
     31Now you try it...Do the following:
    3232
    33  1. Highlight all the text in the central area and hit back space (this'll we'll clear out all the sample code.)
    34  1. Then highlight the code above, right click and click {{{Copy}}.
    35  1. Go [https://www.tutorialspoint.com/execute_python3_online.php 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.
    36  1. Highlight all the code already in the center window.
    37  1. Right click and pick !{{{Paste}}}.  This should replace everything in the window with the contents above
    38  1. Click !{{{Execute}}} right above the center window.
     33 1. Then highlight the code above, right click and click {{{Copy}}.  This put the code on the clipboard so you can share it with another window.
     34 1. Go [https://www.tutorialspoint.com/execute_python3_online.php here].  This site is our code editor for training purposes, and its pretty cool.  You can put code in the center section, and click '''Execute''' to run it.  The lower window shows the results.
     35 1. On that page, highlight all the code already in the center window.
     36 1. Right click and pick !{{{Paste}}}.  This should replace everything in the window with the contents above.
     37 1. Finally click !{{{Execute}}} right above the center window to run the program.
    3938
    40 (From now on, we'll call this step "''Go [https://www.tutorialspoint.com/execute_python3_online.php here] and run the code above''".  (Just come back to these steps  if you forget how to do this).
     39(From now on, we'll call this step "''Go [https://www.tutorialspoint.com/execute_python3_online.php here] and run the code above''".  (Just come back to these steps if ever you forget how to do that).
    4140
    42 After a bit, you'll  be rewarded with the message in the bottom window:
     41After a bit, you'll be rewarded with the following message in the bottom window:
    4342
    4443{{{
    4544Hello World!
    4645}}}
    47 And, boom, now you're a programmer.  The rest is just more details.   This is the end of Lesson 1.
     46The program told the computer to print the value of variable a on the screen.
    4847
    49 = Lesson 2:  More Things To Do =
     48And, boom, now you're a programmer.  The rest of learning how to code is just more details.  This is the end of Lesson 1.
     49
     50
     51= Lesson 2: Making decisions  =
    5052In 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".
    5153
     
    6365print("Hope you had a good time\n")
    6466}}}
    65 It should print:
     67This program uses a IF-THEN-ELSE statement to tell the computer to make a decision in your program, and you'll do it all the time.  ''Go [https://www.tutorialspoint.com/execute_python3_online.php here] and run the code above.''  It should print:
    6668
    6769{{{
    68 Welcome to our store                                                                                                                                                                                                                                   
    69                                                                                                                                                                                                                                                        
    70 We're here! Come on in                                                                                                                                                                                                                                 
    71                                                                                                                                                                                                                                                        
    72 Our ours are 11:00am-11:52am                                                                                                                                                                                                                          
    73                                                                                                                                                                                                                                                       
    74 Hope you had a good time                                                                                                                                                                                                                               
    75                                                                                                                                                                                                                                                           
     70Welcome to our store
     71                                                                                                                                                                                                                                 
     72We're here! Come on in
     73                                                                                                                                                                                                                               
     74Our ours are 11:00am-11:52am
     75                                                                                                                                                                                                                         
     76Hope you had a good time 
     77                                                                                                                                                                                                                                
    7678}}}
    77 Three things to note:
    7879
    79  1. 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.
    80  1. 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.
     80Three things to note about this program:
     81
     82 1. The double equals in location=='HERE' on the first line.   It is called an ''expression''.   In this case it is an equals expression (other kinds of comparisons in python are listed here) .  It returns ''True'' when the condition evaluates to ''True'', otherwise its ''False''.   Note on that line the word 'HERE' is in quotes.  That's to indicate its a string (a text value) not some variable (which doesn't exist).    If you used ''='' instead of ''=='' it wouldn't work as you expected because it would be a variable assignment.   If a IF-THEN statement ever behaves in an unexpected way, check for this problem first, its a common error.
     83 1. Spacing and indents are important in Python.  the line "We're here..." and the line below it are indented to indicate they are part of a statement ''block'' that runs 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.
    8184 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.
    8285
    83 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.
     86Now, in the editor change the ''condition'' to location=='Poughkeepsie, NY' and 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.
    8487
    85 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.
     88If you're writing your own code, you want to the skip the else, just omit the word else: and the indented block below it.  For example:
    8689
     90{{{
     91medianSalary=98260 #Median annual salary of a software engineer in the US
     92salary=100000
     93if salary > medianSalary:
     94   print("Whohoo! Dinner's on you!")
     95}}}
     96"
     97
     98
     99 
    87100Here's a couple other control structures that you should know
    88101