+91 81900 98289

Python Tuple Methods

This Python Tuple Methods lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.

count and index are the only tuple methods. The rest of the work is indexing, slicing, and unpacking.

Tuples are useful as dict keys because they cannot be changed.

Example 1: Start here

A one-item tuple needs a trailing comma.

point = (13, 80) # a tuple; a fixed group of values print(point.count(13)) # how many times it appears print(point.index(80)) # where the first match sits

Example 2: Store the idea in a name

Python Tuple Methods begins with a value you can point at. The name topic holds that text.

topic = "Python Tuple Methods" # text print(topic)

Example 3: Check it with if

An if chooses one branch. Change the score and run it again to see the other message.

score = 33 # a whole number if score >= 80: # run the indented lines only when this is true print("Pass") else: # runs when the test above was false print("Try again")

Example 4: Repeat with a loop

A for loop runs the indented line once per step. The numbers 1, 2, and 3 are printed on their own lines.

for step in range(1, 4): # repeat once per number; range stops before the last print("Step", step) # a comma prints each value, with a space between
A one-item tuple needs a trailing comma.
Call Now 81900 98289