List and Tuple

Опубликовано: 02 Май 2022
на канале: Learn Full Stack Development with GeeksforGeeks
318
6

In this lesson we will go through some of the important concepts of Python language where we discuss about Lists and tuple,
Why it is important to learn about list?
Why it is different from dictionaries?
What are tuple?
What is the difference between list and tuple?

List
• Lists are used to store multiple items in a single variable.
• Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.
• Lists are created using square brackets
• List items are ordered, changeable, and allow duplicate values.
• List items are indexed, the first item has index [0], the second item has index [1] etc.
• Since lists are indexed, lists can have items with the same value.
• To determine how many items a list has, use the len() function.
• List items can be of any data type.
Ordered
• When we say that lists are ordered, it means that the items have a defined order, and that order will not change.
• If you add new items to a list, the new items will be placed at the end of the list.
• There are some list methods that will change the order, but in general: the order of the items will not change.
Changeable
• The list is changeable, meaning that we can change, add, and remove items in a list after it has been created.
Tuple
• Tuples are used to store multiple items in a single variable.
• Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage.
• A tuple is a collection which is ordered and unchangeable.
• Tuples are written with round brackets.
Tuple Items
• Tuple items are ordered, unchangeable, and allow duplicate values.
• Tuple items are indexed, the first item has index [0], the second item has index [1] etc.
Ordered
• When we say that tuples are ordered, it means that the items have a defined order, and that order will not change.
Unchangeable
• Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has been created.
Create Tuple With One Item
• To create a tuple with only one item, you have to add a comma after the item, otherwise Python will not recognize it as a tuple.
The tuple() Constructor
• It is also possible to use the tuple() constructor to make a tuple.
"