Title: An Introduction to Python
1An Introduction to Python
Michael DiRamio District School Board of Niagara
- Imperial Oil Summer Institute
- 2006
2Summary
- IDLE a Python IDE
- Output
- Arithmetic
- Variables
- Input
- Looping
- Selection
3Most Important Feature in a Language
Documentation
- The symbol to start documentation in Python is
4Interpreted Programming Languages
- 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 program specifies. - Python is an interpreted language.
5IDLE A Python IDE
- Free IDE download at www.python.org
- Has both interactions window and program files
Interactions Window
6IDLE Interaction
- Allows you to try out an expression with
immediate results. - Output is the result of the expression
7Activity Exploration
- 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.
8IDLE Writing Programs
- Programs can be written in files, saved, and run.
- The results are produced in the interactions
window.
9Output
- 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
gtgtgt 10 Hello
print 4 6 print "Hello"
program
output
10Activity 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 -gt Run Module) and see the
output produced in the interactions window
11Concatenation
- String concatenation is the symbol
gtgtgt 'Hello ' 'World' 'Hello World'
12 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 your string.
gtgtgt print 'This contains "double quotes"' This
contains "double quotes" gtgtgt print "And here are
'single quotes' inside" And here are 'single
quotes' inside
13Math
- 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 intermediate Python)
14Activity Division
- Explore the difference between
- 5 / 2
- and
- 5.0 / 2
15Variables
- Python variables dont carry type. You can assign
a variable to be a string, then assign it to an
integer later
gtgtgt x 6 gtgtgt print x 3 9 gtgtgt x "Hello" gtgtgt
print x Hello gtgtgt print x " world" Hello world
16Input
- 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("ltTYPE SOMETHINGgt") print "The user
typed " x
Program
gtgtgt ltTYPE SOMETHINGgtThis is my input The user
typed This is my input
Output
17Input
- The string input can be converted to other types
using conversion methods like int(string) and
float(string)
x raw_input("ltTYPE A NUMBERgt ") y
int(x) print y 2
Program
gtgtgt ltTYPE A NUMBERgt 11 22
Output
18Activity Input Conversion
- Write a program that reads in a temperature in
Fahrenheit and converts it to Celsius - C (F - 32) 5 / 9
19Indentation
- Python doesnt use brackets to define blocks of
code - Indentation tells the program where a section
ends - If a program isnt properly indented
20Loops
gtgtgt 0 1 2 3 4 5 6 7 8 9 done!
i 0 while i lt 10 print i i i
1 print "done!"
- The loop continues iterating as long as the
condition following while remains true
Program
Output
21Activity Importance of Indentation
- Create a program that uses a loop, like the
previous example. - Now remove the indentation and try running the
program
22Comparisions
- is used for equality testing
- Inequality is !
- Inequalities are straight forward lt, gt, lt, gt
gtgtgt print 8 lt 15 True gtgtgt print 9 ! 7 True
Interactions Window
23Selection Statements
- if/elif/else
- Must have one if clause
- Followed by 0 or more elif (else if) clauses
- Followed by 0 or 1 else clause
24Selection Statements Example
- Notice what happens when you remove the
indentation.
x raw_input("input please") x int(x) if x lt
10 print "Small" elif x lt 20 print
"Medium" elif x lt 30 print "Big" else
print "HUGE!" print "Done"
gtgtgt input please21 Big Done
Program
Output
25Activity Selection
- Write a program that reads in a number and
determines if that number is even or odd.
26Use in Computer Programming
- 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
wont function properly. - Lightweight syntax prevents students from
becoming lost in the picky details of a language.