Collective Data Structures: Tuples

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

How can we swap two variables a and b?

How can we swap two variables a and b? With conventional assignments, use a temporary variable. temp = a a=b b = temp Tuple assignment is more elegant: a, b = b, a Note: # of variables on the left must be equal to # of values on the right: e.g. a, b = b, a, c is incorrect

index(x)

Return index of first item that is equal to x

Tuples: Repetition

The * operator repeats a tuple n times.

Tuples: Concatenation

The + operator concatenate two tuples.

A value in parentheses is...

not a tuple: t2 = ('a') #t2 is not a tuple, t2 is a string

Tuples:

-A tuple is a sequence of values. The values can be of any type, and they are indexed by integers, so in that respect tuples are similar to lists. -Unlike lists, tuples are immutable.

Tuple: delete del

-Because a tuple is immutable, we cannot change or delete the items in a tuple. -The del keyword deletes the entire tuple.

Tuples: Indexing

-Like strings, each element in a tuple has an index. The index operator [ ] accesses an element in the tuple. -Negative indexing works the same way. Index -1 gets the last item, -2 the second last item etc.

Creating Tuples:

-Placing all the items inside a parentheses (), separated by comma. The parentheses are optional. -A tuple can have items of different datatypes (int, float, list, string, ...) -Syntactically, a tuple is a comma-separated list of values: t = 'a', 'b', 'c', 'd', 'e' -It is common to enclose tuples in parentheses: t = ('a', 'b', 'c', 'd', 'e')

tuple() constructor

-The built-in function tuple() creates a tuple. -With no argument, it creates an empty tuple: t = tuple() -If the argument is a sequence (string, list or tuple), the result is a tuple with the elements of the sequence: t = tuple('python') >>> t ('p', 'y', 't', 'h', 'o', 'n')

Tuples: Slicing Operators

-To slice a range of elements in a tuple, or sub-tuple, use the slicing operators [:] or [::] with a positive or negative stride -With a negative stride, the ordering changes since we are counting down

Changing a Tuple:

-Unlike lists, tuples are immutable. -Elements of a tuple cannot be changed once we create the tuple object. -We can assign a tuple to different values (reassignment).

tuple()

Convert an iterable (list, string, set, dictionary) to a tuple.

all()

Return True if all elements of the tuple are true (or if the tuple is empty).

any()

Return True if any element of the tuple is true. If the tuple is empty, return False.

sorted()

Return a new sorted tuple (does not sort the tuple itself).

enumerate()

Return an enumerate object. It contains the index and value of all the items of tuple as pairs.

max()

Return the largest item in the tuple.

len()

Return the length (the number of items) in the tuple.

count(x)

Return the number of items that is equal to x

min()

Return the smallest item in the tuple

sum()

Return the sum of all elements in the tuple.

To create a tuple with a single element, include...

a final comma: t1 = ('a',) #t1 is a tuple

tuple.count(x)

tuple.count(x) counts the number of times the item x appears.

tuple.index(x)

tuple.index(x) returns the 1st index where the item x is found.


Set pelajaran terkait

MO L&H - Chapter 4: Retirement and Other Insurance Concepts

View Set

European History 1500-1789: Midterm IDs [Dates]

View Set

Mr. Griffin Pre-Simulation vSim Quiz

View Set