1/219
Looks like no tags are added yet.
Where code is entered
code cell
constant
fixed values, those which dont change ex. string, numeric
print()
displays data as the output
strings
sequence of characters
letters, words, numbers
anything encased in single or double quotes
numeric constants
doesnt need quotes
only numbers
comments and how they help
anything following a #, doesnt return an output
improve readability, usually explain code, best practice to use them
line spaces…
do not impact the code!
value
basic unit of a program a letter, number, or other
types of values
integer, str, float, bool, dict, tuple, list
dont type integers with ___
commas
float
numbers with a decimal point, even if its .00
bool
boolean, true or false
type()
returns the type of a value - int,str, float, bool, list, dict, tuple
returned as <class ‘type’>
float()
changes an integer/string to a float
only strings that work with floats are numbers
adding different types of values
int/float + string = ERROR
int + int = INT
float + float = FLOAT
int + float = FLOAT
variables
a named place that stores a value and can retrieve it later. variable can change
assignment statement
creates new variables and gives them values
only one =
ex. x = 2
type of a variable is….
the type it is assigned to
rules of variables name
cant start with a number (but can INCLUDE numbers)
cant contain special characters (@,!, a space)
cannot contain keyword/reserved words (False, class, continue, and)
are case sensitive! (age and AGE are different)
statement
unit of code that the python interpreter can execute
operators
special symbols that represent computations
list of operators and functions
/ = divides, always makes a float
+adds
-subtracts
*multiplies
//will divide then truncate float to an integer (floored division)
% yields remainder when first number divided by the second (modulus operator)
** exponent
multiple operators follow….
PEMDAS/operator precedence
string concatenation
joining strings by linking them end to end
use a + or can use *
operands
values operators applied to
expressions
combination of values, variables, and operators
(value all by itself is considered an expression)
input()
gets a value for a variables from the user
can pass prompt before entering with a string in ()
what the user enters is always a string
\n
represents newline, special character that represents a line break
comparison operators. list and meaning
compares two operand and returns either True or False
== equal?
!= not equal
> greater than
< less than
>= greater than or equal to
<= less than or equal to
“is”
“is not”
logical operators, list and function
and
only returns True if both statements are true
or
only returns true if either statement is true
not
negates boolean expression result
conditional execution
gives ability to check conditions and change behavior of program accordingly
ex. if (boolean):
function
boolean after an if statement known as ..
the condition
indentation portion after an if statement, else, try, etc.
the block, runs all statements in the block
if statements, try, etc are known as
compound statements, consist of more then one line
alternative execution
if (boolean):
function
else:
function
one alternative always executed, else never has conditional with it
alternatives known as branches
chain conditional
when more than 2 alternatives
if (boolean):
function
elif():
function
…..
else:
can have an infinite number of elifs
dont need an else statement, but if so should be at the end
nested conditions
when there is one condition within another
exception handling structure
used when we expect there to be an error in the program
Try:
any one which could create an error
Except:
what to do if there is an error
if try works, exception is skipped
two types of loops
indefinite and definite
indefinite loop
a while loop
indefinite # of iterations, until condition becomes False
iteration variable that changes each time with the loop
iteration
everytime we execute the body and come back to the beginning
iteration variable
the variable that changes with each time the loop executes and controls when it finishes
variable after for in the for loop is an example of an iteration variable
while loop set up and flow of execution
while (boolean):
function
evaluate condition,yield true or false
if false, exit while statement and continue at next statement
if true, execute body and go back to step one
no iteration variable results in…
an infinate loop
break statement
forces the end of a loop
usually comes in an if statement as part of a while loop
continue statement
skips the rest of the body and begins the next iteration
definite loops
ends after a set known period of iterations. # of iterations = items in the set
for iteration variable in sequence:
function
iteration variable moves through all set values in the sequence
function
a named sequence of statements
format of a function
function()
that in () is the argument
return of a function is the return value
two types of functions
built in and those we define ourselves
built in function
those provided as part of python
ex. type(), float(), int()
treated as reserved words
max()
gives max number in a sequence
in a string, this is the letter with the highest value (a is low value and uppercase less than lower)
min
gives min number in a sequence
or in a string, letter with the lowest value (a is low value)
len()
returns the number of items in an argument
for a string, this would be the number of characters
for a list and tuple, number of values
for dict, number of key - value pairs
making your own function
def funtionname()
defines, but gives no output
calling a function
using a function outside where it was defined
parameter
the variable we use in the function definition. handle that lets the code in function access arguments passed to the function when it is called
return statement
gives the value of the function back to where it was called, if we call a function but dont a return a value - we will just get “none”
fruitful function
produces a result or return value
void function
no result, or return keyword
slice
a segment of a sequence
how to get one letter from a string
string/variable[index]
how does an index match to a string
index 0 = first letter of the string
how to get a segment of a string and rules
string/variable[index:index]
returns the left index but not the right
if second number beyond end, stops at the end
how to segment from beginning of string
string/variable[:index]
how to segment to end of string
string/variable[index:]
methods
functions built into the object itself and available to any instance of the object
string methods….
can only be used with strings
dir()
returns directory of all the methods which can be used with the object and the properties of the object
string.upper()
puts all in uppercase
string.lower()
puts all in lowercase
string.find(term)
checks to see if a term exists in an existing string
if found, returns the index of the firs occurrence of the string
if not found, returns -1
word.strip()
gets rid of all spaces on the left AND right
word.lstrip()
gets rid of all spaces on the left
word.rstrip()
gets rid of all spaces on the right
string.replace(term to find, term to replace with)
find the term and puts in substitute
replaces all occurrences of the string
no error if it cant be found
list
a sequence of values
can include: dicts, tuples, integers, floats, strings, or other lists. all data types!
stored in a variable
values in a list
elements or items
separated by a coma
a list must be enclosed in
[]
2nd way to create a list
list()
way to access parts of a list
listname[index]
mutable types
lists and dictionaries
how to change a list
listname[index] = replacement value
list concaatenation
use +
prints the values from the first list, then the seconf
slicing a list from beginning
list_name[:index]
slicing a list
list_name[index:index]
slicing a list to the end
list_name[index:]
how to add items to end of list
list_name.append(term)
adds to list in order added
can repeat term
ONLY ADDS TO END, CANNOT PASS INDEX
can only append one item at a ti
how to sort a list
list_name.sort()
organizes numbers from low to high
if strings, goes in alphabetical order
how to sort a list in reverse
list_name.sort(reverse=True)
organizes numbers from high to low
if strings, goes in reverse alphabetical order
dictionary
a list, but more general. in {}. index dont have to be integers
key value pair
key- the “words” of the dictionary
value- the “meanings” of the words
aka as an item
creating a dictionary
dict()
or {} to a variable
adding items to a dictionary
dictname[newkey]=newvalue
keys can be….
strings or numbers
and CANNOT be repeated, will just update to the “last” instance
values can be…
any data type: strings, integers, floats, lists, dict, bool
duplicated as well!
accessing values in a dictionary
two methods
dictname[keyname] also used for strings and list
dictname.get(keyname) only for dict
can get all values with, returns none if not there
d.values()
traversing through keys of a dict in a loop
for key in dict:
print(key)
traversing through values of a dict in a loop, 2 methods
for variable in dictname:
print(dictname[variable]
for variable in dictname.values():
print(variable)
traversing through items/key value pairs in a loop. 3 methods
for variable in dict:
print(variable, dict[variable])
for variable in dict.items():
print(name)
will return each as tuple
for k,v in dict.items():
print(k, ";”, v)