Tutorial 2

CPSC 231 - T09 Introduction to Computer Science Afshin Esmaeili (2009) - CPSC 231 - T09 1 Afshin Esmaeili Email: a.e...

2 downloads 169 Views 1MB Size
CPSC 231 - T09 Introduction to Computer Science

Afshin Esmaeili (2009) - CPSC 231 - T09

1

Afshin Esmaeili Email: [email protected] Office: ICT 716 (AI Lab) OR ICT 716A (Bioinformatics Lab) CT Hours: Monday 14:00 - 16:00

Afshin Esmaeili (2009) - CPSC 231 - T09

2

Last Tutorial Vi and Unix commands Remotely login to lab machines Editing your Python program Shell mode vs. Script mode Type Checking Brief intro to Quickdraw Afshin Esmaeili (2009) - CPSC 231 - T09

3

QuickDraw Quickdraw is a language-independent graphics API (Application Programing Interface)

It is designed to help students and beginner programmers create complex user interfaces with ease. Afshin Esmaeili (2009) - CPSC 231 - T09

4

QuickDraw 1. Download QuickDraw http://pages.cpsc.ucalgary.ca/QuickDraw/index.html

Note: In windows it will be saved as quickdraw.zip 2. Rename it back to quickdraw.jar 3. In a terminal type: java -jar quickdraw.jar Note: Jar stands for Java Archive Afshin Esmaeili (2009) - CPSC 231 - T09

5

Programmatically Controlling Quickdraw with Python

Output from Python program is ‘piped’ to quickdraw program. Connect your Python code to Quickdraw: python myprog.py | java -jar quickdraw.jar

Afshin Esmaeili (2009) - CPSC 231 - T09

6

Lets run Quickdraw Blank window: $ java -jar quickdraw.jar

You have to make sure that quickdraw.jar is located in your current directory before running the above command. You also need to have java already installed.

Afshin Esmaeili (2009) - CPSC 231 - T09

7

Lets run Quickdraw Color Selection RGB color system

The amount or intensity of each color component (red, green and blue) must be a value between 0 and 255. Python Example: print “color 0 100 255” Afshin Esmaeili (2009) - CPSC 231 - T09

8

Coloring a Pixel

print “color 255 0 0” print “pixel 400 300” Afshin Esmaeili (2009) - CPSC 231 - T09

9

Drawing Shapes

print “color 0 200 0” print “ellipse 400 300 200 100” Afshin Esmaeili (2009) - CPSC 231 - T09

10

Variables Assignment Statement >>> message = “What’s up, Doc?” >>> n = 17 >>> pi = 3.14159

Afshin Esmaeili (2009) - CPSC 231 - T09

11

Variables The assignment operator ,=, should not be confused with an equal sign. Assignment operators link a name, on the left hand side of the operator, with a value, on the right hand side. >>> 17 = n Error! Afshin Esmaeili (2009) - CPSC 231 - T09

12

Variable names & keywords 1. Can be arbitrarily long 2. Can be both letters and numbers 3. Have to begin with letter 4. Case matters: Bruce and bruce are different 5. The underscore character (_) can appear in a name Afshin Esmaeili (2009) - CPSC 231 - T09

13

Variable names and keywords >>> 76trombones = “big parade” SyntaxError: invalid syntax >>> more$ = 1000000 SyntaxError: invalid syntax >>> class = “Computer Science 231” SyntaxError: invalid syntax

Afshin Esmaeili (2009) - CPSC 231 - T09

14

Variables Print Statement also works with variables >>> print message What’s up, Doc? >>> print n 17 >>> print pi 3.14159

Afshin Esmaeili (2009) - CPSC 231 - T09

15

Variables Variables also have types >>> type(message) >>> type(n) >>> type(pi) Afshin Esmaeili (2009) - CPSC 231 - T09

16

Keywords Python has thirty-one keywords and

del

from

not

while

as

elif

global

or

with

assert

else

if

break class

except import print exec

continue finally def

pass yeild

for

in

raise

is

return

lambda

try

Keywords define the language’s rules and structure Afshin Esmaeili (2009) - CPSC 231 - T09

17

Operators & Operands Operators are special symbols that represent computations 20 + 30 hour - 1 hour * 60 + minute minute / 60 5 ** 2 (5+9)*(15-7) Note: The asterisk (*) is the symbol for multiplication, and ** is the symbol for exponentiation

Afshin Esmaeili (2009) - CPSC 231 - T09

18

Operators & Operands >>> minute = 59 >>> minute / 60 0 The value of minute is 59, and 59 divided by 60 is 0.98333, not 0. The reason for the discrepancy is that Python is performing integer division.

>>> minute = 59.0 >>> minute / 60 0.983333333333328 Afshin Esmaeili (2009) - CPSC 231 - T09

19

Operation on strings Can not perform mathematica operations on strings “Hello”/123 message*”Hello” message-1 “15”+2

Afshin Esmaeili (2009) - CPSC 231 - T09

20

Operations on strings

>>> fruit = “banana” >>> baked_good = “nut bread” >>> print fruit + baked_good

Afshin Esmaeili (2009) - CPSC 231 - T09

21

Input Create a .py file with the following content: n = raw_input(“Please enter your name: “) print n n = input(“Enter a numerical expression: “) print n

Afshin Esmaeili (2009) - CPSC 231 - T09

22

References

“How to Think Like a Computer Scientist, Learning with Python” - Chapter 2. http://pages.cpsc.ucalgary.ca/~zongpeng/CPSC231/lectures/ch2.pdf “Quickdraw” http://pages.cpsc.ucalgary.ca/QuickDraw/index.html

Afshin Esmaeili (2009) - CPSC 231 - T09

23