Changes between Version 69 and Version 70 of ControlSystems/SoftwareTeam/IntroToPython


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

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/IntroToPython

    v69 v70  
    8888if location == 'Poughkeepsie, NY':
    8989}}}
    90 
    9190and 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.
    9291
     
    119118print("That's alotta fruit!")
    120119}}}
    121 
    122120It should output:
    123121
     
    190188The solution is [wiki:ProgrammingAnswerPotatoExercise here].   This concludes Lesson 2.
    191189
    192 
    193190= Lesson 3:  Functions =
    194191So 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.
     
    233230
    234231}}}
    235 
    236232Try running these yourself, making sure the results are as expected.
    237233
    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): 
     234You 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):
    239235
    240236{{{
     
    252248  print("Aw, its only",z)
    253249}}}
    254 
    255250'''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:
    256251
     
    266261singHappyBirthday(6,['Milla', 'Emma'])  # now use it.
    267262}}}
    268 
    269263Run 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.
    270264
    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. 
     265The 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.
    272266
    273267A function can return a value too, by using the Python keyword `return` for example:
     
    284278print(isGokuPowerfulEnough)
    285279}}}
    286 
    287280Now 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?
    288281
     282By 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
    289284Here's the [[ThatsaBigNumberSolution|solution]]
    290285
    291 
    292 ----
     286== Lesson 4: Libraries ==
     287Now 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
     289You 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
     291To use it in code, you use the !`import` keyword in Python like so:  
     292
     293{{
     294
     295#!python
     296
     297import enchant
     298
     299d=enchant.Dict("en_US")  # Selects the US english dictionary
     300
     301bad=check("Helo")
     302
     303good=check("Hello")
     304
     305}}
     306
     307It 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
    293309[[[wiki:ProgrammingReserve Programming]Archive | Programming Archive]]