Int / modulus

Int and modulus are two pre-defined functions that you need to know for the higher course.

  • int – converts a floating point number to an integer
  • modulus – returns the remainder from a division

Int

Converts a floating point to an integer.

# Steps tracker
steps = 10345.8  # raw sensor data
steps = int(steps) # drop decimals
print(steps)
#convert to minutes
seconds = 367
minutes = int(seconds / 60) 
print(minutes)

Modulus

  • Modulus returns the remainder from a division.
  • The modulus operator in Python is %.
print(10 % 4) # Output - 2
print(9 % 4) # Output - 1
print(100 % 33) # Output - 1
# Check if number is even
num = 17
if num % 2 == 0:
    print("Even")
else:
    print("Odd")
chairs = 23
tables = 4
print(chairs % tables)  # 3 extra chairs not evenly placed
time = 310
minutes = int(time/60)
seconds = time % 60
print(minutes, seconds)

Target

Write code to convert a floating point into an integer

Write code to calculate the remainder from a division