An Introduction to Python
District School Board of Niagara
Imperial Oil Summer Institute
• IDLE: a Python IDE
Most Important Feature in a
• The symbol to start documentation in
Python is ‘#’
• Some programs are compiled, meaning
that the code you write is translated to
something the computer understands.
• Others are interpreted, meaning that
your code is read, as is, by another
program that then does what the
• Python is an interpreted language.
IDLE: A Python IDE
• Free IDE
• Has both
• Allows you to try out an expression with
• Output is the result of the expression
• Play with the interactions window, see
what the results are.
• Exploration is a good way to learn, and
this window provides an easy
opportunity to explore.
IDLE: Writing Programs
• Programs can be written in files,
saved, and run.
• The results are produced in the
• The output keyword in Python is “print”. print
will output anything, Strings, numbers, etc.
• Note the difference between simply putting
“Hello” in the interactions window, and
putting print “Hello”
print 4 + 6 10
print "Hello" Hello
Activity: First Program
• Write a program (not in the interactions
window, in a separate file) that prints
the phrase “Hello World” on the screen
• Run this program (Run -> Run Module)
and see the output produced in the
• String concatenation is the “+” symbol
>>> 'Hello ' + 'World'
“ or ‘ ?
• Python allows you to use either single
or double quotes for Strings.
• This prevents needing special
characters if you want to use a quote in
>>> print 'This contains "double quotes"'
This contains "double quotes"
>>> print "And here are 'single quotes' inside"
And here are 'single quotes' inside
• The standard arithmetic operations: +, -,
/, *, % (remainder)
• Note: if you divide two integers, the
result will be an integer
• More math functions like ceiling and
floor are in the math module (see
• Explore the difference between
5.0 / 2
• Python variables >>> x = 6
don’t carry type. >>> print x + 3
You can assign a >>> x = "Hello"
>>> print x
variable to be a Hello
string, then assign >>> print x + " world"
it to an integer
• The method raw_input(string) returns a
string the user has typed in
• The parameter to raw_input is the
prompt given to the user.
x = raw_input("<TYPE SOMETHING>")
print "The user typed: " + x
<TYPE SOMETHING>This is my input
The user typed: This is my input
• The string input can be converted to
other types using conversion methods
like int(string) and float(string)
x = raw_input("<TYPE A NUMBER> ")
y = int(x)
print y * 2
<TYPE A NUMBER> 11
Activity: Input Conversion
• Write a program that reads in a
temperature in Fahrenheit and converts
it to Celsius
• C = (F - 32) * 5 / 9
• Python doesn’t use brackets to define
blocks of code
• Indentation tells the program where a
• If a program isn’t properly indented
• The loop i=0
continues while i < 10 :
iterating as long i=i+1
as the condition print "done!"
following “while” Program
remains true 9
Activity: Importance of
• Create a program that uses a loop, like
the previous example.
• Now remove the indentation and try
running the program
• “==“ is used for equality testing
• Inequality is “!=“
• Inequalities are straight forward: <=, >=,
>>> print 8 < 15
>>> print 9 != 7
• Must have one if clause
• Followed by 0 or more elif (else if)
• Followed by 0 or 1 else clause
• Notice what
x = raw_input("[input please]")
x = int(x) happens when
if x < 10 : you remove the
elif x < 20 : indentation.
elif x < 30 :
else : >>>
print "HUGE!" [input please]21
print "Done" Big
• Write a program that reads in a number
and determines if that number is even
Use in Computer
• Free IDE
• The interactions window allows students to
quickly test out ideas.
• Lack of brackets keeps code uncluttered.
• Students *must* indent their code properly or
it won’t function properly.
• Lightweight syntax prevents students from
becoming lost in the picky details of a