Changes between Version 43 and Version 44 of ControlSystems/SoftwareTeam/IntroToPython
- Timestamp:
- Sep 4, 2017, 6:56:17 PM (8 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
ControlSystems/SoftwareTeam/IntroToPython
v43 v44 15 15 Python, like every programming language, has a structure. Here are the big parts: 16 16 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. 19 18 * ''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. 20 20 * ''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. 21 21 … … 27 27 print(a) 28 28 }}} 29 30 29 This tells the computer to print, you guessed it, "Hello World" 31 30 … … 45 44 Hello World! 46 45 }}} 47 48 46 The program told the computer to print the value of variable a on the screen. 49 47 50 48 And, boom, now you're a programmer. The rest of learning how to code is just more details. This is the end of Lesson 1. 51 49 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 = 51 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 (shows on the screen the value of its argument `a`, where `a` is assigned the value "Hello World". 54 52 55 If you want to do more , we need to introduce control structures.53 If you want to do more in a program and actually make decisions in your code, we need to introduce control structures. 56 54 57 55 == If-Then-Else == … … 69 67 print("Hope you had a good time") 70 68 }}} 71 72 69 ''Go [https://www.tutorialspoint.com/execute_python3_online.php here] and run the code above.'' It should print: 73 70 … … 79 76 Our ours are 11:00am-11:52am 80 77 81 Hope you had a good time 78 Hope you had a good time 82 79 }}} 83 84 80 Three things to note about this program: 85 81 … … 93 89 if location == 'Poughkeepsie, NY': 94 90 }}} 95 96 91 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. 97 92 … … 105 100 print("Whohoo! Dinner's on you!") 106 101 }}} 107 108 102 ''Go [https://www.tutorialspoint.com/execute_python3_online.php here] and run the code above.'' It should print: 109 103 … … 111 105 112 106 }}} 113 114 107 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. 115 108 … … 159 152 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? 160 153 161 == Bringing it all Together... ==154 == Here a Control Structure, There a Control Structure, Everywhere a Control Structure... == 162 155 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: 163 156