Changes between Version 71 and Version 72 of ControlSystems/SoftwareTeam/IntroToPython


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

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/IntroToPython

    v71 v72  
    6666print("Hope you had a good time")
    6767}}}
     68
    6869''Go [https://www.tutorialspoint.com/execute_python3_online.php here] and run the code above.''  It should print:
    6970
     
    7980Three things to note about this program:
    8081
    81  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-ELSE statement ever behaves in an unexpected way, check for this problem first, its a really common mistake.
    82  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.
    83  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. [[Here|https://www.quackit.com/python/reference/python_3_escape_sequences.cfm]] are some other escape sequences you can use.
     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-Else statement ever behaves in an unexpected way, check for this problem first, its a really common mistake.
     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.
     84 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. [[Here|https://www.quackit.com/python/reference/python_3_escape_sequences.cfm]] are some other escape sequences you can use.
    8485
    8586Now, in the editor replace the line with the if ''expression'' with
     
    8889if location == 'Poughkeepsie, NY':
    8990}}}
     91
    9092and 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.
    9193
    92 If you're writing your own code, you don't want to provide an ''else'', just omit the word else: and the indented block you want to run with it.  For example:
     94If you're writing your own code, you don't want to provide an ''else'', just omit the keyword `else:` and the indented block you want to run with it.  For example:
    9395
    9496{{{
     
    104106   
    105107}}}
    106 Yep, 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.
    107 
    108 (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.)   Now for a couple of other control structures that you should know...
     108
     109Yep, 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.
     110
     111(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 for yourself that the program doesn't see or run.  The robotics team wants you to put comments in your code '''a lot'''--it makes the code easier for others to read and understand.   And we like it when we can understand you.  Now for a couple of other control structures that you should know...
    109112
    110113== For Loop:  Stop Hitting Yourself! ==
     
    125128Current fruit : mango
    126129}}}
    127 This example loops over an ''array'' (a fancy word for a list) of fruits, and prints each value out, each line starting with the text "Current fruit:".  To understand this, pretend that the FOR loop has a big arrow in pointing to the first value in the fruits ''array''.  The first time it runs, this arrow,{{{fruit}}} is pointing to the first value in the ''array'', {{{banana}}}, which it then sets to be the value of the index variable ''fruit''.  After it prints the message, the for loop senses through arcane magic that there are other values in fruits, so it just moves the pointer to the second value and repeats.  After the third fruit, the loop knows there's nothing left so FOR loop exits and prints "That's alotta fruit!"
    128 
    129 Note 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.
    130 
    131 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.
     130
     131This example loops over an ''array'' (a fancy word for a list) of fruits, and prints each value out, each line starting with the text "Current fruit:".  To better understand what's going on here, pretend that the For Loop is basically a big arrow.  The first time the For loop runs, this arrow (called the ''index variable'') points to the first value in the ''array'', {{{banana}}}, which it then sets to be the value of the index variable ''fruit''.  After it prints the message, the for loop senses through arcane magic that there are other values in fruits, so it just moves the arrow to the second value and so on.  After the third fruit, the for loop knows there's nothing left in fruits so it stops and the prints the last line "That's alotta fruit!"
     132
     133Note that the "Current fruit..." print statement is indented.   Just like in the 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.
     134
     135For loops are handy if you want to avoid repeating yourself in code, and/or do the same thing over and over again.  The less code you write, after all, the less chance you can make a mistake.  Not to mention its easier to read.
    132136
    133137== While Loop:  One more time around the block, Alfred! ==
    134 A 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.
     138A While loop is the more charming cousin of the For loop, this one repeats as long as its expression evaluates to  ''True''.  This expression is evaluated at the start of each loop, so it possible to not even run the While loop at all if expression starts out False.
    135139
    136140{{{
     
    144148print("Good bye!")
    145149}}}
     150
    146151''Go [https://www.tutorialspoint.com/execute_python3_online.php here] and run the code above.''  It should print
    147152
     
    149154
    150155}}}
    151 Oh 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?
     156Since we're getting pretty far into these lessons, I'm not going to tell you the answer for this one.  You should be seeing a pattern by now.  What should it print?  The 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?
    152157
    153158== Compound Control Structures:  Here a Control Structure, There a Control Structure, Everywhere a Control Structure... ==
    154 Lastly, 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:
     159Lastly, you can combine all three of these in any sort of combination, and even nest them.  This is how you do real work in a program.  For example, try running this one:
    155160
    156161{{{
     
    165170print("score!")
    166171}}}
    167 What does it output?   See how the indenting works when nesting?  Note that `count+=1` is shorthand for `count=count+1` because not typing those extra characters __could save your life__.
    168 
    169 Now, modify the code to make it repeat the word potato each time based on the current value of count, and run it.  When you're done, the output should look like:
     172
     173What does it output?   See how the indenting works when nesting?  Note that `count+=1` is shorthand for `count=count+1` because not typing those extra characters __could save your life someday__.
     174
     175Now, your turn: modify the code to make it repeat the word potato each time based on the current value of count, and run it.  When you're done, the output should look like:
    170176
    171177{{{
     
    180186score!
    181187}}}
     188
    182189Yeah, this is a little harder, but I bet you can do it.  Ask a neighbor for help if you need to.  I'll give you three hints:
    183190
    184  1. You can tell a print to not end with a new line by adding an end="" to the print statement, like this `print("Im a teapot!",end="")`''. ''
    185  1. An empty print statement `print("")` just prints a new line, basically wrapping any line you're on.
     191 1. You can tell a print to not end with a new line by adding an end="" to the print statement like this `print("I'm a teapot!",end="")`''. ''
     192 1. An empty print statement `print("")` just prints a new line, basically the equivalent of having the program hit <enter>.
    186193 1. If you want to loop over a set of numbers, use a `range()`.  For example, `for x in range(0,5)` will make x go from 0 to 4 incrementing 1 each time it loops.  The first number is where x starts, and the second number is where x will stop __minus 1__.  Don't ask why---its just the way computers and programs work.  Don't believe me?  Fine. [https://softwareengineering.stackexchange.com/questions/110804/why-are-zero-based-arrays-the-norm Don't say I didn't warn you.]
    187194