100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.6 TrustPilot
logo-home
Summary

Summary Introduction to programming 2 ALL exam summaries

Rating
-
Sold
-
Pages
16
Uploaded on
23-05-2023
Written in
2022/2023

The file contains everything important for the exam of Introduction to programming 2. The information is taken from the lecture slides as well as the books and these summaries helped me pass the exam. The file is divided into 7 parts, for each week of the block.

Show more Read less
Institution
Course

Content preview

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

Written for

Institution
Study
Course

Document information

Uploaded on
May 23, 2023
Number of pages
16
Written in
2022/2023
Type
SUMMARY

Subjects

$9.44
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
enk

Get to know the seller

Seller avatar
enk Rijksuniversiteit Groningen
Follow You need to be logged in order to follow users or courses
Sold
10
Member since
2 year
Number of followers
8
Documents
0
Last sold
1 year ago

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Trending documents

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions