1/42
Looks like no tags are added yet.
addition
+
(e.g 10+5)
subtraction
-
(e.g. 10-5)
multiplication
*
(e.g 10*5 )
division
/
(e.g. 10/5)
Integer division
//
(e.g. 14//5)
(note: always round down, 14//5=2
Power
**
(e.g 2**2)
Modulus
%
(e.g 14%5 will give you 4)
(note: will give you the remainder of a division)
Parentheses
()
(change the order of operations)
Test for equality
==
(e.g. a==b)
(note: will give boolean answer)
Test for inequality
(e.g. a! = b)
(note; will give boolean answer)
Greater/less than
a>b, a<b
(note: will give boolean answer)
Greater/less than or equal to
a>=b, a<=b
(note; will give boolean answer)
converting number to boolean
bool(someData)
(e.g. bool(0) gives False)
converting a float to integer
int(someData)
(e.g. int(7.2) gives 7)
(note; always rounds down so int(7.8) also gives 7)
converting integer to float
float(someData)
(e.g. float(2) gives 2.0)
convert float to string
str(someData)
(e.g. str(17) gives “17”)
convert string to float
float(“someData”)
(e.g. float(“165.4”) gives 165.4)
converting to complex number
complex(someData)
(e.g. complex(5) returns 5+0j)
logical operations overview
performed between boolean variables and result in booleans
Logical “and” operation
value1 and value2
Yields True if BOTH value1 or value2 are True
Yields False otherwise
Logical “or” operation
value1 or value2
Yields True if EITHER value 1 or vlue2 are True
Yields False otherwise
Logical “not” operation
not value
Yields True if value is False
Yields False otherwise
What will addition (+) do to two strings
concatenate them (bring the together)
(e.g. a= “Hi!” b= “How are you?”, a+b returns “Hi! How are you?”
What will multiplication with an integer (*) do to strings
repeat the string multiple times
(e.g. a= “Hi! ”, 4*a returns “Hi! Hi! Hi! Hi! ”)
How would you alter a string
via the object.method()
(e.g. string= “Hi, how are you?”, string.lower() returns “hi, how are you?”)
(note: write your stringName.[then press tab] to open method options)
Does altering a string replace the old string or create new objects
create new objects
What does addition (+) do to lists
concatenates them (adds them together)
(e.g. a=[1,2,3] b=[True, “Bananas”, 17, 28.3], a+b returns [1, 3, 4, True, 'Bananas', 17, 28.3]
What does integer multiplication (*) do to lists
repeats them multiple times
(e.g. a=[1,2,3], 3*a returns [1,2,3,1,2,3,1,2,3])
how do you access elements of a list
aList[index]
(e.g. a=[20,51,-17,3,65,4,812], a[4] returns the 4th term 65)
note: Python uses 0 based numbering so 1st element has index 0
note: can use negative indices (e.g. a[-1] returns 812)
How would you determine the length of a list
len(aList)
(e.g. a=[20,51,-17,3,65,4,812]
how would you select multiple elements of a list
slicing the list, aList[start:stop:step]
start is the first element selected
stop is the first element not selected. If none selected will run till end
step is the step used between two consecutive elements. If none provided a value of 1 is used
(e.g. a=[1,2,3,4,5,6,7,8,9,10,11,12]
a[3:] returns [4, 5, 6, 7, 8, 9, 10, 11, 12]
a[0:8:2] returns [1, 3, 5, 7]
note: inside the slicing list it is elements not numbers (a[0] returns 1)
How would you add an element to the end of a list
aList.append(someData)
(e.g. a = [1,2,3,4,5,6,7,8,9]
a.append(10)
print(a) returns [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
how would you count the times a number appears in a list
aList.count(number)
how would you remove the first occurrence of an element
aList.remove(element)
how would you reverse the list order
aList.reverse()
how would you sort a list into ascending order
aList.sort()
sort the list in descending order
aList.sort(reverse=True)
removes all contents from a list
aList.clear()
replace an element in a list
aList[0]=newNumber (would replace first element)
how do you determine the type of variable
type(value)
e.g type(“hat”) returns str
type(True) returns bool
type(3) returns int
etc
How do you get the absolute value
abs(number)
(e.g. abs(-9) returns 9
How do you determine the minimum/maximum
min(value1, value2, value3, value4)/max(value1, value2, value3, value4)
min(9,0.001,10,1e-32) returns 1e-32
max(9,0.001,10,1e-32) returns 10
How do you round to nearest integer or specific number of decimal places
round(value), round(value,n)
round(1.8) returns 2
round(1.87636430,5) returns 1.87636 (5dp)