Chapter summary - Unit 2
Higher Data Constructs: Lists and tuples: Basic skills for working with lists, list of lists, more skills for working with lists,
tuples;
Dictionaries: get started with dictionaries, more skills for working with dictionaries;
Strings: Basic skills for working with strings, split and join strings;
Dates and times: get started with dates and times
Text Book 1 – Chapters 6,12,10,11
Datatypes - complex
- Lists: A list is a mutable datatype in which all the elements are indexed from 0 to n
| |
- Tuples: A tuple is an immutable datatype in which all the elements are indexed from 0 to n
| |
- Dictionaries: A dictionary is a complex mutable datatype in which the elements are indexed upon the user’s needs, this is called the key value pairs.
| |
- Sets: A set is a datatype of unordered, unindiexed and non-duplicated elements.
| |
Lists in python
Lists are a datatype that can hold multiple values of different datatypes and is mutable i.e. values can be changed without fundamentally changing the variable.
- Initialising a list
- Accessing and printing the list
- List methods
Note: In the below example if return is specified means the orignal list is not modified and just returns a value in the variable. If return is not specified means that the list will be modified. If the function modifies the orignal list then the function/method will not be applicable for tuples.
| |
Functions that require the random module
- List spiclng
The lists in python can be divided into subparts, this is known as splicing.
| |
Here start gives the value of which index to start from, stop gives the final index and step gives the increment count.
Shallow and deep copy
There are a couple of difference in how the interpretor accesses a list compared to an immputable object. Whenever an immutable object is referred by another variable it copies the value of the object at that very instance, so if the variable’s data is changed the variable’s data gets destroyed and a new object is created. So in an nutshell if it’s an immutable object then the value remains same even if the host variable’s data is changed.
In the case of a mutable Object is referred then the interpreter makes a shallow copy of the object what this means is when the host object data is changed the others variables data also changes. The other variable doesn’t take the exact snapshot of the variable when the object was first referred to the variable references the variable and if the host variable changes the second variable also changes.
To remove this anomaly python introduces a new module called as copy. So as we have seen in the previous instances the python interpreter makes a shallow copy of the object. With the use of the copy Module we can make a deep copy of the object that is given above . With the deep copy method if the main object is changed the secondary object takes an exact snapshot of the main object and even after the main object changes the secondary object never changes.
Tuples
Tuples are the mutable conterpart of lists. This is an ordered, indexed and immutable datatype. Almost all the list methods are compatable except those which change lists value.
Refer the list functions above where the orignal list is not modified. If the functions/methods don’t modify the orignal list then the function is also applicable for tuples.
Sets
Sets are ordered, unindexed, immutable datatype with no duplicates.
Dictionary
Dictionaries are ordered indexed and mutable datatype with custiom key-value pairs.
Using a for loop to print out keys and values of the text
Note: There are a couple of points to remember when working with dictionaries. The value of an item can be anything, however the key of an item is only limited to immutable datatypes and no duplicates.
Dictionary methods
| |
Strings
A string is a combination of more than one charecters.
- capitalize()
- count()
- find()
- isalnum()
- isdigit()…
Creation and modification of datetime
- Showing values
- Creating objects