Skip to main content
  • Python is available now. All other compilers are coming soon.

    Python
  • Popular Tutorials

    • Getting Started With Python
    • Python if Statement
    • while Loop in Python
    • Python Lists
    • Dictionaries in Python
    All Python Tutorials

    Reference Materials

    Built-in FunctionsList MethodsDictionary MethodsString Methods
    View all

Online Compilers

  • Python Compiler
  • Python Playground
  • Python Projects
  • Exercises

Tutorials

  • Learn Python
  • Python for Beginners
  • Variables and Types
  • Python Strings
  • Python Lists
  • Python If Else
  • Python For Loops
  • Python Functions
  • Python Classes

Examples

  • Python Examples
  • Hello World
  • Add Two Numbers
  • Even or Odd
  • Factorial
  • Fibonacci
  • Prime Check
  • Palindrome Check

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Use
  • Disclaimer

© 2026 ILoveCompiler. All rights reserved.

  1. Home
  2. Python Examples
  3. Tuple Unpacking

Tuples · Python example

Tuple Unpacking

Unpack a coordinate tuple and calculate its distance from the origin.

Open the compilerAll examples
beginnertuplesunpackingmath

Tuples group related values that should not change. Unpacking assigns each item to its own variable in one statement.

example.py
point = (4, 7)
x, y = point

distance = (x ** 2 + y ** 2) ** 0.5

print(f"x={x}, y={y}")
print(f"Distance from origin: {distance:.2f}")

How the code works

  • point holds two integers.
  • Unpacking binds x to 4 and y to 7.
  • The square root of 65 is about 8.06.

Key ideas

  • A tuple is written with parentheses and commas.
  • x, y = point is tuple unpacking.
  • The distance formula is √(x² + y²).

Try changing it

  • Return two values from a function and unpack the result.
  • Use a tuple to store an RGB colour.
  • Try to change an item to see why tuples are immutable.

Related examples

beginner

Variables and Data Types

Create strings, integers, floats, and booleans, then inspect each type.

beginner

List Statistics

Compute count, sum, minimum, maximum, and average for a list of numbers.

Previous exampleRemove Duplicates while Preserving OrderNext exampleWord Frequency Counter

Expected output

x=4, y=7
Distance from origin: 8.06

Output is compared after trimming trailing whitespace on each line. Run the code to see it produced by CPython in your browser.

Practice next

Apply the same idea with automatic tests or browse a guided project.

ExercisesProjects