Python Tuples

This is a quick Python post on tuples. Lists in Python can have its elements changed while a tuple is a sequence of objects where the contents in a tuple cannot be changed.

Reference: Starting Out With Python – Second Edition By Tony Gaddis

 


Examples Of Tuples

 

Example One

This first example features printing even numbers from 1 to 20 (inclusive).

 

Example Two

The second example features a tuple of five student names. Names from the tuple can be printed in two ways. The first way of printing the names does not use an index while the second way does use an index j, the range() and len() functions.

 

Example Three

A tuple with one element needs a comma and a space. If there is a number with brackets and no commas, Python will recognize it as a single value instead of a tuple with one element.

 

 

 

 

 

Leave a Reply