Discord Server Red Security Twitter Donation to Red Security Red Security Youtube Channel Red Security Tumblr Profile
Login or Register to Hide ads and Accessing all features on the forum

Tutorial 

Python 2.2: Python Tuples

4 Replies, 1314 Views

[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"
Love me like you do Heart
I'm not a python guy, This was totally new for me..

Thanks
Rs
* Thankful to Allah *
Kurdy
It is not available by default in Java

https://www.javatpoint.com/java-tuple#:~...nt%20types.
Rs
* Thankful to Allah *
Kurdy
(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
Love me like you do Heart
(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.
Love me like you do Heart

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tutorial Doubly Linked List in Python Covid-19 0 2,227 07-30-2022, 09:15 PM
Last Post: Covid-19
  Tutorial Linked List in Python Covid-19 2 2,607 07-30-2022, 08:25 PM
Last Post: Covid-19
  Tutorial File Handling in Python Covid-19 2 2,645 06-16-2022, 06:17 PM
Last Post: Covid-19
  Tutorial Exceptions in Python Covid-19 0 1,825 06-08-2022, 09:19 PM
Last Post: Covid-19
  Free Automate the boring stuff with Python Covid-19 0 1,591 06-07-2022, 06:58 PM
Last Post: Covid-19
  Tutorial Turtle in Python Covid-19 0 1,823 06-02-2022, 10:14 PM
Last Post: Covid-19
  Tutorial Python 3.5: Class Methods Covid-19 0 1,772 05-29-2022, 01:07 PM
Last Post: Covid-19
  Tutorial Python 3.2: Inheritance in Python Covid-19 2 2,802 05-27-2022, 06:47 PM
Last Post: Covid-19
  Tutorial Python 3.4: Encapsulation and Abstraction Covid-19 0 1,804 05-27-2022, 06:45 PM
Last Post: Covid-19
  Tutorial Python 3.3: Operator Overloading Covid-19 2 2,799 05-26-2022, 08:50 PM
Last Post: Covid-19



Users browsing this thread: 1 Guest(s)