Changes between Version 26 and Version 27 of ControlSystems/SoftwareTeam/IntroToPython
- Timestamp:
- Sep 4, 2017, 5:49:45 PM (8 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
ControlSystems/SoftwareTeam/IntroToPython
v26 v27 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 you want to do more, we need to introduce control structures. Take the program below. 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. 55 56 56 57 {{{ … … 108 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) 109 110 110 Here's a couple other control structures that you should know 111 Here's a couple other control structures that you should know: 111 112 112 For Loop: This one repeats over a variable. It's good for counting try running this one:; 113 ==FOR Loop== 114 The FOR loop control structure runs code repeatly repeats over a variable. Try running this one: 115 {{{ 116 fruits = ['banana', 'apple', 'mango'] 117 for fruit in fruits: 118 print("Current fruit :", fruit) 119 print("That's alotta fruit!") 120 }}} 113 121 114 For (Potato 122 It should output: 115 123 116 While: The more charming cousin of the for loop, this one repeats 124 {{{ 125 Current fruit : banana 126 Current fruit : apple 127 Current fruit : mango 128 }}} 129 130 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!" 131 132 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 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. 135 136 ==While Loop== 137 138 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. 117 139 118 140 ----