Data type models in python:
- The data type is nothing but, it tells about kinds of data.
- The data type models are used to store collection of data under a common name.
Type’s data type model:
- List
- Tuple
- Set
- Dictionary
1. List:
- The list data type is used to store the collection of an item in an ordered sequence.
- These items are mutable, which means changeable as possible.
- It is denoted by square bracket [].
- Its index value is started with 0 and ends with list_length-1.
Syntax:
Example program:
Output:
Print list in looping statement:
Output:
Insert or append an item into the list:
- To insert an element in the list is used append() function.
Output:
2. Tuple:
- The tuple data type is used to store collection of item in unordered and it allows duplicate values same as a list.
- These items are immutable, that means cannot be changed tuple.
- It is denoted by parentheses () but if calling the index value means using square brackets [].
- Its index value is started with 0 and end with list_length-1.
Syntax:
Example program for print tuple elements:
Output:
3. Set:
- The set data type is used to store unique values and displayed by ordered sequence manner.
- These items are mutable, that means can change the sets.
- It is denoted by curly braces {} but if calling the index with value means not possible.
Syntax:
Example program for print the set of elements:
Output:
Insert an item into the set:
- To insert an element in the set is used add() function.
Output:
Insert number of items at a time into
the set:
- To insert number of element in set is used update() function.
Output:
4. Dictionary:
- The dictionary data type is similar to the sets but a little bit different and the dictionary are also mutable.
- It contains pair of parameters. i.e, keys and values.
- Key is used to store element names.
- Value is used to store element values.
Syntax:
Example program for print the dictionay of elements:
Output:
Print element name in for loop using keys()
function:
Output:
Print element values in for loop using
values() function:
Output:
Print
Output:
No comments:
Post a Comment