Showing posts with label Python Tuples. Show all posts
Showing posts with label Python Tuples. Show all posts

September 1, 2021

Python Tuples

Tuples in Python

Tuples are immutable versions of lists.
A tuple is a collection of values, and declare it using parentheses, ( and ).

immute_set=('one', 2, 'three', 'banana')
>>> tup_python = (23, 'abc', 4.56, (2,3), 'def')
a, b, c = tuple_with_3_values
>>> mytuple=((1,2),(3,(4,5),(6,(7,(8,9)))))
(count, i, list) = (0, 1, [])
(m, n) = (n, m) # swapping two values

tuple2[i]         #ith element, from left
                      Positive index: count from the left, starting with 0
                      Negative index: count from right, starting with –1
tuple1[16]
tuple_py[-4]

tuple2[m:n]    #n elements from mth position
                     Return a copy of the container with a subset of the original members.
                     Start copying at the first index, and stop copying before second. m ≤ i < n.
tup_python[m:n:increment]
tup[12:16]
tup[2:8:2]
>>> mytuple[1][2][1][1][0]
>>> t[::-1]

len(py_tup)
tup_python[len(tup)::-1]                 # to reverse tuple

tuple9.index('b')
>>> (1,3,2).index(3)

tuple9.count('b')
print(sorted(pyTuple))
min(tup)
>>> max((1,3,-1))

python_tu = tuple(li)
tuple2 = tuple1

type(python_tu)

>>> t[:]


>>> myTuple * 3

>>> [x*y for x in vec1 for y in vec2]

>>> [3*x for x in vec if x > 3]


Related Python Articles:  Sets in Python            Dictionaries in Python