Python tuples

we are going to be looking at Python Tuples.

Some characteristics of tuples are:

Tuple data never change.
Tuples are used to store multiple items in a single variable.
Tuples are ordered and unchangeable.
Since tuples are indexed, they can have items with the same value.
Â
Â

we start by creating a tuple called

“tuple1”.Â

Notice the type of parenthesis used.

  tuple1 = (1, 4, 5, 6)

Here we access the first element of the tuple.

  tuple1[0]

Here we access the third element in the tuple.

note that tuple’s are 0 indexed.

Â

  tuple1[2]

Here we access the first element and second elements. indexed 0 and 1, but not the third( index 2 is excluded)

  tuple1[0:2]

Here we create a copy of the entire tuple using slicing.

Â

  tuple1_copy = tuple1[:]

Next we check the length of the tuple1 using the len() function

  len(tuple1)

Here we get the max number in the tuple1 using the max() function

  max(tuple1)

Here we check for the min number in tuple1 using the min() function

  min(tuple1)

Here we create a tuple with one element

  tuple2 = ('Ryan', )

here we create an empty tuple

  tuple3 = ()

Here, we create tuples of mixed items.

  tuple4 = ('Nolan', 1, 1, True)

we cannot modify tuples but we can create a new tuple from existing ones

  tuple5 = tuple1 + tuple4

we use the “del” to delete a tuple

  del tuple5

Here we create a nested tuple where tuple1 and tuple4 are stored as seperate elements inside tuple6

  tuple6 = (tuple1, tuple4) tuple6

Here, we convert a list to a tuple using the tuple() constructor.

The result is an immutable version of the list

  #convert list to tuple list = [1, 4, 8, 19, 2, 3] list_tuple = tuple(list)

Here, we extract the values from a dictionary and convert them into a tuple, resulting in “(1,2,3)”

  dict = {'a': 1, 'b': 2, 'c': 3} # Converting dictionary values to a tuple dict_tuple = tuple(dict.values()) print(dict_tuple)

This creates a new tupple by repeating the contents of tuple2 six times in a row

  tuple7 = tuple2 * 6

Here, we check if the value 6 exists in tuple1. it returns True if found otherwise FalseÂ

  6 in tuple1

This checks if the value 8 is absent from tuple1.

It returns True if 8 is not in the tuple.

  8 not in tuple1

Here we are comparing two different tuple using various comparism operatrors.

  print(tuple1 == tuple2) print(tuple1 != tuple2) print(tuple1 < tuple2) print(tuple1 > tuple2) print(tuple1 <= tuple2) print(tuple1 >= tuple2)

We create a tuple of Cricket players

  Cricket_Players = ('Bradman','Trumper','Hobbs', 'Ranji')

Next we loop through the cricket players by itertng over it.

  for Cricketer in Cricket_Players: print(Cricketer)

This line unpacks the Cricket_Players tuple into four separate variables: Cricketer_1 to Cricketer_4, each holding one value from the tuple.

  Cricketer_1, Cricketer_2, Cricketer_3, Cricketer_4 = Cricket_Players

Here we create another tuple called “tuple8”

  tuple8 = (3, 6, 8, 1, 5, 2)

Using the sort() function, we sort the tuple8

  sorted(tuple8)

This returns a new list with the elements of tuple8 sorted in descending order( largest to smallest)

  sorted(tuple8, reverse=True)

We create another tuple called “tuple9”

  tuple9 = (1, 2, 3, 1, 1)

Next we attempt to modify the tuple. This would result in an error

  tuple9[0] = 1

This prints the number of times the value “1” appears in the uple “tuple9”

  print(tuple9.count(1))

This returns the index of the first occurrence of the value 2 in tuple9

  print(tuple9.index(2))

Tbis function calculates and returns both the area and perimeter of a square based on the given length

  def square_calcs(length): area = length * length perimeter = 4 * length return area, perimeter

This calls the square_calcs function with 5 as the length

  result = square_calcs(5)

This accesses the area from the results tuple  retuned by square_calcs(5)

  result[0]

This retrieves the perimeter from the result tuple returned by square_calc(5)

  result[1]

Here, we unpack the result tuple into two seperate variables “area” and “perimeter”.

  area, perimeter = result
  area
  perimeter

Ryan is a Data Scientist at a fintech company, where he focuses on fraud prevention in underwriting and risk. Before that, he worked as a Data Analyst at a tax software company. He holds a degree in Electrical Engineering from UCF.

Leave a Reply

Your email address will not be published. Required fields are marked *