Week 1 Summary: Programming 2
* OO object is a sort of active data type that combines both data and operations
* Objects know stuff (they contain data), and they can do stuff (they have operations)
* Objects interact by sending each other messages (request for an object to
perform one of its operations)
* A graphics window is actually a collection of tiny points called pixels ("picture elements")
* Every object is an instance of some class, and the class describes the properties the instance will
have
* To create a new instance of a class, we use a special operation called a constructor
* A call to a constructor is an expression that creates a brand new object, the general form is as
follows:
<class-name> (<param1> , <param2> , . . . )
* The set of messages that an object responds to are called the methods of the object (methods are
functions that live inside the object), a method is invoked using dot-notation:
,<object> . <method-name> (<param1> , <param2> , . . . )
move (dx , dy) (moves the object dx units in the x direction and dy units in the y direction)
To move the point p to the right 10 units, we could use this statement:
p . move ( 10 , 0)
* First line creates a circle with a center located at the point (100, 100) and a radius of 30
* The second line creates a GraphWin
* The third line is a request for the Circle object circ to draw itself into the GraphWin object win
circ = Circle (Point ( 100 , 100) , 30)
win = GraphWin ( )
circ . draw (win)
* It is possible for two different variables to refer to exactly the same object; changes made to the
object through one variable will also be visible to the other
## Correct way to create two circles , using clone .
LeftEye = Circle (Point (80 , 50) , 5)
leftEye . setFill ('yellow')
leftEye . setOutline ('red')
rightEye = leftEye . clone ( ) # rightEye i s an exact copy of the left
rightEye . Move (20 , 0)
## Turtle example:
import turtle
win = turtle.Screen()
win.setup(width=512, height=512)
my_turtle = turtle.Turtle()
my_turtle.shape('turtle')
my_turtle.forward(50)
* Objects are instances of classes
* Objects are:
○ A collection of related information (data)
○ A set of operations to manipulate that information (methods)
* File handlers:
file_handler = open("test_file.txt", "r")
line = file_handler.readline()
data = file_handler.read()
file_handler.close()
, Week 2 Summary: Programming 2
* Control structures allow us to alter the sequential flow of our program
* Decision structures are a type of control structure that allow a program to execute different
sequences of instructions for different cases allowing the program to select an appropriate
course of action
* Simple if-then structures are called: one-way decisions
* Two-way decisions are if-else statements
* Multi-way decisions: nesting and adding an if-then statement inside another
* A better solution is to use if-elif-else
* For implements a definite loop: you know exactly how many iterations should be performed
*While implements an indefinite loop: the loop must be performed as long as a certain condition
applies (condition is a Boolean expression, just like in if statements)
* <filevar> = open(<name>, <mode>)
‘name’ is a string with the actual file name on the disk
‘mode’ is either ‘r’ or ‘w’ (read or write)
* infile = open("document.txt", "r")
outfile = open("document.txt", "w")
* <file>.read() – returns the entire remaining contents of the file as a single (possibly large, multi-
line) string
* <file>.readline() – returns the next line of the file. This is all text up to and including the next
newline character
* <file>.readlines() – returns a list of the remaining lines in the file. Each list item is a single line
including the newline characters
* If you open an existing file for writing, you delete the file’s contents
outfile = open("mydata.out", "w")
print(<expressions>, file=outfile)
* Standard Input/Output - allow program to read-from/write-to the console (without opening a
file)
* Using standard input/output or a file depends on program requirements
* Special file-handles that exist in the sys module:
- standard output: sys.stdout
- standard error: sys.stderr
- standard input: sys.stdin
* OO object is a sort of active data type that combines both data and operations
* Objects know stuff (they contain data), and they can do stuff (they have operations)
* Objects interact by sending each other messages (request for an object to
perform one of its operations)
* A graphics window is actually a collection of tiny points called pixels ("picture elements")
* Every object is an instance of some class, and the class describes the properties the instance will
have
* To create a new instance of a class, we use a special operation called a constructor
* A call to a constructor is an expression that creates a brand new object, the general form is as
follows:
<class-name> (<param1> , <param2> , . . . )
* The set of messages that an object responds to are called the methods of the object (methods are
functions that live inside the object), a method is invoked using dot-notation:
,<object> . <method-name> (<param1> , <param2> , . . . )
move (dx , dy) (moves the object dx units in the x direction and dy units in the y direction)
To move the point p to the right 10 units, we could use this statement:
p . move ( 10 , 0)
* First line creates a circle with a center located at the point (100, 100) and a radius of 30
* The second line creates a GraphWin
* The third line is a request for the Circle object circ to draw itself into the GraphWin object win
circ = Circle (Point ( 100 , 100) , 30)
win = GraphWin ( )
circ . draw (win)
* It is possible for two different variables to refer to exactly the same object; changes made to the
object through one variable will also be visible to the other
## Correct way to create two circles , using clone .
LeftEye = Circle (Point (80 , 50) , 5)
leftEye . setFill ('yellow')
leftEye . setOutline ('red')
rightEye = leftEye . clone ( ) # rightEye i s an exact copy of the left
rightEye . Move (20 , 0)
## Turtle example:
import turtle
win = turtle.Screen()
win.setup(width=512, height=512)
my_turtle = turtle.Turtle()
my_turtle.shape('turtle')
my_turtle.forward(50)
* Objects are instances of classes
* Objects are:
○ A collection of related information (data)
○ A set of operations to manipulate that information (methods)
* File handlers:
file_handler = open("test_file.txt", "r")
line = file_handler.readline()
data = file_handler.read()
file_handler.close()
, Week 2 Summary: Programming 2
* Control structures allow us to alter the sequential flow of our program
* Decision structures are a type of control structure that allow a program to execute different
sequences of instructions for different cases allowing the program to select an appropriate
course of action
* Simple if-then structures are called: one-way decisions
* Two-way decisions are if-else statements
* Multi-way decisions: nesting and adding an if-then statement inside another
* A better solution is to use if-elif-else
* For implements a definite loop: you know exactly how many iterations should be performed
*While implements an indefinite loop: the loop must be performed as long as a certain condition
applies (condition is a Boolean expression, just like in if statements)
* <filevar> = open(<name>, <mode>)
‘name’ is a string with the actual file name on the disk
‘mode’ is either ‘r’ or ‘w’ (read or write)
* infile = open("document.txt", "r")
outfile = open("document.txt", "w")
* <file>.read() – returns the entire remaining contents of the file as a single (possibly large, multi-
line) string
* <file>.readline() – returns the next line of the file. This is all text up to and including the next
newline character
* <file>.readlines() – returns a list of the remaining lines in the file. Each list item is a single line
including the newline characters
* If you open an existing file for writing, you delete the file’s contents
outfile = open("mydata.out", "w")
print(<expressions>, file=outfile)
* Standard Input/Output - allow program to read-from/write-to the console (without opening a
file)
* Using standard input/output or a file depends on program requirements
* Special file-handles that exist in the sys module:
- standard output: sys.stdout
- standard error: sys.stderr
- standard input: sys.stdin