Wednesday, June 19, 2019

Tuple in python


Tuple is similar to list but
list is indicate in '[]'square brackets and tuples indicates in "()" parenthesis(optional)
another difference is list in mutable
tuple is immutable

empty tuple
 t=()


example 1:
list=[10,20,30,40]
tuple=(10,20,30,40)
print(list)
print(tuple)


output:
[10, 20, 30, 40]
(10, 20, 30, 40)

example 2:

list=[10,20,30,40]
tuple=(10,20,30,40)
tuple2=10,20,30,40

print(list)
print(tuple)
print(type(tuple2))
print(tuple2)

output:
[10, 20, 30, 40]
(10, 20, 30, 40)
<class 'tuple'>
(10, 20, 30, 40)


example 3:

(x,y) = (5, 3)
coordinates = (x,y)
print(coordinates)

dimensions = (8, 5.0, 3.14)
print(dimensions)
print(dimensions[0])
print(dimensions[1])
print(dimensions[2])

output::
(5, 3)
(8, 5.0, 3.14)
8
5.0
3.14



No comments:

Post a Comment

python class topic video