Maths Functions Flashcards
Power Of
**
Round Down
//
Remainder
%
Precedence
- before +
Strings use escape characters
x = “Her name's "Elizabeth"”
id(varName)
This gives the address
type(varName)
This gives the type, like ‘int’
Name 7 types
bool, int, float, string, tuple, list and dict
Casting
varName = int(10.5)
Multiline string
’’’ OR ‘’’\ The 2nd version escapes the carriage return
Define a Tuple
varTuple = (1,2,3,4)
Define a List
varList = [1,2,3,4)
Define a Dict
varDict = {‘a’:1, ‘b’:2}
Get items 2 and 3 from a list
varList[1:2]
While
While a<100:
print(a)
a+=1
For data in
For data in [1,2,3,4]:
print (data)
For data in
s t r i n g
enumerate
for key, data in enumerate(‘strings’):
try syntax
tuple = (1,2,3,4,5) try: tuple.append(6) except AttributeError as e: print("error", e) except IOError as e: print("ioerror", e) else: for each in tuple: print tuple
When is an exception thrown
As soon as the error is hit
loop - break and continue
break breaks out of the loop
continue jump to next iteration
Does “else” also work in for loops
yes, always gets run assuming successful loop through
How do you work back to front on a list
list[-1]
length of a list
print(len(listName))
How do you 2 step through a list
list[2:4:2]
How do you 2 step through a list to the end, no matter where it is?
list[2::2]
How do you 3 step through a list from the start to item 4 ?
list[:3:3]