Lists And Tuples in Python

Edu Tech Learner
0

 


Lists and tuples are two of the most commonly used data structures in Python. They are both collections of items that can be accessed by index, but they have some important differences. Here are some of the main points you should know about lists and tuples:


  • - Lists are mutable, meaning they can be changed, added to, or removed from after they are created. Tuples are immutable, meaning they cannot be modified once they are created.
  • - Lists are dynamic, meaning they can grow or shrink in size as needed. Tuples are static, meaning they have a fixed size and cannot be resized.
  • - Lists are defined by enclosing a comma-separated sequence of items in square brackets ( [] ). Tuples are defined by enclosing a comma-separated sequence of items in parentheses ( () ).
  • - Lists can contain any type of objects, including other lists. Tuples can also contain any type of objects, including other tuples.
  • - Lists are generally used to store a collection of different items that may change over time. Tuples are often used to store a collection of related items that belong together and will not change.


For example, you can use a list to store the names of your favorite fruits:


```python

fruits = ["apple", "banana", "cherry"]

```


You can use a tuple to store the coordinates of a point on a map:


```python

point = (3, 5)

```


Post a Comment

0Comments
Post a Comment (0)