In this mastery I will talk about tuples.A tuple is a sequence of immutable Python objects, is like lists, but they have on especially diference, that ones yu create a tuple you can not modify but in lits you can. Also the sintax of tuples are parentheses and lists use square brackets.for example:
tuple1=(“efrain”,”duarte”,”lopez”,20)
you can write strings,numbers is just like lists.
be careful when you just write one thing inside a tuple you have to write it like this:
tuple2=(“ice”,) ——> such is just one thing you have to add a comma to the end
To acces values in tuples you have to use square brackets.for example:
tuple1=(“efrain”,”duarte”,”lopez”,20)
print tuple[0]——–>the result will be “efrain”
print tuple[0:2]—–>the result will be [“efrain”,”duarte”,”lopez”]
this is counting beging form 0.
You cant modify a tuple but you can create a new tuple with other tuples like this:
tuple3=tuple1+tuple2
tuple3 will be>>>(efrain”,”duarte”,”lopez”,20,”ice”)
Also the tuples respond to the + and * operators much like strings, and this operations will create a new tuple not just strings .For example the basic tuples operations:
tuple4=(1,2,3,4)
print lent(tuple4)——>resutl= 4
also we can use Concatenation,Repetition,Iteration.
an important point is that we can modify a list inside a tuple, but the tuple we can n ever touch it.
for more information here is a link its very helpful:
mastery24 Creation and use of tuples in Python by Efrain Duarte is licensed under a Creative Commons Attribution 4.0 International License.