Red Security

Full Version: Python 2.2: Python Tuples
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
[Image: BFMMlbcQvml9HSqXcvNp]
Tuples:
         In this thread we are going to explain tuples. The tuple is similar to lists, just a bit difference that tuple is immutable means that once you created a tuple you can’t change as we can insert, delete and append our list we can not perform such operations on tuples. Tuples are ordered and unchangeable. We use parentheses for elements of tuples but these are not compulsory. Let's check the code for better understanding.

Code:
num = [1,231,423,5,3,44,12,3,235,3,4,23,4,2,31]

# See here we changed the value of number on index 1

num[1] = 10

print(num)

# Now let's check if we can do this with tuple or not
# This part will  generate error

num = (1,2,3,4,5,6,7,8,9)

num[1] = 10


We can perform following operations on tuples. Suppose if we want to see how many time a number or a character is repeated so we can check it in tuple

Code:
name = ('a','a','a','b','c')

print(name.count('a'))


And the second function which we can use with tuples is len() it is used to check the length of tuples. Almost every function you perform on list is applicable on tuple except those of changing functions like append(), insert(), pop() etc

But if we really want to change the value of the element inside a tuple. For this first convert your tuple into list then update, delete and whatever you want, and after that change it again into tuple let’s see a code example

Code:
name = ('a','a','a','b','c')
x = list(name)
x[1] = 'z'
x.append('m')
name = tuple(x)
print(name)

If a tuple has only one value use a comma after that value otherwise Python will not recognise it as a tuple.

Unpacking Tuple: when we are creating and assigning values to tuples actually we are packing a tuple so what is this unpacking. Unpacking is assigning element of a tuple to variable this is called unpacking of tuple let’s code it for better understanding of this concept:

Code:
name = ("BMW","Twitter","Apple")

(car,sm,fruit) = name

print(car)

So in this code ‘car’ is assigned “BMW”, the variable ‘sm’ is assigned “Twitter”  and so on. But what if you have less number of variables than number of elements inside the tuple. Here we make a variable with ‘*’ sign and the remaining value are assigned to it in form of list
let’s code it:

Code:
name = ("BMW","Twitter","Apple","Strawberry","Mango")

(car,sm,*fruit) = name

print(fruit)


In this code first two value are assigned to the first two variable and the remaining three values are assigned to the variable with ‘*’ sign.

As each value in tuple has a unique index as in lists we can use loops to iterate and traverse the tuple. Let’s write a code for it

Code:
name = ("BMW","Twitter","Apple","Strawberry","Mango")

for i in range(len(name)):
    print(name[i])

That’s it for today's thread. If you have any suggestions or any question about this threads on Python you can pm me and you can also ask in reply section. Thanks for reading my threads.


"Keep Good care of your Health
 Allah Hafiz and Good Bye Till the next post"
"Keep Learning and Keep exploring"
I'm not a python guy, This was totally new for me..

Thanks
It is not available by default in Java

https://www.javatpoint.com/java-tuple#:~...nt%20types.
(04-30-2022, 12:23 AM)Mr.Kurd Wrote: [ -> ]I'm not a python guy, This was totally new for me..

Thanks

Your welcome Kurd
(04-30-2022, 12:25 AM)Mr.Kurd Wrote: [ -> ]It is not available by default in Java

https://www.javatpoint.com/java-tuple#:~...nt%20types.

Thank you for sharing this information.