Python Basics Flashcards
Declarative Knowledge
A statement of fact
it does not tell you have to do stuff
Imperative Knowledge
A recipe or “how-to”
(this tells you how to do something)
Also know as an algorithm
What is a recipe?
1) A sequence of simple steps
2) Flow of control that specifies when each step is executed
3) Determining when to stop
Syntax
Is passing of a sentence/string
Static semantics
Is which syntactically valid strings have meaning
Semantics
Is the meaning associated with syntactically correct string of symbols with no static semantic errors
Program
Is a sequence of definitions and commands
- definitions are evaluated
- commands are executed
Commands
Statements - instruct interpreter to do something
Data objects
is what the program is manipulating
Objects are
scalar (cannot be subdivided)
non-scalar (have internal structure that can be accessed)
Object type
defines what the programs can do to them
Scalar objects in Python
- Integers (int)
- Real numbers (float)
- Boolean (bool)
- Special (NoneType)
cast
convert objects of one type to another
combine objects and operators
operator precedence without parentheses
** * / \+ and - execute left to right parentheses always have top priority and get done first and will be evaluated inside out.
assignment
equal sign is an assignment of a value to a variable name
- value stored in computer memory
- an assignment binds name to value
- retrieve value associated with name or variable by invoking the name
Abstracting expressions
- give names to values of expressions
- reuse names instead of values
- easier to change code later
Branching
- make a choice and do different things
- each statement gets executed once
- runs in constant time
Variables (bindings)
- name (descriptive, meaningful, re-read code, no keywords)
- value (can be updated)
- right hand side -> VALUE
- left hand side -> VARIABLE
String
- letters, special characters, spaces, digits
- enclosed in quotation marks or single quotes
- concatenate strings by using the + operator
overloaded the + operator
is when using the + sign to concatenate two or more strings together
for loops
- known number of iterations
- can end early via break
- use a counter
- can rewrite a for loop using while loop
while loops
- unbound number of iterations
- can end early via break
- can use a counter, but must initialize before loop and increment inside loop
- may not be able to rewrite a while loop using a for loop
compare/test object type
- type(object) returns the type of the object
- isinstance(object, type) testing if an object is of a certain type
iterative code
- branching structures (conditionals) can jump to diff. pieces of code based on a test - programs are constant time
- looping structures (like while or for) let us repeat code until a condition is satisfied - programs take time depending on value of variables as well as length of program.
Useful for a decrementing function
- maps set of program var into an integer
- when loop is entered, value is non-negative
- when value is <= 0, loop terminates and
- value is decreased every time through loop
Need a loop variable
- initialize outside loop
- changes within loop
- test for termination depends on variable
Guess-and-Check
- guess a value
- check if the solution is correct
- keep guessing until find solution or guessed all values
- the process is exhaustive enumeration