Tuple
Part of the Fundamentals section of Coddy's Python journey — lesson 59 of 77.
A tuple is an immutable (read-only) ordered collection of elements. Unlike lists, once created, tuples cannot be modified.
Create a tuple with parentheses:
fruits = ("apple", "banana", "cherry")You can also create a tuple without parentheses:
colors = "red", "green", "blue"Access tuple elements using indexing (just like lists):
first_fruit = fruits[0] # "apple"Remember, tuples are immutable, so you cannot modify elements:
# This will cause an error
# fruits[0] = "orange"A tuple with a single element needs a trailing comma:
single_item = ("apple",) # This is a tuple
not_a_tuple = ("apple") # This is a stringCheat sheet
Tuples in Python:
- Immutable (read-only) data structure
- Created using parentheses
() - Access elements using indexing, including negative indexing
# Creating a tuple
coordinates = (4, 5)
# Accessing elements
x = coordinates[0] # 4
y = coordinates[1] # 5Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 48Loops
For LoopWhile LoopBreakContinueRecap - FactorialThe Range FunctionNested LoopRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators9Functions
Declare a FunctionArgumentsReturnRecap - Sigma FunctionRecap - Validation FunctionDefault Values12Iterating Over Sequences
Iterating Over ElementsThe Enumerate FunctionIterating Over Strings Part 1Iterating Over Strings Part 2