ULTIMATE REVIEW: MIT: Introduction to Computer Science and Programming in Python

0.0(0)
studied byStudied by 1 person
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/268

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

269 Terms

1
New cards

What Does a Computer do?

A computer fundamentally performs (built-in/primitive and personally defined) calculations and stores the results, but only knows what to do when told.

2
New cards

Declarative Knowledge

statements of fact that computers do not know how to handle

3
New cards

Imperative knowledge

a recipe or “how to” (sequence of steps)

4
New cards

Recipe

  1. Sequence of simple steps

  2. flow of control process that specifies when each step is executed (different decisons, repeating.)

  3. a means of determining when to stop

*Overall an ALGORITHIM

5
New cards

Fixed Program computer

machines with only one function

6
New cards

Stored program computer

Sequence of instructions stored inside the computer, built from a predefined set of primitive instructions. Has a special program (interpreter) that executes each instruction in order (uses tests to hcange flow of control of sequence)

1) arithmetic + logic operations

2) simple tests (equality/inequality statements)

3) moving around and/or storing data

7
New cards

Basic Machine Architecture

4 main parts; goes linearly/step-by-step unless there’s a test that may change the sequence (control flow)

<p>4 main parts; goes linearly/step-by-step unless there’s a test that may change the sequence (control flow) </p>
8
New cards

6 Basic Primitives

Instructions/commands that Alan Turing (Great computer scientists) stated can be used to compute anything with.

  1. Move left

  2. move right

  3. read

  4. write

  5. scan

  6. do nothing

9
New cards

Expressions

complex but legal combinations of primitives (objects + operators) in a programming language that have values and meanings in a programming language;

10
New cards

Primitive Constructs in Python

  • Floats

  • Booleans

  • Strings

  • Simple Operators

11
New cards

Syntax of Programming Languages

Bad = number word; Good = operator, operand, operator

12
New cards

Semantics

the meaning associated with a syntactially correct string of symbols with no static semantic errors

13
New cards

Syntax/ Syntactic Errors

Common and easily caught by Python

14
New cards

Static Semantic Errors

can be caught by Python if program has some decisions to make and goes down the path where error happens. Some languages check for these before running program. Can cause unpredictable behavior

15
New cards

no semantic errors but different meaning than what programmer intended

most frustrating; program crashes, stops running; program runs forever; program gives an answer but different than expected

16
New cards

program

a sequence of definitions/expressions that are evaluated and commands that are executed

17
New cards

commands (statements)

instruct interpreter to do something; can be typed directly in a shell or stored in a file that is read into the shell and evaluated

18
New cards

Objects/ data objects

what everything is in Python; programs manipulate these; all have a type that defines the kinds of things programs do to them

19
New cards

Scalar Object

very basic objects in Python form which everything can be made; cannot be subdivided; int, float, bool, NoneType

20
New cards

int

represent integers (all whole numbers like 5)

21
New cards

float

represent real numbers (anything with a decimal like 3.27)

22
New cards

bool

represent Boolean values True and False (needs capital T and F!)

23
New cards

NoneType

special and has one value, None; represents the absence of a type

24
New cards

type()

special command used to see the type of an object

<p>special command used to see the type of an object </p>
25
New cards

Non-Scalar Objects

have some internal structure and can be subdivided (ex. list of numbers)

26
New cards

int → float

float (#) converts integer # to float #.0

27
New cards

float → int

int (#.#) truncates (takes decimal away without rounding) float #.# to integer #

28
New cards

print ()

command used to show output from code to a user

29
New cards

syntax for a simple expression

<object> <operator> <object>

30
New cards

i + j

the sum (addition)

31
New cards

i - j

the difference (subtraction)

32
New cards

i * j

the product (multiplication)

33
New cards

i / j

the quotient/division (result is always float no matter what)

34
New cards

Result of addition, subtraction, and multiplication

if both are ints, result is int. if either or both are floats, result is float (object type of result depends on both variable types)

35
New cards

i % j

modulus operator; the remainder when i is divided by j

36
New cards

i ** j

exponentiation operator; i to the power of j

37
New cards

Simple Operations

Arithmetic operations in Python are done in PEMDAS order (parentheses, exponents, multiplication, division, addition and subtraction (depends on which is first left to right))

38
New cards

equal sign (=)

an assignment of a value to a DESCRIPTIVE variable name; assigns right to left

39
New cards

Why give names to values of expressions?

To reuse names instead of values which makes it easier to change code later and makes code look nicer

40
New cards

Programming vs Math

in programming, you do not “solve for x” (computer needs formulas/steps to solve)

41
New cards

Changing bindings

can re-bind (change value of a variable) variable names using new assignment statements; previous value may still be stored in memory but is useless.

42
New cards

Strings

object type; sequences of any character (letters, special characters, spaces, digits); enclose in quotation marks or single quotes (doesn’t matter as long as your consistent); can do some operations as defined in Python docs such as multiplication (repeats string # of times)

<p>object type; sequences of any character (letters, special characters, spaces, digits); enclose in quotation marks or single quotes (doesn’t matter as long as your consistent); can do some operations as defined in Python docs such as multiplication (repeats string # of times) </p>
43
New cards

Concatenate strings

put strings together with "+” ; doesn’t implicitly add spaces between variables (need to insert space manually)

<p>put strings together with "+” ; doesn’t implicitly add spaces between variables (need to insert space manually) </p>
44
New cards

,

automatically adds a space between two things (not everything needs to be a string)

<p>automatically adds a space between two things (not everything needs to be a string) </p>
45
New cards

+(concatenation)

no spaces; combines strings together into one big string; all things need to be strings

<p>no spaces; combines strings together into one big string; all things need to be strings </p>
46
New cards

input (““)

prints whatever is in the quotes; user types in something (default given will be a string) and hits enter; binds that value to a variable

<p>prints whatever is in the quotes; user types in something (default given will be a string) and hits enter; binds that value to a variable </p>
47
New cards

int (input(““)

casts string into a integer (can do arithmetic operations)

<p>casts string into a integer (can do arithmetic operations)</p>
48
New cards

i >, <, <=, >=, j

comparison operators used on integers, floats, and strings that evaluate to a Boolean (True or False)

<p>comparison operators used on integers, floats, and strings that evaluate to a Boolean (True or False)</p>
49
New cards

i == j

equality test; True if i is the same as j

50
New cards

i != j

inequality test, True if i is not the same as j

51
New cards

Comparison Operator Rules

Can compare: integers with integers, floats with floats, strings with strings (lexographically, a-z), and floats with integers, but CANNOT compare strings with numbers (floats/integers)

52
New cards

not

logic operator that inverts True or False values

<p>logic operator that inverts True or False values </p>
53
New cards

and

Logic operator; needs both conditions to be True to have a True value overall

<p>Logic operator; needs both conditions to be True to have a True value overall</p>
54
New cards

or

Logic Operator; needs at least one condition to be True to have a overall True value

<p>Logic Operator; needs at least one condition to be True to have a overall True value </p>
55
New cards

if statement only

checks if condition is True (executes if code block) or False (keep going through code without executing if code block)

<p>checks if condition is True (executes if code block) or False (keep going through code without executing if code block) </p>
56
New cards

if statement and else statement

either executes if statement code block (if condition is True) or else code block (if condition is False) NEVER BOTH

<p>either executes if statement code block (if condition is True) or else code block (if condition is False) NEVER BOTH </p>
57
New cards

if, elif, and else statements

only one of the statements’ code block will be executes (earliest True condition)

<p>only one of the statements’ code block will be executes (earliest True condition) </p>
58
New cards

Control Flow - Branching

Allows computers to make decisions based on programmed alogrithim; <condition> has a True or False; evaluate expressions in that block if <condition> is True

59
New cards

Indentation

matters in Python; how you denote blocks of code

60
New cards

If you use = instead of == in if statements..

leads to syntax error! ( = is a assignment operator with == is for equality test)

61
New cards

while Loops

<condition> evaluates to a Boolean; if <condition> is True, do all the steps inside the while code block; check <condition> again; repeat until <condition> is False

<p>&lt;condition&gt; evaluates to a Boolean; if &lt;condition&gt; is True, do all the steps inside the while code block; check &lt;condition&gt; again; repeat until &lt;condition&gt; is False </p>
62
New cards

for Loops

Each time through the loop, <variable> takes a value; 1st time, <variable> starts at the smallest value; next time, <variable> gets the prev value +1, etc.

<p>Each time through the loop, &lt;variable&gt; takes a value; 1st time, &lt;variable&gt; starts at the smallest value; next time, &lt;variable&gt; gets the prev value +1, etc.</p>
63
New cards

range (start,stop,step)

default values are start = 0 and step = 1 (optional to state (only need stop) and is customizable); loop until value is stop -1; has to be ints not floats

<p>default values are start = 0 and step = 1 (optional to state (only need stop) and is customizable); loop until value is stop -1; has to be ints not floats </p>
64
New cards

break statement

immediately exits whatever loop it is in; skips remaining expressions in code block; exits only innermost loop

<p>immediately exits whatever loop it is in; skips remaining expressions in code block; exits only innermost loop</p>
65
New cards

for vs while Loops

<p></p>
66
New cards

lens()

a function used to retrieve the length (how many characters) of the string in the parentheses

<p>a function used to retrieve the length (how many characters)  of the string in the parentheses</p>
67
New cards

function

sort of procedure that does something for you

68
New cards

indexing

gets value of a certain index/position of a string; always start at 0 and last element always at index -1

<p>gets value of a certain index/position of a string; always start at 0 and last element always at index -1</p>
69
New cards

slicing

creating substrings from a string using [start index: stop index: step]

<p>creating substrings from a string using [start index: stop index: step] </p>
70
New cards

immutable

cannot be modified

71
New cards

for char in string

use when need only one character in string; more pythonic (simple and elegant)

<p>use when need only one character in string; more pythonic (simple and elegant) </p>
72
New cards

for i in range (indexing)

use when need to compare character positions or accessing nearby characters not to find one character

73
New cards

Guess-and-Check/Exhaustive Enumeration Algorithm

Try all possible solutions one by one (brute force), stop when you find the correct one. Need to be able to check if the solution is correct to use.

<p>Try all possible solutions one by one (brute force), stop when you find the correct one. Need to be able to check if the solution is correct to use.</p>
74
New cards

Approximate Solutions Algorithm

Test guesses that are "close enough" to the real solution using a small increment. Decreasing increment size causes slower program but more accurate answer while increasing epsilon (close enough guess) causes a less accurate answer but faster program.

<p>Test guesses that are "close enough" to the real solution using a small increment. Decreasing increment size causes slower program but more accurate answer while increasing epsilon (close enough guess) causes a less accurate answer but faster program.</p>
75
New cards

Bisection Search Algorithm

Start with a high and low guess and repeatedly cut the range in half.

<p>Start with a high and low guess and repeatedly cut the range in half.</p>
76
New cards

How do we write code?

so far we… open a file → type some code/sequences of instructions (may contain assignments, loops, conditionals,etc.) to solve a particular problem given.. using only a SINGLE file

77
New cards

Problems of only using a Single file

  • Easy for small-scale problems but messy for larger problems

  • hard to keep track of details (to know if the right info is supplied to the right part of code)

78
New cards

Good Programming

more code not necessarily a good thing, instead it’s measured by the amount of functionality (introduce functions!)

79
New cards

Functions

mechanism to achieve decomposition and abstraction; reusable pieces of chunks of code that need to be written; aren’t run in a program until they are “called” or “invoked” in a program

80
New cards

Decomposition

in programming, divide code into modules (achieved with functions and classes); idea is that different devices/ parts of codes (modules) work together to achieve a common end goal; used to create structure

81
New cards

modules

self-contained; used to break up code; intended to be reusable (can be used with different inputs); keeps code organized and coherent

82
New cards

Abstraction

Used to suppress details; in programming, think of a piece of code as a black box (cannot see unnecessary nor wanted tedious coding details) which can be achieved with function specifications or docstrings; idea is to not need to know how program works to use it (just need to know inputs and outputs)

83
New cards

Functions characteristics

  • has a name

  • has parameters (0 or more)

  • a docstring (optional but recommended)

  • a body

  • returns something

84
New cards

Sample function

def (name) (parameters):

(indent) “““specification/docstring(multiline comment)”””

(indent) body(run commands and include return statement with expression to evaluate)

85
New cards

Scope

mapping of names to objects

86
New cards

If NO return statement..

Python returns the value None (not Boolean, NoneType) which represents the absence of a value

87
New cards

return vs print

knowt flashcard image
88
New cards
<p>Define a function with a variable that’s also defined outside of function →</p>

Define a function with a variable that’s also defined outside of function →

no interference

<p>no interference </p>
89
New cards
<p>function called with a variable that’s only defined outside of function →</p>

function called with a variable that’s only defined outside of function →

use outside definition

<p>use outside definition </p>
90
New cards
<p>function tries to increment/modify a variable that’s only defined outside →</p>

function tries to increment/modify a variable that’s only defined outside →

not allowed in Python but could try global variables

<p>not allowed in Python but could try global variables</p>
91
New cards

global variables

not recommended to use in Python but they allow for the modification of variables (that are only defined outside of a function) inside a function (defeats purpose of functions)

92
New cards

Tuples

an immutable and ordered sequence of elements; element types can be mixed; represented with parentheses (); can perform indexing, concatenation, and len() on them

93
New cards

empty tuple

variable = ()

94
New cards

singleton tuple

tuple of one character/element

95
New cards

for loops with tuples

iterate over the tuple object at each position

96
New cards

Lists

ordered and mutable sequence of info/ elements that are usually homogenous, accessible by index; denoted by square brackets []; can index and len()

97
New cards

Iterating over a list

compute the sum of elements of a list; common pattern, iterate over list elements; indexed 0 to len(variable)-1 and range(n) goes from 0 to n-1

98
New cards

L.append(element)

add elements to end of list (mutates the list)

99
New cards

L.extend (some_list)

combines lists together

100
New cards

del(L[index])

delete element at a specific index