Transcript Session 1

Python Workshop for Teachers
#1 March 2012
Peter Andreae
The standards:
• what's new:
• program decomposed into modules
• functions with parameters (rules out Scratch)
• lists/arrays/sequences
© Peter Andreae
Textual vs Drag&Drop
Textual Languages ( eg Python, Java, ….)
vs
Drag and Drop Languages (eg Scratch, Alice)
• Just the same
• But harder for learners because
• There is much more to remember (no menu of options)
• Much more detail to get right (or wrong)
© Peter Andreae
Python vs other textual languages:
• Plus
•
•
•
•
simple syntax rules (fewer details to get wrong)
untyped variables and parameters (less sytax to get wrong)
interpreter (allows testing and experimenting)
lots of libraries
• Minus
• untyped variables and parameters (computer can't help debug as
much)
• interpreter (computer doesn't help debug as much)
• some inconsistencies, especially with lists.
• non-standard structures as well as common ones.
© Peter Andreae
Python syntax.
• Don't have to specify types of variables
• Indentation is meaningful - specifies nesting
• Very little required punctuation
• if, while, for, def use a :
• " … " or ' … ' or ' ' ' ….. ' ' '
for strings
• [ ] for lists
• ( ) for function arguments/parameters
and for "tuples"
• { } for dictionaries (mappings)
© Peter Andreae
Example programs
• guess-the-word.py
• no functions - the instructions in the file will be executed in order (just
as if typed directly at python)
• input and output
• while loop
• triangleWord.py
•
•
•
•
•
defining a function (no parameters)
converting strings to numbers
for loop using range
lengths and substrings of Strings
if
• (see the resources link for the programs)
© Peter Andreae
7
Guess the word
• (This is a silly program, but it illustrates some python)
print('Lets play "guess the word", ok?')
word = input("guess a word: ")
while word != "pancake" :
print("no, ", word, " is not the right answer")
word = input("guess again: ")
print("Yes, you guessed the magic word!")
© Peter Andreae
8
TriangleWord
def triangleWord() :
word = input("enter a long word: ")
for i in range(len(word)+1) :
print(word[0:i])
def trapezoidWord() :
word = input("enter a long word: ")
start = int(input("how long should the first substring be?: "))
end = int(input("how long should the last substring be?: "))
if end > len(word) :
print("the word isn't that long! going up to maximum: ", len(word))
end = len(word)
for i in range(start, end+1) :
print(word[0:i])
triangleWord()
print("---------------------")
trapezoidWord()
© Peter Andreae
To Do
• Temperature converter (F -> C)
• ask for temperature in fahrenheit
• compute and print out temperature in celcius
• Times table printer
• asks for number and prints out 1 x up to 10 x
choose number for table: 8
1x8=8
2 x 8 = 16
3 x 8 = 24
4 x 8 = 32
5 x 8 = 40
6 x 8 = 48
7 x 8 = 56
8 x 8 = 64
9 x 8 = 72
10 x 8 = 80
© Peter Andreae
TimesTable
10
def timesTable() :
num = int(input("Enter number for table: "))
print("The ", num, "times table")
for i in range(1, 11) :
print(i, "x", num, "=", (i*num))
def printTimetable(num) :
for i in range(1, 11):
print(i, "x", num, "=", i*num)
for j in range(1, 11) :
printTimetable()
© Peter Andreae