Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python Tuple Exercises



Python Tuple Exercise 1

Python program to find unique numbers in a given tuple −

T1 = (1, 9, 1, 6, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 2)T2 = ()for x in T1:   if x not in T2:      T2+=(x,)print ("original tuple:", T1)print ("Unique numbers:", T2)

It will produce the followingoutput

original tuple: (1, 9, 1, 6, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 2)Unique numbers: (1, 9, 6, 3, 4, 5, 2, 7, 8)

Python Tuple Exercise 2

Python program to find sum of all numbers in a tuple −

T1 = (1, 9, 1, 6, 3, 4)ttl = 0for x in T1:   ttl+=x   print ("Sum of all numbers Using loop:", ttl)ttl = sum(T1)print ("Sum of all numbers sum() function:", ttl)

It will produce the followingoutput

Sum of all numbers Using loop: 24Sum of all numbers sum() function: 24

Python Tuple Exercise 3

Python program to create a tuple of 5 random integers −

import randomt1 = ()for i in range(5):   x = random.randint(0, 100)   t1+=(x,)print (t1)

It will produce the followingoutput

(64, 21, 68, 6, 12)
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp