Coding in Python 17 – Tuples

In video number 17 of the Coding in Python series, you’ll learn about tuples.

Subscribe to LearnLinuxTV

Commands Used in this Video

Creating a tuple

#!/usr/bin/env python3


heroes = ("The Batman", "Spider-Man", "The Joker", "Wonder Woman")
print(type(heroes))
print(heroes)

Comparing tuples and lists

#!/usr/bin/env python3


heroes_tuple = ("The Batman", "Spider-Man", "The Joker", "Wonder Woman")
heroes_list = ["The Batman", "Spider-Man", "The Joker", "Wonder Woman"]

print(heroes_tuple)
print(heroes_list)

Iterating over a tuple

#!/usr/bin/env python3


heroes = ("The Batman", "Spider-Man", "The Joker", "Wonder Woman")

for h in heroes:
    print(h)

Using len() to view length of a tuple (works for other object types too)

#!/usr/bin/env python3


heroes = ("The Batman", "Spider-Man", "The Joker", "Wonder Woman")

for h in heroes:
    print(h)


print(len(heroes))