values and data types
values and data types
VALUE:
• Indexing
• Slicing
• Concatenation
• Repetitions
• Member ship
2. Lists
List is an ordered sequence of items. Values in the list are called elements / items.
It can be written as a list of comma-separated items (values) between square
brackets[ ].
Items in the lists can be of different data types.
Operations on list:
• Indexing
• Slicing
• Concatenation
• Repetitions
• Updation, Insertion, Deletion
3. Tuple:
A tuple is same as list, except that the set of elements is enclosed in
parentheses instead of square brackets.
A tuple is an immutable list. i.e. once a tuple has been created, you
can't add elements to a tuple or remove elements from the tuple.
Benefit of Tuple:
Tuples are faster than lists.
If the user wants to protect the data from accidental changes, tuple can be used.
Tuples can be used as keys in dictionaries, while lists can't.
Basic Operations:
Altering the tuple data type leads to error. Following error occurs when user tries to
do.
>>> t[0]="a"
Trace back (most recent call last):
File "<stdin>", line 1, in <module>
Type Error: 'tuple' object does not support item assignment
Mapping
• This data type is unordered and mutable.
• Dictionaries fall under Mappings.
Dictionaries:
Lists are ordered sets of objects, whereas dictionaries are unordered sets.
Dictionary is created by using curly brackets. i,e. {}
Dictionaries are accessed via keys and not via their position.
A dictionary is an associative array (also known as hashes). Any key of the
dictionary is associated (or mapped) to a value.
The values of a dictionary can be any Python data type. So dictionaries are
unordered key-value-pairs (The association of a key and a value is called a
key-value pair )
Dictionaries don't support the sequence operation of the sequence data types
like strings, tuples and lists.
If you try to access a key which doesn't exist, you will get an error message:
>>> words = {"house" : "Haus", "cat":"Katze"}
>>> words["car"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'car'
List Tuple Set Dictionary
Lists can be nested among Tuple can make use of nested Sets can be nested among All dictionaries can use nested
themselves. among all. themselves. among themselves.