1 / 10

Introduction to Python

Introduction to Python. Lesson 4 Modules, Objects and Methods. Learning Outcomes. In this lesson the students will: Use in-built libraries (modules) Create a Turtle object and a Screen object Call methods. Modules.

kirsi
Download Presentation

Introduction to Python

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Python Lesson 4 Modules, Objects and Methods

  2. Learning Outcomes • In this lesson the students will: • Use in-built libraries (modules) • Create a Turtle object and a Screen object • Call methods

  3. Modules • A module is a file that contains definitions, including variables and functions that you can use. • To use a module you just import it into your program.

  4. Turtle Module • We are going to use a Python module called turtle • The turtle module allows us to create simple graphics. We can tell the turtle to go forward, go right, go left etc. • Let’s look at some sample code…

  5. Example import turtle # allows us to use the turtles library win = turtle.Screen() # creates a graphics window alex = turtle.Turtle() # create a turtle named alex alex.forward(150) # move forward by 150 units alex.left(90) # turn by 90 degrees alex.forward(75) # move forward by 75 units

  6. Code Explained • The module allows us to create two new data types – Turtle and Screen • The Screen data type allows us to create a window we can draw in and the Turtle data type allows us to create a Turtle to draw with • The last 3 lines of code tell the turtle what to draw (a straight line, turn left, draw another straight line) • Let’s run the code

  7. Run the code • Copy and paste the code into IDLE and run it

  8. Other methods • We can also change the colours of the Screen and the line printed by the turtle • To change the background colour: win.bgcolor("lightgreen") • To change the colour and size of the line we draw alex.color("blue") alex.pensize(3) • Let’s add this to our program

  9. Note to Teacher • Lots of methods here that you could use: http://docs.python.org/3/library/turtle.html Also check out the eBook on Moodle: http://interactivepython.org/courselib/static/thinkcspy/

  10. Lesson Review • In this lesson …

More Related