tuple type in python

tuple is one of python sequence types. and tuple is immutable sequence type.
"immutable" means that the data of tuple cannot be modified.

at first, it would be good to see the declaration of tuple constructor.

tuple([iterable])
and then, we can make tuple with some ways like below
  • Using empty parentheses: ()
  • Separating items with commas: a, b, c or (a, b, c)
  • Using the tuple() constructor or tuple(iterable)


if you want to make empty tuple, do it like below

t = ()
t = tuple()

if you have some items to make tuple, do it like below


t = (1,2,3,4)
t = tuple(1,2,3,4)

if you have any other sequence type data or customized iterable data to make tuple, do it like below

sourceTuple = (1,2,3)
sourceList = [1,2,3]
t = tuple(sourceTuple)
t = tuple(sourceList)

it is very simple and easy to make tuple.
let's try to make example code and see the result.


t1 = ()
t2 = (1,2,3,4)
t3 = tuple(t2)

print(t1)
print(t2)
print(t3)










Comments

Popular posts from this blog

making menubar and menu on QML

let's start QML programming with PySide2

QSizePolicy Fixed example in pyside2