Difference between a list and tuple in python programming

Hello everyone

I just started learning python and trying to understand the different data types in Python programming language.
I would like to know the difference between a list and a tuple and the cases in which i can use them.

Thanks in advance…

Tuple are actually lists that cannot be edited , its immutable while list can be edited it works like an array you can delete elements from the list as well as add elements to it but u cant do that in tuple tuples have a fixed size

1 Like

Hi aaronkili45,

Tuples
Tuples are pretty easy to make. You give your tuple a name, then after that the list of values it will carry. For example, the months of the year:

months = (‘January’,‘February’,‘March’,‘April’,‘May’,‘June’,‘July’,‘August’,‘September’,‘October’,‘November’,’ December’)

Python then organizes those values in a handy, numbered index - starting from zero, in the order that you entered them in.

Lists
Lists are extremely similar to tuples. Lists are modifiable (or ‘mutable’, as a programmer may say), so their values can be changed. Most of the time we use lists, not tuples, because we want to easily change the values of things if we need to.Lists are defined very similarly to tuples.
For example, you have FIVE cats, called Tom, Snappy, Kitty, Jessie and Chester. To put them in a list, you would do this:

cats = [‘Tom’, ‘Snappy’, ‘Kitty’, ‘Jessie’, ‘Chester’]

As you see, the code is exactly the same as a tuple, EXCEPT that all the values are put between square brackets, not parentheses. Again, you don’t have to have spaces after the comma.
You recall values from lists exactly the same as you do with tuples. For example, to print the name of your 3rd cat you would do this:
Recalling items from a list

print cats[2]

You can also recall a range of examples, like above, for example - cats[0:2] would recall your 1st and 2nd cats.

To add a value to a list, you use the ‘append()’ function. Let’s say you got a new cat called Catherine. To add her to the list you’d do this:

cats.append(‘Catherine’)

1 Like

@Shayan_Muhammad @Hackerzone Thanks guyz for the clear explanations.