Coding in Python 17 - Tuples

From LearnLinux.tv Wiki
Jump to navigationJump to search

Overview

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

Relevant Links
Original Video


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))