Data type: tuple & for loop

Objectives

a The specifics of the tuple data type, short and sweet.

Instructions

Tasks:
  1. Create a second file: tuple_and_lists.py (Note: files must not be named for reserved words, because it can lead to name conflicts!)
  2. Save your address in a tuple.
  3. Enter the address of the tuple.
  4. Use a for loop to output the tuple.

Content

Properties ...

  • Immutable like strings
  • External characteristic in the source code are the parentheses
  • Can take any object
  • Parts of a tuple can be extract. The index starts with 0.
  • Attempt to change the value of a tuple entry is not possible!

Example:

In these examples, the for loop is used for the control flow.

T1 = () # an empty tupel (considered unusual and purely academic)
t2 = ('The', 'quick', 'brown', 'fox', 'jumps', 'over', 7, 'stones')
print(t2[0], t2[1], t2[2], t2[3], t2[4], t2[5], t2[6], t2[7])
for i in t2:
    print(i, )
print()
print(t3)
t2[4] = 9 # Will that work?

As you can extract parts of a tuple is in the station slicing shown.