This video tutorial is aimed at demonstrating how to manage different types of sequences. The version of Python used here is Python 3.1. The video shows how to work with strings, lists, tuples and dictionaries on Python. Sequences are nothing but items that you can iterate and also index the same with some suitable method. For example, index subscript 0 would be the first element in the sequence the next would be subscript 1 and so on.
The first thing you will be learning from the video is string. Consider the msg=’hello world’ This is a string. The quotes here can either be double or single. So if you had to extract the word hello from the above example, you would have to say msg(0:5) and when you hit enter, you will see that the program will return ‘hello’. You can also loop through these characters. For example,
for c in msg:
print (c)
This will print the string. You need to remember that strings are not mutable objects. So if you are trying to manipulate a string, you will actually be creating a new string.
Lists
The video covers both lists as well as Tuples. The difference between each is that Lists are mutable whereas Tuples are not mutable. The way you create a list is to use square brackets to define the objects in a list for example,
stooges=[ ‘Larry’, ‘Curly’, ‘Moe’ ] So this is how you define a list. Once you have done this, if you simply type out ‘stooges’ it will print out the entire list. Like with strings, you can also extract individual objects from a list. If you say, stooges[1] the output should be ‘Curly’. Similaryly, if you enter stooges[0:2] it will give the first two object from the list viz. [‘Larry’, ‘Curly’]. You can also loop through the lists like with a string. All you need to do is,
for s in stooges:
print (s)
This will print the entire list. You can also append lists unlike strings. So, you can say stooges.append (‘Schemp’). Once you have done this, you can simply enter stooges and the list would include an additional object known as ‘Schemp’. If you want to learn what data types are available for use with a function, all you need to do is type or dir(function) and you will get the entire list of the data types that can be applied to the function. The pop function can be used to remove the last element from the list.
Tuple
A tuple is very similar to a list but it differs with one main characteristic that is a tuple is not mutable like the list. This means, you cannot manipulate the elements or objects that are defined withing the tuple. However, there are still some data types that you can apply to tuples. You can convert a list to tuple and vice versa. For example, the list stooges that you have created here can be converted to a tuple by simply entering the following,
stooges=(‘Larry’, ‘Curly’, ‘Moe’) This is possible because the virtual machine has an automatic garbage collector that can removes the lists completely and changes it to a tuple. So now that you have created the tuple, if you try to modify it by say using append, you will get an error as the tuple is not mutable. So you may wonder why is a tuple necessary, this is because, sometimes you may need a function and you want to ensure that what you are passing in to the function such as a tuple does not get manipulated in any manner.
Similarly, you can also convert a tuple into a list at any time. All you need to do is define it again and the garbage collector would change it completely. You can also attribute the objects of a tuple into another list. For example, if you want to create a list called friends with friends with the elements of stooges, all you need to do is write friends=list(stooges). The opposite is also possible. All you need to do is replace list with tuple.
Dictionaries
A dictionary is denoted by flower braces and you need to separate the keys and values in a dictionary with a colon. So you can say that a dictionary is nothing but an unordered set of key data and value pairs. So for example, say you want to create a dictionary that includes country codes. All you need to do is write codes= {‘De’ : ‘Germany’, ‘CN’ : ‘China’, ‘US’ : ‘USA’} Here you will have key and value pairs that are separate by a colon. The order of these pairs in a dictionary do not matter. What that means is if you iterate through the dictionary, it might not happen in an order that you want it to be.
You can loop through a dictionary much like with the lists and also like tuples. All you need to do is type out,
for key in codes.keys():
print (key, “=”, codes[key])
The ‘key’ here is an arbitrary variable and it could be anything. This should give out the corresponding values of the keys as well as the values in the output. If the list is not presented in a manner you like, you can simply use sorted to get the entire dictionary sorted. So another way to loop through the dictionary would be to use the following,
for key in sorted(codes):
print (key, “=”, codes[key])
This will give you a sorted output in the ASCII. This wraps up the basics of these data structures.
Please Share This Knowledge With Others!