Skip to content

Mojo Data Types

Every value in Mojo has a data type: it’s a whole number, a decimal number, true or false, some text, and so on. The type decides what you can do with the value and how much memory it takes. This page is a tour of the types you’ll use every day. The next few lessons go deeper into numbers, strings and booleans one at a time.

These four cover most beginner programs:

Type What it holds Example
Int Whole numbers, positive or negative 42, -7, 1_000_000
Float64 Decimal numbers 3.14, -0.5, 2.5e-3
Bool True or False True
String Text "Hello", 'Mojo'

Here they are in a program:

def main():
var age: Int = 36
var height: Float64 = 1.75
var is_member: Bool = True
var name: String = "Ada"
print(name, age, height, is_member)

Output:

Ada 36 1.75 True

All four are built in, so you don’t need to import anything to use them.

You don’t have to write the type every time. When you leave it out, Mojo looks at the value and picks a default:

You write Mojo picks
A whole number, like 42 Int
A number with a decimal point, like 9.99 Float64
True or False Bool
Text in quotes, like "hi" String

You can ask Mojo what type a value has with reflect[type_of(x)].name():

def main():
var count = 42
var price = 9.99
var ready = True
var label = "hi"
print(count, reflect[type_of(count)].name())
print(price, reflect[type_of(price)].name())
print(ready, reflect[type_of(ready)].name())
print(label, reflect[type_of(label)].name())

Output:

42 SIMD[DType.int, 1]
9.99 SIMD[DType.float64, 1]
True Bool
hi String

Bool and String look as expected, but what’s SIMD[DType.int, 1]? That’s the full name of Int. In Mojo, number types are built on SIMD, a type that can hold several numbers at once so the CPU can work on them together. A plain Int is just a SIMD with room for one number. You don’t need to think about this yet: write Int and Float64 like normal names. It only matters later, when you want speed.

Int uses your machine’s native size, which is 64 bits on almost every computer today. When the exact size matters (file formats, pixels, network data, or saving memory), Mojo has sized number types. The number in the name is how many bits it uses:

Kind Types
Signed whole numbers Int8, Int16, Int32, Int64, Int128, Int256
Unsigned whole numbers (no negatives) UInt8, UInt16, UInt32, UInt64, UInt128, UInt256, and UInt (machine size)
Decimal numbers Float16, Float32, Float64 (and a few special ones for AI work)

Each type has its own range. You can print it with .MIN and .MAX:

def main():
print("UInt8:", UInt8.MIN, "to", UInt8.MAX)
print("Int8:", Int8.MIN, "to", Int8.MAX)
print("Int32:", Int32.MIN, "to", Int32.MAX)
print("Int:", Int.MIN, "to", Int.MAX)

Output:

UInt8: 0 to 255
Int8: -128 to 127
Int32: -2147483648 to 2147483647
Int: -9223372036854775808 to 9223372036854775807

There’s no plain Float type in Mojo. A decimal number without an annotation is a Float64. The Mojo numbers lesson covers all of these in detail.

Mojo won’t quietly turn one type into another. Multiplying an Int variable by a Float64 variable is an error:

def main():
var apples = 3
var weight = 0.25
print(apples * weight)
hello.mojo:4:20: error: invalid call to '__mul__': value passed to 'rhs' cannot be converted from 'Float64' to 'Int'
print(apples * weight)
~~~~~~~~~^~~~~~
mojo: error: failed to parse the provided Mojo source module

(The real output also has a note: pointing into the standard library; we left it out.) The fix is to convert one side yourself, so it’s clear what you want:

def main():
var apples = 3
var weight = 0.25
print(Float64(apples) * weight)

Output:

0.75

Float64(apples) makes a Float64 copy of 3, so both sides have the same type. The Mojo Type Conversion lesson shows all the ways to convert.

A number you type straight into the code, like 42, is a literal. Literals are a bit special: they don’t have a fixed type until they’re used. So you can write a whole-number literal into any number type:

def main():
var small: UInt8 = 200
var wide: Int64 = 9_000_000_000
var ratio: Float32 = 42
print(small, wide, ratio)

Output:

200 9000000000 42.0

42 became a Float32 (42.0) because that’s what the annotation asked for. This only works one way, though: a decimal literal can’t become a whole-number type. var price: Int = 9.99 fails to compile with constraint failed: the SIMD type must be floating point. Mojo won’t drop the .99 for you without being told.

The Mojo types manual explains literals and materialization (how a literal turns into a real runtime value) in more depth.

Besides single values, Mojo has types that hold several values. You’ll get a full lesson on each later, but here’s a first look:

def main():
var scores: List[Int] = [90, 85, 77]
var point = (3, 4)
print(scores)
print(point)
print(point[0], point[1])

Output:

[90, 85, 77]
(3, 4)
3 4
  • List[Int] is a list of Int values that can grow and shrink, like a Python list. Every item has the same type. See Mojo Lists.
  • (3, 4) is a tuple: a fixed group of values, which can have different types. See Mojo Tuples.
Python Mojo
Whole numbers int, any size Int (64-bit on most machines), plus Int8 to Int256, UInt8 to UInt256
Decimal numbers float (64-bit) Float64 (the default), Float32, Float16
True/false bool Bool
Text str String
Type annotations Optional hints Checked by the compiler
3 * 0.25 with variables Works, gives a float Error: convert with Float64(...) first

The biggest change: Python’s int can grow as big as you like, but Mojo’s Int has a maximum. What happens when you go past it is covered in Mojo Integer Overflow.

Write a program with four variables, each with a type annotation: a city name ("Lisbon"), its population (545923), its average temperature (17.5), and whether it’s a capital (True). Print them all on one line.

Hint: you need String, Int, Float64 and Bool.

Show solution
def main():
var city: String = "Lisbon"
var population: Int = 545923
var avg_temp: Float64 = 17.5
var is_capital: Bool = True
print(city, population, avg_temp, is_capital)

Output:

Lisbon 545923 17.5 True

Each annotation matches the kind of value: text, a whole number, a decimal number and true/false.

This program fails to compile. Make it print Total: 13.5.

def main():
var quantity = 3
var unit_price = 4.5
print("Total:", quantity * unit_price)

Hint: quantity is an Int and unit_price is a Float64.

Show solution
def main():
var quantity = 3
var unit_price = 4.5
print("Total:", Float64(quantity) * unit_price)

Output:

Total: 13.5

Mojo doesn’t mix Int and Float64 on its own, so you convert quantity with Float64(...) first.

  • The everyday types are Int, Float64, Bool and String, all built in.
  • Without an annotation, whole numbers become Int, decimals become Float64.
  • Sized types like UInt8, Int32 and Float32 have a fixed number of bits and their own .MIN and .MAX.
  • Number types are built on SIMD, which is why Int shows up as SIMD[DType.int, 1].
  • Mojo never mixes types for you: convert with Float64(x), Int(x) and friends.
  • Literals adapt to the type you ask for, but a decimal literal can’t become an Int.

Next up: numbers, a closer look at integers, floats and how to write number literals.

Quick check

1. What type does var price = 9.99 have?
2. What happens with var a = 3 and var b = 0.5, then print(a * b)?
3. Which type holds whole numbers from 0 to 255?
4. Why does printing the type of an Int show SIMD[DType.int, 1]?