1/153
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
Computer Science
The study of computation and computational devices
What are the two major components of computation
algorithms and abstraction
algorithm
A set of instructions demonstrating how to explicitly perform a task
Are algorithms always the same?
Most of the steps are standardized, and are thus the same for solving any one of a given class of problems
Certain details may be changed to provide problem-specific solutions
What are the four components of any algorithm?
Finite number of steps
Eventually ends
well-defined, explicit instructions
solves a general class of problems
What are the four ways in which we measure algorithms?
Correctness
Robustness
Efficiency
Clarity
Correctness
The ability of an algorithm to produce the right output for all inputs
Robustness
The ability of an algorithm to handle unexpected inputs
Efficiency
The ability of an algorithm to not take too long when performing a task
Clarity
The ability of an algorithm to be easily understandable and alterable
Abstraction
a simplified representation of something more complex. Abstractions allow you to hide details to help you manage complexity, focus on relevant concepts, and reason about problems at a higher level.
heavy abstraction
removing all fine details, leaving only major components intact.
What does a programming language do?
converts instructions that are provided by the user into machine code output (implements an algorithm)
Python
a high level, very readable, high abstraction coding language
why is Python considered to be high-abstraction
Because it hides all the underlying details behind turning user input into machine code output, focusing only on the major details of what's occurring in a way that's comprehendible.
Hides all the irrelevant data.
Variable
A name that refers to a value
How are variables in math similar to variables in computer science? How are they different?
In both, variables are names that represent values. Additionally, in both, they are often stored for later usage.
In computer science, variables can be redefined repeatedly, unlike in math.
syntax
The form of a statement that we must adhere to whenever inputting statements in Python.
Assignment statement (and syntax)
Uses the equal sign to give the object property on the left of the equal sign the value on the right of the equal sign.
Naming rules for python variables (3)
1. Must start with a letter or a _
2. Can only contain numbers, letters, and _
3. Case sensitive
Give the proper terminology for the following:
x = 7
x = 4 + 4
print ( )
x
4
+ , =
Assignment statement
Assignment with expression
Print statement
Variable
Constant
Operator
How does assignment work in Python (three step process)
1. Interprets the variable on the left side of the =
2. Evaluates the statement on the right side of the =
3. Assigns the value to the stored variable in memory
Order of operations (Python)
( ) parentheses.
** exponentiation
* multiplication, / division, % remainder
+ addition, - subtraction
How can we swap variable values in Python (two methods)
1.
temp = x
x = y
y = x
2. Simultaneous assignment
x , y = y , x
Relational Operators in Python
Compare two values and return a Boolean
==, !=, >=, <=, >, <
Logical Operators in Python
Check for a condition and return a Boolean
And, Not, Or
Explain how and, not, and or work
and - if all conditions are true, output is true
or - if at least one condition is true, output is true
not - if condition is false, output is false
Describe the precedence for the logical operators in Python
Not first, then and, then or
Data Type (Python)
An implicitly understood class of data that is given to all variables, literals, and constants in Python
What are the five main data types
List, string, Boolean, character and integer
How can we check the data type in Python?
type ( )
How does "+" work differently for numbers and strings in Python
With strings, it will concatenate whatever is within the ""
With numbers, it will output a sum
float vs. integer. Which one has precedence (i.e., if you entered 7 + 7.0, would the response be a float or an integer)?
floats are decimal values, integers are integers
floats take precedence (14.0)
How do we convert values into different types
float ( integer ) = integer.0
int (float ) = float rounded down
str ( ) = " "
What are the other two numeric data types in Python (not float or integer)
Complex numbers (x+yi) and long integers.
What do we call a string with a single character?
Character
Lists vs. Tuples
Both can contain multiple items of varying data types
You can change the items in a List, while a Tuple is locked
Lists are represented with [ , , , ], while Tuples are represented with ( , , , )
how can we prompt a user for input
input ( " " )
will display whatever is in the "" to the user
what data type do input statements return by default?
string
how can we request numerical input?
float (input ( ) )
int (input ( ) )
how do we implement comments in Python
#, anything after this will be ignored
Why is # so important in Python?
Because it allows for comments, which can improve the clarity and readability of your code
also because you can use it to "turn off" certain lines of code while debugging
Booleans
a data type that contains the values True and False
what are the two values in the Boolean data class?
True, False (capitalized)
how do relational operators work with individual letters?
letters are compared to one another based on their order, from a-z-A-Z
no real values assigned to each letter, just ambiguous assignment that compares their ordered position
how do relational operators work with strings?
The first letters are compared. If they match, the second letters are compared, and so on.
what is False in Python
any empty data structure (0, 0.0, empty string, None objects (data type), etc.
everything else is True
When does Python stop evaluating? (give two examples for and and or statements)
Python stops evaluating when it knows the value of a Boolean. For example, if we have:
1 > 2 and 4>2, Python will stop evaluating after the first statement, because it already knows that the entire statement is False
alternatively, if we have 2 > 1 or 4 > 2, Python will stop evaluating after the first statement, because it already knows that the entire statement is True
What statements should we avoid when attempting to keep our Booleans simple?
== True
== False
double negatives
if statement
A line that determines whether or not you run a certain chunk of code
if-else statement
allows your program to perform one action if the Boolean expression evaluates to true and a different action if the Boolean expression evaluates to false.
identical to if statements, but adds an alternative set of instructions for False Boolean instances, whereas if statements just stop/skip in this instance
if-elif-else statement
similar to if-else, but allows for different conditions
can end in else, but doesn't need to
if-elif-else syntax
if
elif
else
can have as many elifs as you want
for loop
Loops that have a predetermined beginning, end, and increment (step interval).
parameter specifications for for loops
1 parameter = end (start set to 0, increment to 1)
2 parameters = start and end (increment set to 1)
3 parameters = start, end, and parameter
when will a for loop stop executing
when the value equals or exceeds the end parameter (will not run the final value when this occurs)
does a for loop have automatic incrementation? what about a while loop?
for loop - yes
while loop - no (you have to implement it or the loop will never terminate)
does a for loop require outside initialization? what about a while loop?
for loop - no, it will initialize at the start parameter
while loop - yes, you need to predefine the variable
when do we use for loops?
when we're applying computation to every item in a collection
when do we use while loops?
when we're attempting to get to a certain condition
can we have float increments?
no
what happens in a nested for loop?
for every incrementation of the outside loop, the entirety of the inside loop is run
break
command used to terminate the current loop immediately
use with caution, because it can make your code hard to read
Try-Except Statement
Python executes all of the statements underneath try.
If there is no error, then Python does nothing and skips over all the statements underneath except.
However, if Python crashes while inside the try portion, it recovers and jumps over to except, where it executes all the statements underneath there.
what can you do to an except statement to make it more versatile
you can type except
when do we use functions in python?
when we want to perform the same operation repeatedly throughout a program.
how can functions make debugging easier?
you only have to fix one instance of the buggy code, rather than every individual instance of the body within the program
how do we define a function? how do we call a function? (syntax)
def
function
reusable code that takes arguments as input, performs a computation, and returns a result
global keyword
allows you to use a non-parameter function within a body of statements
is variable definition/reassignments within functions local or global (i.e., if you define or reassign a variable value within a function, will the changes/definition be present outside of the function).
local, any defined variables and any variable value changes will not be present outside of the function
parameter
a piece of information that a function needs to work properly (one that is not the same in every use of the function)
how many parameters can a function have?
can have none, can have (theoretically) an infinite amount
what does a return statement do?
the value after the return command will be evaluated and associated with the specific function call, which can then be used outside of the function. After this assignment, the function ends.
e.g.
funcname (x):
return 2x
print(funcname(7))
-> 14
print vs. return
return will store the returned value in the specific function call in question, whereas print will simply display any values on the screen (following the function, these values are destroyed and cannot be referenced again).
what is the associated value of any function call that does not have a return statement?
None
how do we write a function (5 steps)?
1. figure out the intended behavior of the function
2. define what types of data we want to put in and get out
3. write the header
4. add a description of what it will do in a comment
5. fill in the body
what are the two types of functions in python?
defined functions and built-in functions
where did the term "bug" come from?
in the 1940s, Navy Admiral Grace Hopper found a moth in a computer that was being worked on. From this point on, technical errors became known as bugs.
what are some of the major causes of bugs?
syntax (brackets, colons, indentation, etc.), variable naming (case-sensitivity, naming rules, etc.), and function formatting (what orders are the parameters being used it, is the output what we expect it to be, etc.), among others.
module
a python file containing a collection of functions
how do we import modules
import
what must be true if we are to import a self-made module?
the python file being imported must be on the same device as the interpreter
how do we call a function within a module
how do we import a specific function from a module
does this make a difference?
from
when a function is imported in this way, we don't have to use dot notation when we call the function
overloaded operator
An operator that can operate on two or more types of operands (+ and * serve different functions when working with strings, integers, floats, etc.).
what are the two ways we can iterate through strings? How are these two ways different from one another?
for in
for in range (len(
in the first case, the iterating variable is a character in the string; in the second case, the iterating variable is a number corresponding to an index position.
slicing a string
the process of finding a subset of characters from a string.
can we use negative index positions when slicing a string?
yes, it just counts down from the last character to the first
the last character has an index of -1
how can we check if a string is contained within another string?
what is the order of characters that python uses for relational operators
capitals have lower value than lower case levels
lower alphabet letters have lower value than higher alphabet letter
z has higher value than any other letter, A has the lowest
method
a function that is specific to one type of object
how do we call functions?
strings are immutable. How can we alter a string given this reality?
none of its methods can change the content of a String object
we can only alter strings through reassignment
what function can we use to find out all of the methods associated with a data type?
dir(datatype)
what does .find() do?
returns the beginning index of the first occurrence of the argument within a given string
what does .replace(x,y) do?
replaces all x with y in a string
what does .count() do?
returns the number of times a substring occurs within a given string
what do .startwith() and .endwith() do?
return True when the start/end value matches that in the parameter, False otherwise
lists
data structures that let us store collections of data with associated indices