Changes between Version 28 and Version 29 of ControlSystems/SoftwareTeam/IntroToPython
- Timestamp:
- Sep 4, 2017, 5:58:11 PM (8 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
ControlSystems/SoftwareTeam/IntroToPython
v28 v29 52 52 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". 53 53 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. 54 If you want to do more, we need to introduce control structures. 55 56 == If-Then-Else == 57 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. You'll use these all the time. 56 58 57 59 {{{ … … 66 68 print("Hope you had a good time") 67 69 }}} 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: 69 72 70 73 {{{ … … 84 87 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. 85 88 86 Now, in the editor change the line with the if ''expression'' to89 Now, in the editor replace the line with the if ''expression'' with 87 90 {{{ 88 91 if location == 'Poughkeepsie, NY': 89 92 }}} 93 90 94 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. 91 95 … … 107 111 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. 108 112 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.) 110 114 111 Here's a couple other control structures that you should know: 115 Now for a couple of other control structures that you should know... 112 116 113 117 == For Loop == 114 The For loop control structure runs code repeat ly repeats over a variable. Try running this one:118 The For loop control structure runs code repeats over a variable. Try running this one: 115 119 {{{ 116 120 fruits = ['banana', 'apple', 'mango'] … … 132 136 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. 133 137 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. 138 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. 139 135 140 136 141 == While Loop == … … 138 143 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. 139 144 145 {{{ 146 x=0 147 while (x<100): 148 x=x+1 149 print(x) 150 }}} 151 140 152 ---- 141 153 [[[wiki:ProgrammingReserve Programming]Archive | Programming Archive]]