Powered by Ajaxy
Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Python: Sorting a tuple

T = ('cc', 'aa', 'dd', 'bb')
tmpListe = list(T)
tmpListe.sort()
print tmpListe
T=tuple(tmpListe)
print T

Python: Concatenating a tuple with a list items

list=[1, 2, 3]
T1= (4, 5, 6)
T2=(T1 + tuple(list))
print T2
for elem in T2:
     print elem

Python: Copy a tuple

x = (1, 2, 3)
y = x
print x
print y

Python: using range() method to create a tuple

a, b, c = range(3)
print a, c
T1=range(3)
print T1

Python: Using zip to Group Sequence Items

# Define 3 tuples
T1, T2, T3 = (1,2,3), (4,5,6), (7,8,9)
# Grouping the 3 tuples
t4= zip(T1,T2,T3)
print t4
print t4[1][2] # results: 8

Python: How to Index a single-item tuple

print  ('x','y', 'z')[1]

Python: function to return a tuple.

def tupleFonc (x):
     return x**2, x**3, x**4, x**5
print tupleFonc(6)

Python: Tuple assigment

[x, y, z] = (10, 20, 30)
print x, z                 #prints: 10, 30
(a, b, c) = "ABC"
print b, c                 #prints: B, C

Python: Get the minimum and maximum values ??of tuples

T1 = ("z","b", "c", "d")
T2 = (8,2, 3, 4)
print T1, " Min T1= ",min(T1)
print T2, " Min T2= ",min(T2)
print T1, " Max T1= ",max(T1)
print T2, " Max

Python: Sorting a tuple

T = ('cc', 'aa', 'dd', 'bb')
tmpListe = list(T)
tmpListe.sort()
print tmpListe
T=tuple(tmpListe)
print T

Python: Define a tuple with only one item

celib = 'python',
print len(celib)
print celib

Python: Comparing tuples

T1 = (-1,-2, -3, -4)
T2 = (1,2, 3, 4)
print T1 == T2     #False
print T1 >= T2     #False
print T1 <= T2     #True
print T1 != T2     #True
[/pytho

Python: How to Concatinate two tuples

lttr= ("a", "b", "c", "d")
tple = ("january","february", "march")
print tple
lttr+= tple
print type(lttr)
print lttr

Python: Check if an element exists in the tuple

T = ("a","b", "c", "d")
if "b" in T:  # check if "b" exits in the tuple T
    print "Yes."

Python: Exchange value of tuples

x = (1, 2, 3)
y= ('a', 'b', 'c')
print 'X= ',x
print 'Y= ', y
x, y = y, x
print 'X= ',x
print 'Y= ', y