StudentShare
Contact Us
Sign In / Sign Up for FREE
Search
Go to advanced search...
Free

Python Knowledge and Understanding - Example

Summary
Download full paper File format: .doc, available for editing
GRAB THE BEST PAPER91.8% of users find it useful

Extract of sample "Python Knowledge and Understanding"

Python Knowledge and Understanding Name Course Institution Difference between Lists, Tuples and Dictionaries Lists, tuples and dictionaries are data structures in the python language that can be used to store numerous elements which may be accessed and modified in some cases (Lutz, 2008). They are also defined as sequence type or data collections due to the nature of ordered or unordered sequences in them and the nature in which they contain data elements. Lists A List is an ordered sequence of values that are numbered from zero maximum number of values minus one i.e. it is indexed from 0 to (n-1) where n is the number of values contained. Lists are preferred to store homogenous values which are going to be modified, added or deleted later. A list is instantiated using square brackets and assigned to a variable e.g. list_x = []. Values within the list are comma separated and can be of different data types e.g. list_x = [“hello”, 81, 0.08, [0, 1]]. The values in the examples are of data type String, Integer, Float and List respectively. Values in a list are accessed by iteration over the list. To obtain a specific value in the list, the index of the value is specified on the variable e.gl list_x[2] which gives 81 as the result. A list is mutable meaning that value contained in it can be modified or replaced, removed or even added into the list (Deitel, 2002). Mutability also implies that the list can be sorted i.e. order is rearranged or the order can be reversed. The mutability of lists allows it to have method that can be used to modify it by appending values, ordering or sorting value and removing values at given indices. Tuples A tuples is data structure almost similar to a list but has a few differences. It is also an ordered sequence of values that are numbered from zero maximum number of values minus one i.e. it is indexed from 0 to (n-1) where n is the number of values contained. Tuples are preferred for storage of heterogeneous values which are not going to be modified later after instantiation. A difference between a tuple and a list is the manner of instantiation. A tuple is instantiated by using parenthesis and is assigned to a variable e.g. tuple_x = () instantiates an empty tuple and assigns it to variable tuple_x. Tuples, like lists can be used to store several values of different data types e.g. tuple_x = (“First”, 0.1, (4, 3)) where the data type of values stored in tuple_x in the example provided is a String, Float and Tuple respectively. Values in a tuple are accessed by unpacking the tuple. A tuple, unlike list, is immutable meaning it cannot be modified in any way once instantiated. Values cannot be added, removed or altered once instantiated in a tuple. The immutability of tuples does not allow for any methods that can be used to modify it thus tuples don’t have any methods associated with them (Downey, 2012). Dictionaries A dictionary is a data structure used as a container for an unordered sequence of values (Deitel, 2002). A dictionary is indexed by keys, unlike a list and a tuple which is indexed by a range of number starting from zero. A dictionary stores data in a key-value pair where the key can be of different immutable data types such as String, Integer, Tuple (with no mutable object). Lists cannot be used as keys in a dictionary since they are mutable. A dictionary is instantiated using pair of braces e.g. dict_x = {} which creates an empty dictionary and assigns it to variable dict_x. A dictionary can contain values of different data type values orders by an immutable data type keys e.g. dict_x = {“first”: 1, “second”: 0.5}. From the example provided, dict_x contains two key-value pairs which are ordered by an String data type keys (“first”, “second”) which index an Integer and Float value (1, 0.5). Values in a dictionary can be access via iteration. To access a specific value, the key is used identify the index e.g. dict_x[“first”] returns value 1. A dictionary like a list is mutable i.e. the key-value pairs contained in it added, removed or changed. This allows for methods to manipulate the dictionary pairs as well as access and modify the keys and values of the dictionary’s pairs (Python.org, n.d). Variables management in python In most programming languages, a variable is a reference to a memory location that is used by a computer program. The memory location the variable refers to nay contain different values such as numbers, characters or more complex data type values. As the name implies a variable can change if an action is applied to it such as a new value is assigned to it (Scott, 2014). In python, variables are labels put on objects; they are not pointers or references to the memory. They can be viewed as tags put on values contained in a memory location. This means that a value like 100 of a type integer may be have a label ‘a’ if the following is done: a = 1. If a different variable ‘b’ is instantiated to have the same value 100 using b = 100, a new label is created and assigned to the value 100 in the memory location. Therefore, there will be only one memory location containing value 100 but with two different tags. Python has its own internal garbage collection that removes any object from memory that does not have a label (Fangohr, 2014). It uses reference counting to track how many labels are currently pointing to a particular object in a memory location. When that count of the references to the object reaches zero, the memory location is marked for garbage collection where it may be removed from memory. Python is a loosely typed language. Due to the fact that variables are labels of objects in memory, they themselves are no strict type in their definition. However the object in memory has a type which means certain operations cannot be performed on it. Python has some operation s that can be used for type conversion such as str() to convert values to string (Fangohr, 2014). References Deitel, HM 2002 Python - How to Program: Introducing XML Control Structures, Functions, Lists and Tuples, Dictionaries. Upper Saddle River, NJ: Prentice Hall. Downey, A 2012. Think Python. Sebastopol, CA: O'Reilly. Fangohr H 2014, Introduction to Python for Computational Science and Engineering [online] Available at: http://www.southampton.ac.uk/~fangohr/training/python/pdfs/Python-for-Computational-Science-and-Engineering.pdf [April 6, 2015]. Lutz, M 2008, Learning Python. 3rd ed. Sebastopol, CA: O'Reilly, Python.org, n.d, "5. Data Structures." 5. Data Structures — Python 2.7.10rc0 Documentation Available at: https://docs.python.org/2/tutorial/datastructures.html#dictionaries [April 6, 2015]. Scott, MS 2014, An introduction to Python for scientific computing [online] Available at: http://www.engr.ucsb.edu/~shell/che210d/python.pdf [April 6, 2015]. Read More
sponsored ads
We use cookies to create the best experience for you. Keep on browsing if you are OK with that, or find out how to manage cookies.
Contact Us