Pages

Tuesday, January 31, 2017

Python: Modules and TurtleWorld

 Most programming languages include modules in them, like Python. “Modules [are] a file that contains a collection of related functions” (Downey 24). For example, there is already a built-in module in Python called “math”. The math module imports lots of functions into the program so that you can reference them in your code. An example of this would be when you import the math function, you can access difference new functions such as square roots, pi, and logarithms.



In the book “Think Python, Chapter 4” it tells to download a package called “Swampy”. Packages are just a collection of modules that you can install, since modules are collections of new functions, packages are in essence adds lots of new functions into the programming language (Downey 37). In Swampy there is a module that is called TurtleWorld. Using this you can create a “TurtleWorld” which is a window where a “Turtle” will draw what you tell it to draw. For example in the code below, the first line imports all the functions from the TurtleWorld module from the Swampy package. Then the next lines in the script make a TurtleWorld called world, and a Turtle called bob. Using this we can use bob and the TurtleWorld to draw a square and other shapes as we want. The “fd” function moves the Turtle “bob” forward 100, and the “lt” function turns him to the left. Repeat this 3 more times and we have a square.




From this experience, I was surprised the capabilities of Python and what it can accomplish, specifically with the TurtleWorld modules.
 Question: What are some things that you were surprised you can accomplish?
Downey, Allen B. Think Python: How to Think like a Computer Scientist. Sebastopol: O'Reilly Media, 2015. Print

0 comments:

Post a Comment