Title: An Introduction to Python
1An Introduction to Python
Suzanne Dunford Mackenzie High School
- RCDSB Conference 2007
- Used and modified with the permission of Michael
DiRamio NDSB
2This Session
- Differentiated Instruction in the Computer class
- Python Part 1
- Character education and computers yes!
- Python Part 2
- Assessment and Evaluation discussion
- Python - Summary
3Differentiated Instruction in the Computer class
- How does a computer class lend itself to this
form of instruction?
4The use of Differentiated Instruction in ICS3M
and ICS4M
- Computer science classrooms lend themselves to
differentiated learning - Four ways to differentiate Instruction
- Content/Topic
- Process/Activities
- Product
- Individual Learning Styles
5Python Part 1
6Summary - Python Part 1
- Why Python?
- Textbooks
- Documentation in Python
- IDLE a Python IDE
- Activity 1
- Output
- Activity 2
- Arithmetic
- Activity 3
7Why Python?
- Why Python, not JAVA, C?
- UofT makes a choice
- Python is an interpreted language.
8Textbook choice
- Python Programming Second Edition for the
absolute beginner by Michael Dawson - published by Thomson ISBN 1-59863-112-8
9IDLE A Python IDE
- Free IDE download at www.python.org
- Has both interactive window and program files
(script mode)
Interactions Window
10IDLE Interaction
- Allows you to try out an expression with
immediate results. - Output is the result of the expression
11Script Mode
- Why script mode?
- Just choose new program from File
12Most Important Feature in a Language
Documentation
- The symbol to start documentation in Python is
13How do we end the program?
- raw input (\n\n\Press the enter key to exit.)
14Activity 1 Exploration of IDLE and Script Mode
- Purpose to play with the interactions window,
see what the results are. - For the next 5 minutes try printing in IDLE
(print Hello World) for example. - Also, try out the comment symbol and the last
command - Textbook reference pages 11-14
15IDLE Writing Programs
- Programs can be written in files, saved, and run.
- The results are produced in the interactions
window.
16Output
- 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
17Activity 2 First Program
- Write a program (not in the interactions window,
in a separate file) that outputs the famous
quotation To be or not to be on the screen. - Run this program (Run -gt Run Module) and see the
output produced in the interactions window - Reference in text book pages 17-24
18Concatenation
- String concatenation is the symbol
gtgtgt 'Hello ' 'World' 'Hello World'
19 or ?
- Python allows you to use either single or double
quotes for Strings. - This prevents needing special characters if you
want to use a quotation 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
20Math
- Uses 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 (not covered in this session)
21Activity 3 Division
- Explore the difference between
- 5 / 2
- and
- 5.0 / 2
- Textbook Reference pages 25-32
22Character Education in Programming class
- Honesty
- Perseverance
- Diligence
23Python Part 2
24Summary Python Part 2
- Variables
- Input
- Activity 4
- Looping
- Activity 5
- Selection
- Activity 6
25Variables
- Python variables dont carry type.
gtgtgt x 6 gtgtgt print x 3 9 gtgtgt x "Hello" gtgtgt
print x Hello gtgtgt print x " world" Hello world
26Input
- 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
27Input cont
- 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
28Activity 4 Input Conversion
- Write a program that reads in a temperature in
Fahrenheit and converts it to Celsius - C (F - 32) 5 / 9
- Textbook reference32-46
29Indentation
- Python doesnt use brackets to define blocks of
code - Indentation tells the program where a section
ends - If a program isnt properly indented you will get
an error
30Loops
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
31Activity 5 Importance of Indentation
- Create a program that uses a loop, like the
previous example. - Now remove the indentation and try running the
program - Textbook reference51-58
32Comparisions
- 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
33Selection 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
34Selection 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
35Activity 6 Selection
- Write a program that reads in a number and
determines if that number is even or odd. - Textbook reference page 59 65
36Assessment and Evaluation
- Using same principles of honesty, perseverance
and diligence in our assessment and evaluation - Group discussion
37Summary of Python
38Use 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.
39Thanks again to Michael DiRamio.
- Plug Michael is giving a talk at the ACSE
conference Nov. 10 on a different topic.