Download Starting in Python

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Intro to Computer Science: Starting in Python – Earl Bergquist
Starting in Python:
Line by line instructions creating your first Python Program
We will start by using Python version 2.7.2; on the PC’s go to Windows “Start” in the lower left, and
under All Programs look for Python 2.7 > IDLE (Python GUI). Be careful do NOT use version
Python 2.4 that is on the PC's for a later project. You may be able to drag a shortcut for 2.7 to your
desktop (let’s try that together in the lab).
Python has an easy to use Integrated Development Environment (IDLE):
1) The Python shell allows you to just type commands in and see the results, it an interpreter,
interpretting your commands one at a time. Notice the syntax highlighting to help you.
2) You can also create and edit a file with a bunch of commands (a program) that will be interpreted
in their sequence - this is much more powerful.
Let's try the Python Shell, see if we can guess how things works:
- How would we print Hello World to the screen?
>>> print "Hello World"
- Get the sum of the numbers 2 and 3?
>>> 2+3
- Add a comment?
>>> # this is a comment
Turtle Graphics!
Good introduction and the function details at: http://docs.python.org/library/turtle.html , notice all the
functions available for our Turtle graphics, similar to the blocks we used in Scratch to make the
geometric shapes.
To start we first need to include the turtle library of functions:
>>> from turtle import *
Now we can try some functions like:
>>> color("red")
>>> forward(50)
>>> left(90)
>>> forward(50)
Create a Program File:
tedious, so lets put those commands in a file, creating our first Python Program.
In IDLE go under the File menu item to > New Window and start typing in that new window.
To use the turtle functions you must:
- be using Python version 2.7.2 (instructions above, not version 2.4)
- Add the line "from turtle import *" to the top of your program
- Add the line "mainloop()" at the bottom of your program
To add a comment, (a non-executed message to describe what your code is doing) use a # sign in
the first place. It is good to add comments for the tricky part of the program.
# this is a comment
Intro to Computer Science: Starting in Python – Earl Bergquist
Add a few lines of statements to your program like these to make a small red square:
color("red")
forward(50)
left(90)
forward(50)
left(90)
forward(50)
left(90)
forward(50)
When you go to save your file it needs to have the suffix ".py" at the end, like "drawing.py" otherwise
it will not automatically run as a Python and you won't get syntax highlighting (colors and help)
Save your file to a reliable place like your folder in our class folder - S: > embergquist > Writeable >
Intro to CS > Class Period # > your folder
Running your Program file (.py):
To run it use the "Run" menu item "Run Module" (you can also use F5 key).
- Pay attention to the Python Shell Window - it will let you know if there is an error, and notice it does
a "Restart" every time you run your program.
- When it works correctly, a "Python Turtle Graphics" window will pop up and show the graphics
commands you had in your program! You have just completed your first program in Python!!
Add your own Functions:
Once you have something simple working, try adding your own function to do a group of commands,
using the syntax:
def <name>():
statements
Spaces (or tabs) in front of statements are important in Python, they indicate the grouping
(structure) of your code:
- Executed statements start in the first character position
- Statements for a new functions or loop have additional spaces or tabs in front to group them,
assigning them to the command above. i.e. you will be required to do the statement of you function.
To use (call) your new function, just make a statement with its name followed by “()”. For example if
your function was called drawsquare, you would call it with (with no spaces in front):
drawsquare()
If you want, you can download the simple "drawing.py" program to use as an example - don't forget to
save it in your own folder.
Your Assignment:
Then it’s Your turn to Draw a house using turtle graphics:
- Divide the house into functions like roof(), door(), walls()…
- Try to add more details like windows
- Be creative and make sure to save it in your folder of our class folder.