<jupyter_start><jupyter_text>Tuple - A tuple is a sequence of immutable Python Objects. A tuple is an ordered collection of immutable elements (cannot be changed). > It is similar to lists, but there are some differences. Tuples are immutable but lists are.> Lists are denoted by square brackets [] and tuples by round brackets ()> Tuples are separated by commas (,) like lists. WHhen to use tuple*   When you need to complete the task in a short time (Tuple has less execution time*   Where you have to deal with values which can not be change  Syntax<jupyter_code># Synatx

tuple1 = ('vijay', 'pattar', 'karnataka')

tuple2  = (1,2,3)

tuple3 = "x","y","z"

tuple4 = 'a','b','c'

#Empty tuple
tuple_empty = ()

#tuple with single element
tuple_single = (100)
tuple_single1 = (100,)

print ('tuple1 - ', tuple1)
print ('tuple2 - ', tuple2)
print ('tuple3 - ', tuple3)
print ('tuple4 - ', tuple4)
print ('tuple_empty - ', tuple_empty)
print ('tuple_single - ', tuple_single)
print ('tuple_single1 - ', tuple_single1)

# Example 1

tuple1 = (10,20,30,40,50,60)
for x in tuple1:
  print(x)
# Convert list to tuple
list1 = [10,20,30,40,50,60]
tuple1 = ( tuple(list1))
print(type(tuple1))
print(tuple1)
# List is mutable and tuple is not mutable
list1 = [10,20,30,40,50,60]
list1[2] = 10000
print(list1)  

tuple1 = (10,20,30,40,50,60)
# Will throw error - TypeError: 'tuple' object does not support item assignment
tuple1[2] = 10000<jupyter_output>[10, 20, 10000, 40, 50, 60]<jupyter_text>**Can list be immutable **When a list is created only with tuples then it it becomes immutable<jupyter_code>list2 = [(1,2), (3,4,5),(7,8)]
print('The value at index 2 - ', list2[2])
list2[2] = 100
print('The value after changing the value of idex item 2 - ', list2)
print('The value at index 1 and the tuple index 1',  list2[1][0])
list2[1][0]=30 # This will error with TypeError: 'tuple' object does not support item assignment<jupyter_output>The value at index 2 -  (7, 8)
The value after changing the value of idex item 2 -  [(1, 2), (3, 4, 5), 100]
The value at index 1 and the tuple index 1 3<jupyter_text>Can a tuple be mutable?When you create a tuple of lists, it becomes mutable due to mutable nature of list.<jupyter_code>tuple_1=([10,20],[30,40])
tuple_1[1][1]=50
tuple_1<jupyter_output><empty_output><jupyter_text>Adding Tuples<jupyter_code>tuple5 = tuple1 + tuple2
print(tuple5)<jupyter_output>(10, 20, 30, 40, 50, 60, 1, 2, 3)<jupyter_text>Deleting Tuple<jupyter_code>del(tuple1)
print(tuple1)<jupyter_output><empty_output><jupyter_text>Operations on the tupleThe tuple + and * operations are very similar to strings. It means repetition and concatenation. The result is always a tuple, never a string.1.   len2.   +3.   *24.   5 in (1,2,3)5.   for x in (1,2,3,4,5): print x<jupyter_code># tuple concatenation
x = (4, 4, 2, 5)
y = (2, 4, 0)
print("Concatenation of x and y: ",x + y)

# tuple repetition
a = ("Python Lobby Tutorials ")
print("Repetition of x and y: ",a * 2)

# tuple length
print("length of tuple x is: ", len(x))
print("length of tuple y is: ", len(y))<jupyter_output>Concatenation of x and y:  (4, 4, 2, 5, 2, 4, 0)
Repetition of x and y:  Python Lobby Tutorials Python Lobby Tutorials 
length of tuple x is:  4
length of tuple y is:  3<jupyter_text>Built in Tuple functions1.  max(tuple). -- returns max value in the tuple2.  min(tuple). -- returns min value in the tuple3.  tuple(seq)  -- convets sequence into the tuple4.  cmp(tuple,tuple2) -- compares elements of the tuple1 and tuple25.  len(tuple) -- returns the len of the tuple<jupyter_code>tuple1 = (6,56, 87, 1,3 ,4, 5)
print(tuple1)

print('Max value in the tuple :', max(tuple1))
print('Min value in the tuple :', min(tuple1))
print('Length value in the tuple :', len(tuple1))
tuple1 = (6,56, 87, 1,3 ,4, 5, 'A')

print('Length value in the tuple :', len(tuple1))

#Below two statements will error as str and int cannot be used for min and max
print('Max value in the tuple :', max(tuple1))
print('Min value in the tuple :', min(tuple1))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12876915472071182, 'fraction_numerical': 0.06376668314384577, 'mean_word_length': 4.773181169757489, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '338825', 'license': 'MIT', 'n_tokens_mistral': 1579, 'n_tokens_neox': 1435, 'n_words': 534, 'path': '03_Tuples.ipynb', 'repo_name': 'vijaypattar28/Python_Training', 'size': '4046'}