true division = / returns a floating point decimal result, even if the result is a whole number
floor division = // returns the largest integer less than or equal to the quotient i.e. discards the fractional part and returns a whole number
rounding down to the nearest integer = math.floor()
splitting integer into a list of the digits
digits_list = [int(digit) for digit in str(number)]separating integer into units, tens, hundreds
units = number % 10 ## output: 5
tens = ((number//10) % 10)*10 ## output: 50
hundreds = (number//100) * 100 ## output: 500seeing if a variable is a whole number
if k % 1 == 0 and k >= 0:
return f"{k} is a whole number."
else:
return f"{k} is not a whole number."to join a list into a string
- using a separator between list items =
mylist = [a, b, c]
separator = ", "
mystring = separator.join(mylist)
## output: a, b, c- for no separator =
no_space_string = "".join(mylist)
## output: abcslicing strings/list
general syntax list[start:stop:step] or string[start:stop:step]
- start = the starting index of the slice (inclusive)
- stop = the index of where the slice ends (exclusive)
- step = the interval between indices
| description | syntax |
|---|---|
| full slice (copy of the string) | s[:] |
| first character | s[:5] |
| characters from index n to end | s[n:] |
| last character | s[-1] |
| last n characters | s[-n:] |
to see if a number n is a multiple of m
def multiple(n,m):
"""
checks if n is a multiple of m.
"""
if m == 0:
return False # cannot be a mulltiple of zero
if n % m == 0:
return Truelisting multiples of 3 or 5 below n
def multiples(n):
multiples = []
for i in range(n):
if (i % 3) == 0 or (i % 5) == 0:
multiples.append(i)
print(multiples)