xin liu. * use python 3.0 or later version * major differences from earlier versions * interactive...

6
Xin Liu * CPSC 231 Tutorial Jan 16, 2013

Upload: andrew-ward

Post on 03-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Xin Liu. * Use python 3.0 or later version * Major differences from earlier versions * Interactive mode * For quick experiments * “python3” to enter *

Xin Liu

*CPSC 231 TutorialJan 16, 2013

Page 2: Xin Liu. * Use python 3.0 or later version * Major differences from earlier versions * Interactive mode * For quick experiments * “python3” to enter *

*Getting started with python

*Use python 3.0 or later version

*Major differences from earlier versions

* Interactive mode

* For quick experiments

* “python3” to enter

* print(’python programming is fun!’)

* “Ctl+D” to quit

*Script mode

* For regular programs, that can be executed repeatedly

* Edit a program (with gedit)

* python3 myprogram.py

Page 3: Xin Liu. * Use python 3.0 or later version * Major differences from earlier versions * Interactive mode * For quick experiments * “python3” to enter *

*The first python program

*In gedit, create the first.py containing the following program

*In terminal, run the program by typing

print(‘hello world!’)print(‘this is my first python program’)Print(‘it’s funny’)

Python3 first.py

Page 4: Xin Liu. * Use python 3.0 or later version * Major differences from earlier versions * Interactive mode * For quick experiments * “python3” to enter *

*Turtle graphics

*http://docs.python.org/3/library/turtle.html

(0, 0)

(100, 100)

Cartesian plane

Page 5: Xin Liu. * Use python 3.0 or later version * Major differences from earlier versions * Interactive mode * For quick experiments * “python3” to enter *

*Turtle commands

* import turtle

* clear()

* title(‘window name’)

* goto(x, y)

* forward(distance)

* back(distance)

* left(angle)

* right(angle)

* circle(radius)

* begin_fill()

* end_fill()

* pencolor(r, g, b), 0 <= r, g, b <= 1

* fillcolor(r, g, b)

* write(‘a string’)

Page 6: Xin Liu. * Use python 3.0 or later version * Major differences from earlier versions * Interactive mode * For quick experiments * “python3” to enter *

*A drawing program

*Just to get a feeling of programming

from turtle import *color('red', 'yellow')begin_fill()while True: forward(200) left(170) if abs(pos()) < 1: breakend_fill()done()