#How to define a standard tupleArr = (1,2,3,4,5,6,7)Print("arr =%s"%str (arr))#How to define a tuple with only one elementPrint("")Print("How to define a tuple with only one element") Arr= (1,)Print("correct: arr1= (1,):%s"% Type (arr))#correctarr = (1)Print("Error: arr1= (1):%s"% Type (arr))#error, type int#tuple-related operatorsPrint("")Print("tuple-related operators") arr1= (1,2,3,4,5) arr2= (1,2,3,4)Print("arr1 =%s\narr2 =%s"%(str (ARR1), str (ARR2)))Print("accessing the first element of a tuple: arr1[0]:%d"%arr1[0])Print("arr1 > arr2:%s"% (Arr1 >arr2))Print("arr1 < arr2:%s"% (Arr1 <arr2))Print("arr1 = = arr2:%s"% (arr1 = =arr2)) ARR3= arr1 +arr2Print("ARR3 = arr1 + arr2:%s"%str (ARR3))Print("1 in arr1:%s"% (1incharr1))Print("8 not in arr1:%s"% (8 not incharr1))Print("ARR2 * 3:%s"% STR (ARR2 * 3))#common functions of tuplesPrint("")Print("common functions of tuples") Arr3= (1,1,1,3,1)Print("ARR3 =%s"%str (ARR3))Print("number of occurrences using count to view elements: Arr3.count (1)%s"% Arr3.count (1))Print("the first occurrence of using the index Index: Arr3.index (1)%s"% Arr3.index (1))Print("use Len to view the number of tuple elements, Len (ARR3):%s"%Len (ARR3))Print("using Max to view the maximum value of an element in a tuple, Max (ARR3):%s"%Max (ARR3))Print("use min To view the minimum value of elements in a tuple, Min (ARR3):%s"%min (arr3))#How to slice a tuplePrint("")Print("How to slice a tuple") ARR4= (0,1,2,3,4,5,6,7,8,9)Print("ARR4 =%s"%str (ARR4))Print("arr4[:]:%s"%str (arr4[:]))Print("arr4[1:4]:%s"% Str (arr4[1:4]))Print("Arr4[1:-1]:%s"% Str (arr4[1:-1]))Print("Arr4[:5]:%s"% Str (arr4[:5]))Print("arr4[5:]:%s"% str (arr4[5:]))
Python Learning Notes--basic use of meta-groups