Beginner·16 min·0/47 done

Python Numbers

Learn Python number types: int, float, and complex. Then see how they show up in AI / ML.

Why it matters: Scores, learning rates, batch sizes, and seeds are all numbers.

Basics

Python Numbers

There are three numeric types in Python:

  • int
  • float
  • complex

Variables of numeric types are created when you assign a value.

To check the type of any object, use the type() function.

Tiny example

Create one int, one float, and one complex number.

x = 1        # int
y = 2.8      # float
z = 1j       # complex
print(type(x))
print(type(y))
print(type(z))

Basics

Int

Int, or integer, is a whole number, positive or negative, without decimals.

In Python, integers can be very large. You do not need to worry about a fixed size limit.

Tiny example

Integers are whole numbers. No decimal point.

x = 1
y = 35656222554887711
z = -3255522
print(type(x))
print(type(y))
print(type(z))

Basics

Float

Float, or floating point number, is a number with one or more decimals.

Float can also use scientific notation with e for power of 10:

x = 35e3
y = 12E4
z = -87.7e100
print(type(x))

Common mistake: writing a learning rate as the string "2e-5" instead of the float 2e-5.

Tiny example

Floats have a decimal point.

x = 1.10
y = 1.0
z = -35.59
print(type(x))
print(type(y))
print(type(z))

Basics

Useful Number Operators

Useful operators:

  • / normal divide (always gives float)
  • // floor divide (whole number result)
  • % remainder
  • ** power

Also useful: abs(), round(), min(), max(), sum().

Tiny example

Try divide, floor divide, and remainder.

print(7 / 2)
print(7 // 2)
print(7 % 2)
print(2 ** 10)

Then AI / ML

AI / ML examples

Same idea, used in real AI work. Press Try in lab to run it.

AI / ML example

AI / ML: learning rate and batch size

Same number types, now used like a tiny training config.

In AI / ML: Learning rates are floats. Batch sizes and epochs are ints.

example.py

Python

learning_rate = 2e-5   # float
batch_size = 32        # int
epochs = 3             # int
print(type(learning_rate))
print(1000 // batch_size)

AI / ML example 2

AI / ML: keep or drop a score

A float score plus a simple comparison.

In AI / ML: Classifiers often keep predictions only when score >= threshold.

example.py

Python

score = 0.82
threshold = 0.7
if score >= threshold:
    print("keep")
else:
    print("drop")

Takeaways

  • 1Python has three number types: int, float, and complex.
  • 2Use type() to check what kind of number you have.
  • 3In AI / ML, scores and rates are floats; counts are ints.
Certificate