Changes between Version 72 and Version 73 of ControlSystems/SoftwareTeam/IntroToPython


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

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/IntroToPython

    v72 v73  
    6666print("Hope you had a good time")
    6767}}}
    68 
    6968''Go [https://www.tutorialspoint.com/execute_python3_online.php here] and run the code above.''  It should print:
    7069
     
    8988if location == 'Poughkeepsie, NY':
    9089}}}
    91 
    9290and 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.
    9391
     
    106104   
    107105}}}
    108 
    109106Yep, 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.
    110107
     
    128125Current fruit : mango
    129126}}}
    130 
    131127This 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 better understand what's going on here, pretend that the For Loop is basically a big arrow.  The first time the For loop runs, this arrow (called the ''index variable'') points 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 arrow to the second value and so on.  After the third fruit, the for loop knows there's nothing left in fruits so it stops and the prints the last line "That's alotta fruit!"
    132128
     
    136132
    137133== While Loop:  One more time around the block, Alfred! ==
    138 A While loop is the more charming cousin of the For loop, this one repeats as long as its expression evaluates to  ''True''.  This expression is evaluated at the start of each loop, so it possible to not even run the While loop at all if expression starts out False. 
     134A While loop is the more charming cousin of the For loop, this one repeats as long as its expression evaluates to  ''True''.  This expression is evaluated at the start of each loop, so it possible to not even run the While loop at all if expression starts out False.
    139135
    140136{{{
     
    148144print("Good bye!")
    149145}}}
    150 
    151146''Go [https://www.tutorialspoint.com/execute_python3_online.php here] and run the code above.''  It should print
    152147
     
    170165print("score!")
    171166}}}
    172 
    173167What does it output?   See how the indenting works when nesting?  Note that `count+=1` is shorthand for `count=count+1` because not typing those extra characters __could save your life someday__.
    174168
     
    186180score!
    187181}}}
    188 
    189182Yeah, this is a little harder, but I bet you can do it.  Ask a neighbor for help if you need to.  I'll give you three hints:
    190183
     
    291284Here's the [[ThatsaBigNumberSolution|solution]]
    292285
    293 == Lesson 4: Libraries ==
    294 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.
    295 
    296 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.
     286== Lesson 4: Packages ==
     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 packages out there that contain custom functions for Python that do pretty much anything you'd want, from advanced machine intelligence, to 3D graphics, to yes, controlling robots.  These packages 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.  You can also go to the [[https://pypi.python.org/pypi|Python Package Index]].  What ever you do, make sure you use the correct version of the library for the version of Python you're using (in our case, Python 3)
    297290
    298291To use it in code, you use the !`import` keyword in Python like so:
    299292
    300 {{
    301 #!python
    302 import enchant
    303 d=enchant.Dict("en_US")  # Selects the US english dictionary
    304 bad=check("Helo") # Spell checks the given string, not correctly spelled, returns False
    305 good=check("Hello")  # Correctly spelled word returns True
    306 }}
    307 
    308 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.
    309 
    310 
     293{{{
     294#!python
     295import enchant
     296d=enchant.Dict("en_US")  # Selects the US english dictionary
     297bad=check("Helo") # Spell checks the given string, not correctly spelled, returns False
     298good=check("Hello")  # Correctly spelled word returns True
     299}}}
     300
     301It would be a enormous effort to create your own spell check library, so reusing one saves a lot of time.   We'll be using this when we program robots because a lot of the work we need to do to communicate with the parts of a robot has already been done.  And we want to spend time making robots do things, not figuring out how to communicate with then.
     302
     303Unfortunately, you can't include external libraries in your code in our web-based code runner and run them, so you'll have to use your imagination to see how this particular example works.  You'll use them a lot in the intermediate training as the year goes on.
     304
     305= Final Exam =
     306[[image:https://www.google.com/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&cad=rja&uact=8&ved=0ahUKEwjS5oD-gY3WAhUs94MKHbpwB4sQjRwIBw&url=http%3A%2F%2Fphi2010.blogspot.com%2F&psig=AFQjCNG8rEtQbDhBSL7ojOYD9kOuL-d0ew&ust=1504665503635530]]
     307Create a program that prints a random fortune cookie saying, drawing randomly from a different lists of nouns and verb phrases.  Make it sound suitable cryptic and wise.   
     308The program should prompt the user for the number of sayings they want output, validate that it is indeed a number, and print out that many. 
     309Use functions and control structures to make the code manageable.
     310Its best if you work in pairs for this exercise.  If you need help, refer to this lesson, the [[https://docs.python.org/3/index.html | Official Python 3 Documentation]], and don't hesitate to ask the mentors. 
     311 
     312
     313= What's Next =
     314Well that concludes the Software Introduction.  We welcome your feedback.  Software takes a lot of work and study to write well, and this is just a taste.  For those of you who select the Software team, we'll begin intermediate training with the Raspberry Pi, and show you how to write programs that use external libraries to communcation with the outside world through motors, sensors and the like. 
    311315----
    312316[[[wiki:ProgrammingReserve Programming]Archive | Programming Archive]]