Control Flow: The if
, elif
, else
Statements
len() Function for List, Tuple, Set, and Dictionary
list_example = [1, 2, 3, 4, 55, 6, 6]
tuple_example = (1, 2, 3, 4, 55, 6, 6)
set_example = {1, 2, 3, 4, 55, 6, 6}
dictionary_example = {1: "One", 2: "Two", 3: "Three", 4: "Four", 5: "Five"}
print(len(list_example), "-> ", list_example)
print(len(tuple_example), "-> ", tuple_example)
print(len(set_example), "-> ", set_example)
print(len(dictionary_example), "-> ", dictionary_example)
Output:
7 : [1, 2, 3, 4, 55, 6, 6]
7 : (1, 2, 3, 4, 55, 6, 6)
6 : {1, 2, 3, 4, 6, 55} : order of elements is not preserved and duplicates are not allowed in sets.
Lists are defined by a pair of []
,
Tuples are defined by a pair of ()
,
Sets are defined by a pair of {}
,
Dictionaries are also defined by a pair of {}
,
keys
in the dictionaryThe positional order does not imply the incremental or decremental ordering based on magnitude of numbers or the lexicographic ordering of strings in a list or a tuple. If such an ordering is necessary, the programmers will need to sort the items in the list or the tuple.