Course material |
Overview
algorithm: a step-by-step sequence of instructions to carry out some task
programming: the process of designing, writing, testing and debugging algorithms
that can be carried out by a computer
programs are tools for solving problems
e.g., solving Sudoku puzzles, disease vector modeling, hailstone sequence
Python is a simple but powerful (and widely used) language
invented in 1991 by Guido von Rossum
Variables, statements & expressions
Python interpreter, book examples, IDLE shell
data types
numbers (int, float): +, -, *, /, //, **, %
strings: +
Booleans (True, False)
variables
variable names, naming conventions, reserved words
assignment statements, expressions
built-in functions
type, max, min, abs, round, str, int, float, bool
print: mixing text and variables, separating spaces
input: prompt message, converting input to a number
Debugging
syntax vs. runtime vs. logic errors
strategies
create test cases with known outcomes
utilize diagnostic print statement to isolate errors
integrate testing with code development
Functions
predefined modules, import statement
random: random, randint, choice
math: sqrt, floor, ceil, nan, ...
turtle: forward, left, right, color, ...
for loop
counter-driven repetition
utilizes loop variable & range function
examples: building a random string, turtle polygons
user-defined functions
a function defines a unit of computational abstraction
can have inputs: parameters, default values, optional types
can have output: return statement
document using doc strings, displayed by help function
user-defined modules
metric: inchesToCentimeters, centimetersToInches, feetToMeters, metersToFeet, ...
casino: rollDie, rollDice, flipCoin, pickCard
Conditional execution
if statement, optional else case
Boolean operators: <, <=, >, >=, ==, !=
logical connectives: and or, not
precedence rules, short-circuit evaluation
cascading if-else statement using elif
provides for multiway conditionals
Repetition
counter-driven vs. logic-driven loops
for loops
accessing the loop variable, adjusting the range
examples: summing dice rolls, counting doubles
while loops
repetition is based on a Boolean test
beware of black holes (infinite loops)
examples: summing numbers in a range, hailstone sequence, listener loop, input validation
extended example: Pig game simulations
|