Saturday 24 November 2018

Difference between Tuple and List in Python-tccicomputercoaching.com

Definition

Tuple and List are used to store data in Python Language.

Tuple: A tuple is a collection of values and it is declared using parentheses. However, we can also use a tuple packing to do the same, and unpacking to assign its values to a sequence.

Lists: Unlike in C++, we don’t have arrays to work with in Python . Here, we have a list instead.


· Mutability

o A List is Mutable
o B Tuple is Immutable

· In built Methods:

o Following Methos can apply to List append(), insert(), remove(), pop(), clear(), sort(), and reverse().
o But not applicable to Tuple.

· Inside Storage

o We can store tuples in a list when we want to. For example,
o [(1, ‘ABC’), (2, ‘DEF’), (3, ‘GHI’)]

o Lists in a Tuple

o Likewise, we can also use a tuple to store lists. Let’s see how.

· Use:

o >>> mytuple=([1,2],[3,4],[5,6])
o Use a tuple when you know what information goes in the container that it is. For example, when you want to store a person’s credentials for your website.
o    >>> person=('ABC','admin','12345')
o But when you want to store similar elements, like in an array in C++, you should use a list.
o    >>> groceries=['bread','butter','cheese']


TCCI-Tririd Computer Coaching Institute is focused on providing Quality
    education with practical sessions. We pride ourselves for providing
    proficient
    
        IT
    
    solutions. We have a highly qualificated and expereinced faculties, who
    provide teaching for all computer subjects and All
    
        Engineering Course
    
    .


Call us @ 98256 18292

Visit us @ http://tccicomputercoaching.com/

2 comments: