Changes between Version 69 and Version 70 of ControlSystems/SoftwareTeam/IntroToPython
- Timestamp:
- Sep 4, 2017, 10:11:02 PM (8 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
ControlSystems/SoftwareTeam/IntroToPython
v69 v70 88 88 if location == 'Poughkeepsie, NY': 89 89 }}} 90 91 90 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. 92 91 … … 119 118 print("That's alotta fruit!") 120 119 }}} 121 122 120 It should output: 123 121 … … 190 188 The solution is [wiki:ProgrammingAnswerPotatoExercise here]. This concludes Lesson 2. 191 189 192 193 190 = Lesson 3: Functions = 194 191 So are you tired of just printing stuff yet? In the last lesson, we introduced you to all sort of ways to make decisions and loop over code to make your program easier to read and shorter too. … … 233 230 234 231 }}} 235 236 232 Try running these yourself, making sure the results are as expected. 237 233 238 You can even combine these functions with control structures to do some interesting stuff, and even nest them. For example, try running this (entering values for x and why when prompted. Try putting in non-numeric or decimal values too): 234 You can even combine these functions with control structures to do some interesting stuff, and even nest them. For example, try running this (entering values for x and why when prompted. Try putting in non-numeric or decimal values too): 239 235 240 236 {{{ … … 252 248 print("Aw, its only",z) 253 249 }}} 254 255 250 '''But wait, there's more'''...you can create your own functions. That way, if you come up with cool way to do something you can reuse it and don't have to repeat yourself as much. For instance: 256 251 … … 266 261 singHappyBirthday(6,['Milla', 'Emma']) # now use it. 267 262 }}} 268 269 263 Run this one. What does it output? A lovely happy birthday song for two kids? Now imagine if you had to write a program to print out a happy birthday for every single person in your grade without it. It'd get pretty ugly, fast. 270 264 271 The keyword `def` is used in Python to indicate a custom function, and you have to ''def''ine it in your program before you use it. This function takes an argument of an age, and second argument of a list of names. The "singHappyBirthday" line at the end actually ''calls'' or runs the custom function, with the two arguments it needs. 265 The keyword `def` is used in Python to indicate a custom function, and you have to ''def''ine it in your program before you use it. This function takes an argument of an age, and second argument of a list of names. The "singHappyBirthday" line at the end actually ''calls'' or runs the custom function, with the two arguments it needs. 272 266 273 267 A function can return a value too, by using the Python keyword `return` for example: … … 284 278 print(isGokuPowerfulEnough) 285 279 }}} 286 287 280 Now your turn... Modify the 'That's-a big number" exercise above, adding a custom function called called inputNumber to input one number, and use it instead of repeating the while-not stuff. Run it and verify the output is the same. Much easier to read, no? 288 281 282 By using functions, you can perform powerful operations that are built in the to the language, or someone created. These function in turn can use other functions and build on top of them to make more advanced programming commands. This concludes lesson 3. 283 289 284 Here's the [[ThatsaBigNumberSolution|solution]] 290 285 291 292 ---- 286 == Lesson 4: Libraries == 287 Now that you seen the basics of the Python language, and have created your own functions. Its time to see what else is out there. It turns out there are A LOT of libraries out there for python that do pretty much everything you want, from advance machine intelligence, to 3D graphics, to yes controlling robots. These libraries were created by others and are offered for free, and others may be offered at a cost. 288 289 You can find these libraries by googling for with the term "<subject> python library". For example "spell check python library". This turns up [https://pythonhosted.org/pyenchant/ PyEnchant] which describes how to use and access the library. There are probably others. 290 291 To use it in code, you use the !`import` keyword in Python like so: 292 293 {{ 294 295 #!python 296 297 import enchant 298 299 d=enchant.Dict("en_US") # Selects the US english dictionary 300 301 bad=check("Helo") 302 303 good=check("Hello") 304 305 }} 306 307 It would be a enormous effort to create your own spell check library, so this saves a lot of time. We'll be using this when we program robots because a lot of work has already been done. 308 293 309 [[[wiki:ProgrammingReserve Programming]Archive | Programming Archive]]