Python Final Exam Flashcards
Program
sequence of instructions that specifies how to perform a computation
Computation
mathematical computation -> data processing, aka software
Hardware
physical parts that make up a computer
Typical hardware components
Central processing unit (CPU)
* Main memory (RAM)
* Secondary storage devices (HD)
* Input and output devices
Application software
Programs that make computer useful for
business and users
* Word
* Excel
* Photoshop
* Outlook
* Chrome
* iTunes
System Software
Operating system
* Controls hardware
* Windows, MacOS, Linux, IOS, Android
Utilities
* Enhance or protects the computer
* Anti-virus, 7-zip, Parallels
Software development tools
* Create and modify software
* IDLE, Visual Studio, IntelliJ IDEA
Low-level language
Close in nature to machine language
* Computer architecture specific. Not portable.
FORTRAN, COBOL, and C
High-level language
Allows simple creation of powerful and complex programs
* Agnostic to the computer architecture. Portable code.
Python, PHP, Java, C++, Ruby.
What types of processes do we have?
Math, conditions, repetition
Algorithm
set of step-by-step instructions that detail a process or
computation
(set of logical, well-defined rules or procedures for efficiently and
accurately solving a problem)
Pseudocode
informal high-level description of the structure of an
algorithm
* Written in a combination of natural language and programming
language constructs.
Flowchart elements
terminal symbol = oval
processing symbol = rectangle
decision symbol = diamond
input/output = slanted rectangle
Python Advantages
High-level language
Extensible (lots of packages)
Versatile
Emphasis on style & readability
No semicolons/brackets –> mainly identation
interpreted NOT compiled (translate on the fly)
Difference Interactive mode versus Python scripts
Interactive mode = “console”, won’t save
Python script saves code, end in .py, can be run from IDLE with run button
IDLE
Integrated Development Program
Single program that provides tools to write, execute and test a program
Has interactive and scripting mode
Function
prewritten code that performs a “blackbox” operation (e.g. print, max etc.)
Group of statements that exist within in a larger program
Argument
any data that is passed to a function, e.g. “Hello again”
Default type for input
Str
Variable
letter or word(s) that represents data stored in
the computer’s memory.
//
Floor division, divides two numbers and then returns the lower integer
Example
7//2 = 3
(7 divided by 2 is 3.5, and then rounded down)
%
Modulo - retains remainder
For example
7 % 2 = 1
(because 7 divided by 2 is 3 with a remainder of 1)
format function
.2f –> print(f” {number.2f}”)
,.2f –> comma and two decimal places
.0% –> percentage rather than decimal
,d –> adds commas for whole integers
end and sep arguments. Give an example
print(number sep=”___”)
print(number end=”___”)
\n = new line
\t = insert tab
Decision Structure
execute certain statements
only under certain circumstances.
Conditional execution
execute statement only when the condition is
true. Simplest form of Decision.
Condition Control (while loop)
Number of loops/iterations is unknown, Boolean expression usually exits the loop
Count controlled (for)
Based on a counter, number of loops is known
Nested loops
Inner cycle cycles through each iteration for each outer loop
inner loop iterates multiple times (faster) than outer loop
iterations = inner loop * outer loop
How to change items of a list?
a.append(“new word”)
a.pop() –> deletes last item
a[1] –> choose the index 1
a[4,6] –> selects index 4 and 5
a.insert(5, “banana”) –> inserts banana at index 5
a.remove (“banana”) –> removes all values with banana
What can control structure be divided in?
sequence, selection, and iteration.
Control structure
logical design that controls the order in which a set of statements execute
What are short-circuit evaluators?
logical operator that evaluates its operands from left to right and stops as soon as the result of the entire expression is determined
The two existing one’s in Python: And/or
Modularized program
Each task within the program is a function of its own
Benefits of modularized program
simpler use, better testing and debugging, faster development, code can be reused
Void function
executes a statement (e.g. prints a statement)
Value-returning function
executes a statement and then returns a value back (e.g. input, integer, float function etc)
Syntax void function
def function_name():
statement
or
def main():
function1()
function2()
top-down design
technique to break down algorithm into functions through decomposition (through hierarchy)
Syntax value-returning function
def function_name(parameter1, parameter2):
sum = parameter1+parameter2
return sum
Final Parameter
local variable that receives an argument
list
collection of items within an object, mutable
element
an item in a list
Syntax list
list1 = []
tuple
similar to list, but immutable (no insert or append)
enumerate function
adds counter to sequence
listb = list(enumerate(lista)
slice syntac
lista[start:stop:step]
“in” operator in list
if x in lista:
list methods
append(item)
index(item)
insert (index, item)
sort()
for n in list:
average of a list
calculate total and divide by len(list)
2-dimensional lists
nested lists, rows and columns, a = [[a,b,c], [1,2,3]]
indexed by two indeces e.g. [2][2] = 3
What does \ do in a string?
allows to break a statement into multiple line (similar to \n or \t)
Can strings be indexed?
Yes
Strings mutable or immutable?
Immutable, but can slice strings
Dictionaries
store collection of data with key and value
Syntax dictionary
dictionary = {k1:v1, k2:v2} or dict() function on a list
Advantages dictionaries
keys must be unique, very fast
How to retrieve values in dictionaries?
Use key, e.g. dictionary[key]
Get method for dictionaries
dictionary.get(key, default) –> if key does not exist a default value is returned
Iterate through dictionary
for key in dictionary:
print(key) #prints the key
print(dictionary[key]) #prints the value
Set
object that stores a collection, all items must be unique
Syntax Set
set1 = {} (but not key-value pairs) or set() function
open file syntax
file_name = open(filename, mode)