Skip to main content

Posts

Dictionary In Python

Dictionaries are similar to other compound types except that they can use any immutable type as an index. We can refer to a dictionary as a mapping between a set of indices (which are called keys) and a set of values. Each key maps a value. The association of a key and a value is  called a key-value pair . A dictionary is an extremely useful data storage construct for storing and retrieving all key value pairs, where each element is accessed (or indexed) by a unique key. However, dictionary keys are not in sequences.             Dictionaries are mutable.             Dictionaries are unordered.               Items in 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 dictio...

Tuple in Python

Tuples are used for grouping data.A tuple is an immutable list, i.e. a tuple cannot be changed in any way once it has been created. A tuple is defined analogously to lists, except that the set of elements is enclosed in parentheses instead of square brackets. The rules for indices are the same as for lists. Once a tuple has been created, you can't add elements to a tuple or remove elements from a tuple. Where is the benefit of tuples? Tuples are faster than lists. If you know that some data doesn't have to be changed, you should use tuples instead of lists, because this protects your data against accidental changes. Tuples can be used as keys in dictionaries, while lists can't. The following example shows how to define a tuple and how to access a tuple.  Furthermore we can see that we raise an error, if we try to assign a new value to an element of a tuple: Tuple creation >>> t = ("tuples", "are", "immutable") ...

List in Python

A list is an ordered set of values, where each value is identified by an index. The values that make up a list are called its elements. Lists are similar to strings, which are ordered sets of characters, except that the elements of a list can have any type. Lists are the most versatile sequence type. The elements of a list can be any object, and lists are mutable - they can be changed. Elements can be reassigned or removed, and new elements can be inserted. List is one of the simplest and most important data structures in Python. Lists are enclosed in square brackets [ ] and each item is separated by a comma. Lists are collections of items where each item in the list has an assigned index value. A list is mutable, meaning you can change its contents. Lists are very flexible and have many built-in control functions Creating Lists To create a list, place a (possibly empty) comma-separated list of objects or expressions in square brackets. For example, the following initialize...