cheatSheet - Rose

advertisement
Powers of two:
Place
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Power
2^0
2^1
2^2
2^3
2^4
2^5
2^6
2^7
2^8
2^9
2^10
2^11
2^12
2^13
2^14
2^15
2^16
2^17
2^18
2^19
2^20
2^21
2^22
2^23
2^24
2^25
2^26
2^27
2^28
Value
1
2
4
8
16
32
64
128
256
512
1,024
2,048
4,096
8,192
16,384
32,768
65,536
131,072
262,144
524,288
1,048,576
2,097,152
4,194,304
8,388,608
16,777,216
33,554,432
67,108,864
134,217,728
268,435,456
Present? Present?
Present?
Present?
Binary
Dec
0100 0001
0100 0010
0100 0011
0100 0100
0100 0101
0100 0110
0100 0111
0100 1000
0100 1001
0100 1010
0100 1011
0100 1100
0100 1101
0100 1110
0100 1111
0101 0000
0101 0001
0101 0010
0101 0011
0101 0100
0101 0101
0101 0110
0101 0111
0101 1000
0101 1001
0101 1010
Glyph
65 A
66 B
67 C
68 D
69 E
70 F
71 G
72 H
73 I
74 J
75 K
76 L
77 M
78 N
79 O
80 P
81 Q
82 R
83 S
84 T
85 U
86 V
87 W
88 X
89 Y
90 Z
Color table
Decimal Binary
0
1
2
3
00
01
10
11
Color
Python cheat sheet.
Control-N to make a new program. F5 to run it. Save source
file as “someName.py”.
keywords strings variables
<otherCode>
number
---Printing: the print function.
print(<expression>)
print(stringToPrint)
Examples:
print(“Hello, World!”)
print(3+4)
--Defining variables
varName = <expression>
Examples:
b = a+2
a = a+1
--Conditions:
== is the equality operator. = is the assignment operator.
if <condition>:
<expressions>
elif <otherCondition>:
<expressions>
else:
<expressions>
Example:
if a>b:
print(“a is greater than b!”)
a=a-5
elif a<b:
print(“a is less than b.”)
else:
print(“It seems a equals b.”)
if(a == b):
print(“Yes, yes it was.”)
print(“I finished the if block!”)
Remember the indenting. Also, you don’t need the elif and
else.
--Input:
String input:
input(prompt)
Number input:
eval(input(prompt))
---
Looping:
for varName in range(rangeSize):
<expressions>
Example:
for i in range(10):
print(i)
Remember the indents! i will take values 0,1,2,… 7,8,9. (It
doesn’t hit 10.)
Getting ready for graphics:
from zellegraphics import *
Making a window:
windowName = GraphWin(title, width, height)
example:
win = GraphWin(“My Window”, 200,200)
Closing a window:
windowName.getMouse()
windowName.close()
making a point:
pointName = Point(xPos, yPos)
Example:
pt = Point(100,100)
Drawing a point:
pointName.draw(windowName)
Example:
pt.draw(win)
General graphics program shape:
from zellegraphics import *
win = GraphWin(“Pointy”,200,200)
pt = Point(100,100)
pt.draw(win)
xinit = 0
yinit = 100
for iters in range(100):
xinit= xinit+1
if iters < 50:
yinit = yinit + 1
else:
yinit = yinit – 1
pt = Point(xinit,yinit)
pt.draw(win)
win.getMouse()
win.close()
Zellegraphics reference:
Generating Colors
GraphWin Objects
Colors are indicated by strings. Most normal colors such
as 'red', 'purple', 'green', 'cyan', etc. should be available.
Many colors come in various shades, such
as'red1', 'red2','red3', 'red4', which are increasingly darker
shades of red.
A GraphWin object represents a window on the screen where
graphical images may be drawn. A program may define any
number of GraphWins. A GraphWin understands the following
methods:
GraphWin(title, width, height)
Constructs a new graphics window for drawing on
the screen. The parameters are optional, the default
title is ``Graphics Window,'' and the default size is
200 x 200.
setBackground(color)
Sets the window background to the given color. The
initial background is gray. See Section 5.8.5 for
information on specifying colors.
close()
Closes the on-screen window.
getMouse()
Pauses for the user to click a mouse in the window.
Graphics Objects
The module provides the following classes of drawable
objects: Point, Line, and Circle. All objects are initially created
unfilled with a black outline. All graphics objects support the
following generic set of methods:
setFill(color)
Sets the interior of the object to the given color.
setOutline(color)
Sets the outline of the object to the given color.
A complete program:
win = GraphWin("Title text!",400,600)
win.setBackground('linen')
for i in range(100):
pt = Point(i,100)
pt.setOutline('blue')
pt.draw(win)
pt1 = Point(252,450)
pt2 = Point(102,205)
line1 = Line(pt1,pt2)
line1.setWidth(20)
line1.setOutline('red')
line1.draw(win)
win.getMouse()
line1.undraw()
setWidth(pixels)
Sets the width of the outline of the object to this
many pixels. (Does not work for Point.)
draw(aGraphWin)
Draws the object into the given GraphWin.
cir = Circle(pt1,60)
cir.setFill('green')
cir.draw(win)
undraw()
Undraws the object from a graphics window. This
produces an error if the object is not currently drawn.
Point Methods
Point(x,y)
Constructs a point having the given coordinates.
getX()
Returns the x coordinate of a point.
getY()
Returns the y coordinate of a point
Line Methods
Line(point1, point2)
Constructs a line segment from point1 to point2.
Circle Methods
Circle(centerPoint, radius)
Constructs a circle with given center point and radius.
Rectangle Methods
Rectangle(point1, point2)
Constructs a rectangle having opposite corners
at point1 and point2.
pt3 = Point(300,400)
pt4 = Point(400,500)
rect = Rectangle(pt3,pt4)
rect.draw(win)
win.getMouse()
win.close()
Download