Python
Python
http://www.python.org/
Python Vocab
• Program – A larger file of code that may contain one or
more functions.
• Variable - names that you can assign values to, allowing
you to reuse them later on.
E.g.: x = 1 or msg = “Hi, I’m a message!”
• Comments – These are notes ignored by the computer. In
Python, comments start with a hash mark (#) and end at
the end of the line.
E.g.: >>> x + y #both variables store user
input
• Operators – Mathematical symbols, like +, -, *, and /, but
also ** (for exponents).
Python Vocab
• Keyword – Words with meaning/purpose in
Python. E.g. “and”, “print”, and “if”.
• Function – A chunk of code that performs an
action. Functions have names and are reusable.
Kinds: built-in ones, and “user-defined” ones.
• Expression – Statements that produce values.
Examples include 3 + 5, "Hello world!“.
• Error – When your program has a problem, the
command area will provide an error message.
What is JES?
http://docs.python.org/glossary.html#glossary
Calling Functions
main()
will give the output
Hello world! :
- There is no colon when calling a function!
- Use colons with “def”.
• Try this yourself
Output
E.g. Print:
print "Hello world!"
will give the output
Hello world!
Similar formatting, different output
• print "Hello", "world", "!"
will output
Hello world !
then outputs
John Doe
Ex. Try it with Numbers!
num = requestNumber("Enter a number:")
print "Your number:", num
print "Your number squared:", num*num
Click anywhere in
the picture that
just popped up
and you’ll see
the X & Y
coordinates of
the pixel.
Pictures
Def negative():
file = pickAFile()
pic = makePicture(file)
for px in getPixels(pic):
red = getRed(px)
green = getGreen(px)
Blue = getBlue(px)
negColor = makeColor(255-red, 255-green, 255-blue)
setColor(px, negColor)
repaint(pic)
show(pic)
Mirror, Mirror on the Wall
• We can use Python to manipulate more than
the colors of the picture. We can do this:
Code, Code on the Screen
#Starting with pseudocode
mirrorVertical():
Get a picture
Identify its middle.
in every row
replace each column with the at the same
distance from the middle until you reach the
middle
apply changes
show the picture
Code, Code on the Screen
def mirrorVertical():
file = pickAFile()
pic = makePicture(file)
mirrorpoint = getWidth(pic)/2
for y in range(1, getHeight(pic)):
for xOffset in range(1, mirrorpoint):
pright = getPixel(pic, mirrorpoint+xOffset, y)
pleft = getPixel(pic, mirrorpoint-xOffset,y)
c = getColor(pleft)
setColor(pright, c)
repaint(pic)
show(pic)
Extra Challenge
• As you can see, the right side mirrored the left
side (and that’s why the creature has two
heads).
• Can you change your code
So it does the opposite?
(i.e. let the left side mirror
the right side)
Reversed Mirroring
• This is the result I got:
Are the soldiers, clouds, big creature
actually there?
The Green Screen
• Placing an object (foreground) in a
background of our choice.
The Green Screen
• Tell the computer look at each pixel, and see if
its red and blue values are less than its green
value.
• If (red+blue < green) then that pixel is likely to
belong to the green screen. Now tell the
computer to get the pixel at the same location
from the background and paint it on the green
screen.
The Green Screen
So in a way, you are actually placing the
background on the foreground, not the other
way around.
Also, it could be a blue screen. (How will the
code change?)
The Green Screen Code
• How do you think the code should look like?
The Green Screen Effect
You do not get perfect
results all the time…
Our Journey Ends Here
This presentations was based on the CS101 Guide to Python and JES
(http://www.cs.bu.edu/courses/cs101b1/jes)