Transcript Chapter 12

I210 review
About Final Exam
 When: 2:45-4:45 p.m., Thurs., May 7
 Where: BU 400
 What will be covered: Everything we talked so far
(excluding the optional topics)
 If you attended most of the lectures, did the PLTL
exercises, wrote your own codes, took the quizzes
by yourself, you should have nothing to worry
about the final exam!
Guide to Programming with Python
2
Data Types
 Data type: sequences (containers)
– Strings (a sequence of letters)
– Tuples (a sequence of elements of any type;
immutable)
– Lists (a sequence of elements of any type; mutable)
– Dictionary is NOT a sequence
 How to access the elements in a sequence
– Sequential access (using for and while loops)
– Indexing & Slicing
 Removing and adding elements
 Important sequence operators and functions,
len([1, 2, 3]), ”a" in "abc", [1, 2, 3].index(1)
Guide to Programming with Python
3
Branching Structures
 Branches based on a condition/conditions
– if
– if-else
– if-elif-else
 Condition: Expression that is True or False
 Often create conditions by comparing values
 Treating values as conditions: Any empty (None)
or zero value is False
 Compound conditions (logical operators)
Guide to Programming with Python
4
Loop Structure
 Need to know when and how to use for and while
loops correctly
– for loops: iterate over the elements in a sequence
– while loops: repeat a block of code as long as a
condition is
 insertion_sort.py
Guide to Programming with Python
5
Functions
 How to write functions
 How to receive and return values
 Understand local and global variables
Guide to Programming with Python
6
A Sample Question
 Write a function to translate temperate value in
Fahreheit to Celcius. The function takes in two real
numbers, minF and maxF through its parameters,
which signify the minimum Fahrenheit and
maximum Fahreheit temperature to display, as well
as a third real number, step, which signifies the
increment between temperatures to display. The
function needs to display the Fahrenheit values
between minF and maxF in increments of step in a
table associated with their Celcius equivalents to
the screen. Recall that the formula for converting a
Fahrenheit temperature to Celcius is: C = (5/9) * (F32)
Guide to Programming with Python
7
Files
 How to use files
– Read from text files; readline(), readlines()
– Write to text files (permanent storage); write(),
writelines()
– Need to open a file before using it, and close it when it is done
 How to extract information from the strings you read in from
a text file!
text_file =
for line in
line =
(name,
open("read_it.txt", "r")
text_file:
line.strip()
score) = line.split()
Guide to Programming with Python
8
A Sample Question
 Write a function that accepts a filename and reads
in the comma-separated numbers from the text file.
The function saves the numbers in a list, calculates
and displays the average of the numbers, and
return the list of numbers.
Guide to Programming with Python
9
Handling Exceptions
try:
num = float(raw_input("\nEnter a number: "))
except(ValueError):
print "That was not a number!"
else:
print "You entered the number", num
 Can add single else clause after all except clauses
 else block executes only if no exception is raised
 num printed only if assignment statement in the try
block raises no exception
Guide to Programming with Python
10
OOP
 How to create classes to define objects
 How to write methods and create attributes for
objects
 How to instantiate objects from classes
 Understanding inheritance
– Derive new classes from existing ones
– Extend the definition of existing classes
– Override method definitions of existing classes
 Using modules
Guide to Programming with Python
11
GUI Programming
 Events-driven programming!
 Work with a GUI toolkit
– Tkinter
– Create a root window and create a frame
– Create and use widgets
• Buttons; text entries; text boxes; check buttons; radio
buttons
– Layout of the widget; grid()
– Create event handlers and bind (associate) events
with event handlers
Guide to Programming with Python
12
A Sample Question
Guide to Programming with Python
13
Games
 Multiple choices only (no small questions)
 How to use livewires, override update() and
constructor method in a class derived from the
games.Sprite
 Understanding the important games objects
(screen, keyboard, mouse) and classes (Sprites,
Text, Message)
Guide to Programming with Python
14
Test me
 Test if a function or a class works correctly by
writing up a series of testing cases with different
inputs
 Of course, you need to know what are the
expected results
Guide to Programming with Python
15