In Mojo, data types play an important role in performing multiple operations like it is used as holding variable values and storing those values in memory. It makes your program efficient, predictable, and bug-free.
Let’s understand why data types are important in Mojo.
Mojo is designed to combine the ease of coding like Python with the speed of execution like C++ and Rust. For this system, data types can easily optimize performance by using low-level memory representation, prevent type-related bugs at compile time, and make the code faster and safer than a dynamically typed language.
There Are Multiple Data Types in Mojo
Now, we will understand the set of primitive (basic) and complex data types.
- Integers (Int, Int8, Int16, etc.)
- Floating-Pont Numbers (Float32, Float64)
- Boolean (Bool)
- Strings (String)
- Tuples
- Arrays (Static Arrays)
- Optional
1) Integers (Int, Int8, Int16, etc.)
Integers store all numbers, including positive and negative values, without decimals. Mojo provides different big sizes (Int8, Int16, Int32, Int64) for memory optimization.
See this example of Integer data types:
fn main():
let apples: Int = 10
let oranges: Int16 = 200 # Small, memory-optimized integer
print("Total fruits:", apples + oranges)
Output:

Int is a default integer type, while Int16 is a 16-bit integer that saves memory when dealing with large arrays or low-level systems code.
2) Floating-Point Numbers (Float32, Float64)
Floating-point numbers are used for decimal and fractional numbers. Float32 is a single-precision, and Float64 is a double-precision, based on performance.
Understand the precision by this example:
fn main():
let temperature: Float32 = 23.5
let pi_value: Float64 = 3.14159265358979
print("Room Temperature:", temperature)
print("Pi Approximation:", pi_value)
Output:
Room Temperature: 23.5
Pi Approximation: 3.14159265358979
Note: Float32 is faster and uses less memory, while Float64 is more suitable for scientific or highly accurate calculations.
3) Boolean (Bool)
Boolean represents the True and False values. This data type is used for decision-making, loops, and conditions.
fn main():
let is_logged_in: Bool = True
let is_admin: Bool = False
if is_logged_in and not is_admin:
print("Welcome, user!")
else:
print("Admin Panel Access Granted")
Output:
Welcome, user!
4) Strings (String)
Strings are sequences of characters, and are commonly used for text, messages, and labels. Mojo strings are also Unicode-compatible.
fn main():
let username: String = "MojoLearner"
let greeting: String = "Welcome, " + username + "!"
print(greeting)
Output:

5) Tuples
Tuples are immutable collections that can store multiple different types of values. This data type is useful for grouping data without creating custom structures.
fn main():
let user: (String, Int, Bool) = ("Alice", 25, True)
print("Name:", user[0])
print("Age:", user[1])
print("Active:", user[2])
6) Arrays (Static Arrays)
Static arrays mean it is allow fixed-size and type-safe storage, while Mojo supports dynamic collections through libraries.
fn main():
let scores: [Int; 3] = [85, 90, 78] # Fixed size: 3 elements
for s in scores:
print("Score:", s)
Static arrays are faster because the compiler knows the size of that.
7) Optionals ( ? )
Python uses a None method, but Mojo uses Optionals for safer null-handling because sometimes a value can be absent (null).
fn greet_user(name: String?):
if name != None:
print("Hello,", name)
else:
print("Hello, Guest!")
fn main():
greet_user("Mojo Dev")
greet_user(None)
Output:

Optionals avoid null pointer crashes and making your program more robust.
Type Inference vs. Explicit Types
Mojo programming supports type inference, so we don’t always have to specify a type. The compiler automatically figures out the variable’s type based on the assigned values. For example:
fn main():
let x = 42 # Automatically inferred as Int
let y = 3.14 # Inferred as Float64
print(x, y)
This example shows a type inference. However, explicit typing is more recommended in large applications for better performance and clarity.
Real-World Example of Data Types
Now, we are create a Simple Cafe Billing System where:
- Customers order food,
- The system tracks items, prices, and discounts,
- It shows the bill, and
- Uses every data type logically.
Cafe Billing System
fn main():
# --- Using Strings, Integers, and Floats ---
let cafe_name: String = "Mojo Cafe"
let tax_rate: Float32 = 5.0 # 5% tax
let discount_active: Bool = True
let discount_percent: Float32 = 10.0 # 10% discount if active
# --- Using Array for menu prices (fixed size) ---
let item_names: [String; 3] = ["Coffee", "Sandwich", "Cake"]
let item_prices: [Float32; 3] = [80.0, 120.0, 150.0]
# --- Using Tuple for storing customer order ---
# Format: (Customer Name, Ordered Items as Indices, Tip Amount as Optional)
let customer_order: (String, [Int; 3], Float32?) = ("Alice", [1, 2, 0], 50.0)
print("Welcome to", cafe_name)
print("Customer:", customer_order[0])
# --- Calculate total bill ---
var subtotal: Float32 = 0.0
for index in customer_order[1]:
subtotal += item_prices[index]
print("- Ordered:", item_names[index], "₹", item_prices[index])
# --- Apply discount if active ---
if discount_active:
let discount_amount = (subtotal * discount_percent) / 100
subtotal -= discount_amount
print("Discount Applied: -₹", discount_amount)
# --- Add tax ---
let tax_amount = (subtotal * tax_rate) / 100
let total = subtotal + tax_amount
# --- Add optional tip (if any) ---
var final_total = total
if customer_order[2] != None:
final_total += customer_order[2]!
print("Tip Added: ₹", customer_order[2]!)
print("-------------------------------")
print("Subtotal (After Discount): ₹", subtotal)
print("Tax (5%): ₹", tax_amount)
print("Final Total: ₹", final_total)
print("Thank you for visiting", cafe_name, "!")
Console Output of this program:
Welcome to Mojo Café
Customer: Alice
- Ordered: Sandwich ₹120.0
- Ordered: Cake ₹150.0
- Ordered: Coffee ₹80.0
Discount Applied: -₹35.0
Tip Added: ₹50.0
-------------------------------
Subtotal (After Discount): ₹315.0
Tax (5%): ₹15.75
Final Total: ₹380.75
Thank you for visiting Mojo Cafe!
Understand this program code by yourself and write it with your logic.
Simple explanation about this program:
- Strings – cafe_name, item_names, and customer_order name.
- Integers – item indices for menu selection.
- Floats – Prices, discounts, tax, and tip for accurate billing.
- Booleans – discount_active to check if a discount should apply.
- Tuples – Store customer order details in one variable.
- Arrays – Store menu items and their prices (fixed size for speed).
- Optionals ( ? ) – Tip may or may not be added by the customer.
Your Exercise of The Mojo Data Types
Exercise 1: Grocery Cart Calculator
Create one program that can do the following terms:
- Stores the prices of 3 grocery items (like Rice, Milk, Bread) in an array.
- Calculates the total bill for a customer who buys one of each.
- Adds a 5% tax.
- Prints the final bill amount with a message.
Hint for you:
- Use Float32 for prices.
- Use an Array for storing prices.
- Perform a simple math calculation for the total.
Your output should match with this:
Your total grocery bill (with 5% tax): ₹315.75
Thank you for shopping!