 So the next major topic is functions. So python allows you to create functions and it is extremely easy to do. So let us get started. Why do you need functions? First thing, why do not we just write code like that? Why do you need functions at all? First major reason is it allows you to reduce duplication of code. So for example let us say you have to convert some quantity into say a number. Take for example the integer. When I did int of 29, someone has implemented this functionality somewhere, int of 29 will return an integer. Somebody has written that functionality. Imagine that every time I encountered the string I have to sit and write one 10 lines of code to convert this silly string to an integer. So what have they done for me? By providing this one function int, in this case int is not a function actually, but supposing they did provide this function content, it would convert that function nicely. It will convert that quantity nicely in one line of code rather than 10 lines of code. So it essentially allows you to remove any duplication. So if somebody else wants to do the conversion, they just call this function with the corresponding argument. A lot of things that we have done so far, we have used convenience methods like join, split, all of that functionality is essentially abstracted out into functions. So therefore it reduces duplication of code. You need fewer lines of code and therefore it also reduces bugs. So let us imagine you have a big project and you took the naive approach of copying all this. You write this function once, you write those 10 lines and you copy the 10 lines to 20 different functions. You find a mistake. Now what do you do? You have to go to every one of those 20 files and change every one of them. Instead if I wrote one function, I fix the error in that one function, everybody else also gets the fix. So it reduces duplication, fewer lines of code and hence less scope of bugs. It allows other people to reuse your function. And the nice thing is if I write, so we did not care about what the integer fellow wrote. When I said int of 29, I did not care what he did. It works. So I do not care about those details. So I can use other people's function without knowing the nitty-gritty of their implementation. So which means it helps me manage my programming tasks become much easier. So we need to create functions. Let us see what we can do. So let us say we want to create the function f of x such that it squares the argument of it, the argument provided and returns the result. So that is I want to return x squared. So write this in Python, you simply do the following. Def, which is a keyword, fx and then everything that is indented one level is the content of this function. So if I say, by the way, if you want to give comments, use the hash key, it will give you comments in Python. Now if I do f of 1, it printed a and it returned 1. So I could say for example, x is f of 1, x is, okay. So long as I am able to do multiplication between the two elements x, I can call the function f, which is an integer of float. So for example, let me create a complex number. Def of, is this clear? So basically I can create a function with just these three lines. Def, f is the function name, x is an argument that is passed to the function followed by a colon which defines the block of the function. To return a value, you simply use return. But if you do not want to return, for example, if I want to say def f of x, I do not want to do anything, I can simply say pass and then f of 1 will do nothing. Or I can say def f of x, print x, it will simply print the value. You do not have to return anything, but if you want to return something, you may do so. So recapping, def is a keyword, f is the name of the function, x is the parameter of the function and return is also a keyword that specifies what should be returned. So it is as simple as that. So here is another function as I had shown you, greet, print hello world. So greet is also a function and notice that greet does not take any argument. So you do not necessarily have to take arguments. However, if I called f now with an argument, what do you think I should get? f does not take any arguments. But I did not, I am saying f of 1. It will give me a nice error message. It will say f takes no arguments, you have given me 1. Similarly, if I define def f of x and now I called f with no arguments, it will tell me f takes 1 argument, you have only given me nothing, 0 given. So the error message once again is extremely helpful. Now supposing I have a function, in this case f printed 1, but did f return anything? Let us find out. So x is equal to f of 1. What was f? x is equal to f of 1. Notice that x is something called none. None is a special quantity in Python which is somewhat similar to null in C or C++. It basically says it is nothing and it is commonly used. There are many situations where none is a useful quantity that we have. So the question is if not using ipython, we should after def statement in the next line plus 1 tab key. Well, you can use tab but I would suggest you use spaces, use 4 spaces ideally then the code becomes visually pleasing to look at. So after the colon, you should always do spaces is a good idea. So moving on, you can give multiple arguments. As many arguments as you want, you can give all separated by commas. So in this case, average of a and b will be a plus b by 2 in this case. So I can calculate the average of 12 and 10 fairly simply. Now one important convention that is followed in most Python, good Python code is to write what is called doc strings or documentation strings. So it is highly recommended that all functions should be documented. So we write a doc string along with the function definition. So let us try this. Now if I do average of 12 and 10, it is 11. But what is average? Remember when I did average question mark, it exactly gives me that help. Notice returns because the average of the two emperors supplied is input. So the ipython magic will work for every function you define. But if you did not write it for my f function, I did not supply a doc string. There is no documentation. Therefore it is a very good idea for you to supply documentation in a part of your function. Every time you write a function, please supply a document string by default. You should always do it. So now we know how to create functions that return nothing or return something. How do you return multiple things? Turns out it is very easy. So let us say you have a circle function in this case and I want to return both the area and perimeter. Given a radius, return area and perimeter. So in this case I am saying pi is 3.14. Area is, pi is obviously not 3.14. I am approximating it. This is wrong code. But just to illustrate the idea of a function, area is pi r square. Perimeter is 2 pi r. And notice that the return is simply return area, perimeter. So what will happen here is if I call circle 4, nothing will happen. So let us try this. This is a bad doc string. I should have written it better. Pi is not even that but I do not remember too much. Notice that it returns the tuple. But I can say a, p is circle 6 for example. So it allows us to return multiple values and you can return as many things as you want. And you are not constrained to return. So supposing you have a function that has a conditional. For example you say if r is positive, do something. Return two values. If r is negative, return one value. You can do anything you want basically. You can return nothing. You can return something. It is completely up to you on how the function is written. Okay. So I will quickly recap what we have learnt on functions. Creating a function is as simple as saying def function name. Please note that the function name like variable names in Python have to be a standard form. They cannot start with a digit. For example this is illegal. Trinvalid syntax. Whereas as12 is a reasonable variable name. A function name should be a reasonable variable name. So alpha numeric characters, you must not start with a numeric. It cannot contain dot. For example, I cannot say p dot a s is 1. That is a syntax error. So you can do alpha numeric and underscore. No white space. No comma, semicolon, all of those are not allowed. It is a standard. So any of those names are supported. Def, f. Any number of arguments or no arguments. Optionally return. You should supply an argument, a doc string and you can return multiple quantities by basically you could return a list or you could return a tuple like this. Area, parameter. Now that you know this, take two minutes. Try to guess what this function does. I will give you exactly two minutes time. Please try and figure out what this function does. So does anybody have the answer? What does this function do? Assume that n is an integer. You may assume that n is an integer. What is passed is an integer. So everybody is saying that if argument is odd it returns false and in case of even number it returns true. Then what is all that division by 10 and all that? Why do you do all that? Why do you need a while loop? Or even I can simply do n percentage 2 is equal to 1. If n percentage 2 is 1, there is a reminder, then it is odd. If n percentage 2 is 0, it is even. Why do I need all that while loop? If n is negative, all of that, why do you need all of that? So think before you answer. This is not a trivial question. Just typing out the answer is going to give you a negative response. No output. You should get negative time minus 100 marks. No output means no input so far. So let me help you this time. The next question I will not help you. Very good. So you are getting close, separate into digits if it is even. So if n is an integer and assuming it is in a decimal number system obviously, if n is less than 0, make n minus n, which means if it is negative, it will convert the number to a positive number, simple. Then whenever n is greater than 0, which means you are going to keep dividing by 10, so at one point it will become less than, it will become less than 10 and dividing that it will become 0. So long as it is greater than 10, greater than 0, check if the number is odd or even. A number is odd or even, purely you can determine by the last digit. So if the last digit is odd, if the last digit is even, it will be divisible by 2. The last digit is odd, it will not be divisible by 2. So any of the digits of that number is odd. It will return false. If every digit of that number is even, it will return true. So why? Because when n is greater, so let us say we have n is 123, n is positive, so this y, this if condition will not be handled. Please note this, it is possible instead of having to say colon next line, you can say it in one line itself. If n less than 0 colon, you can give it a single expression, single statement here. So while n is positive, so n is now 123, n percentage 2 is equal to 1, 123 divided by 2 is, 123 modulo 2 is 1. So it will return false. But what if it is 122? Okay, 122 it is not false. So it comes to this line, it will divide it by 2 by 10. What do you get? 122 by 10 is 12, 122 by 10, integer division. Remember they are all integers. So it is 10, sorry it is 12. Now it will go back to the while loop. It will again check. 12 is divisible by 2, yes. Again it will divide. What do you have left? n is now 1. Is n greater than 0? Yes. It will go to this. n divided by 2 is, n modulo 2 is 1. So it is false. So it will return false. So even though 122 is even, the answer is false because the digit 1 is even. So basically this function checks if every digit of this integer is even or odd. If there is one, if all digits are even, it will return 2. That is all this function does. And this is why it is important that you document functions. Now this is your chance to redeem yourself. Tell me what this function does. Very easy. Again assume n is an integer. Assume n is a positive integer, no problem. So there is no need to write down this code. Just look at it, interpret it line by line. So the function what takes one argument called n? n is an integer. So you are running the out, the program for different input is not what I am looking for. That the computer will do very nicely for me. The exercise here is not that you run this for 5 different cases and tell me what it does for a viewer. You have to be able to read code. If you are a programmer, it is important that you be able to read code and understand it line by line. If not, how are you going to debug your own problem? When you debug, when you have a problem, there is no way you will know anything unless you are able to read it and find mistakes and understand the flow of logic and you cannot do anything. So it finds if it is a perfect square or not and then find the nearest possible perfect square. So basically it is going to iterate, start at i is equal to 1. Check if i squared is less than n. If i squared is less than n, increment i. If it is equal to n, what happens? It will break out of this, return true comma the square of that, the square root of that number. So what if it is less than but it has become greater than it? Then it will be false and that number. So it will give me the next nearest perfect square that is higher than this number and if it is a perfect square it will return true comma its square root or false comma nearest square root of a number that is larger than this number that is a perfect square. So the point of this exercise is that you should be in a comfortable position that you are able to understand enough Python to be able to read simple code fragments like this. So in Python you notice many of the functions and methods that we used had default arguments. So can you give me one example? It is already there in your screen. Round took one argument and a number of digits as an optional argument. Optional arguments are typically called default arguments in Python. So round for example you said 2.484 will give you or 2.4. Round 2.4842 will give you 2.48. So the idea is this two is an optional parameter. For this how do you create functions? The question that we are trying to answer here is how do you create a function that you create which has optional arguments. Another example is split where you gave it an optional argument, range which had multiple optional arguments. So how would you implement these? So the syntax is def same as before. Welcome is the name of the function some example. First argument this is not optional. So when I call this function I have to supply greet because this function does not know what greet is. However name is equal to word, word means the argument word name is optional and it defaults to word. So to understand this better let us try it as an example. So if I say welcome high it says high word. If I say welcome high Guido who is the creator of the language it says high Guido. So the point here is if I do not supply a second argument it will treat that a second argument name as word. If not it will just take the first argument that you supply. What if I did welcome without any arguments? What error should you get? You should get type error saying I expect one at least one argument. At least one argument you have given me 0 because greet has no default value so it does not know what to do. One thing very important is default argument should always come at the end. They cannot come in the starting of the function. This is illegal syntax. So if you try it it will give you an error. So remember default arguments always come towards the end. Now default arguments have a little bit more to them than just defaults. In python the more general way of calling arguments is called keyword arguments. So in the same case let us go back to the example that we had our function was this was our function welcome greet name is word print greet command name simple function. Now if I say hello James welcome this will say hi James no problem. But now if I say welcome hi name equals Prabhu it gives me hi Prabhu. So which means I can name I can say welcome default some argument and a named argument I can say I can say name is this. In addition I can say welcome greet equals hi name is Prabhu. Is this clear? So the actual argument name can also be used to specify the name how you call the function. In addition now that I know the name the order does not matter. If I am going to specify all the arguments by name I know everything it is obvious as to whether it is which parameter is what becomes obvious. So I can say welcome name is Prabhu greet is hello. So once you specify all the arguments in terms of keyword arguments the position of those arguments does not matter. However if I were to do this what do you think will happen? This will give me an error because keyword argument should always come at the last. So if I have any positional arguments like this which do not have a specific name take cannot they should only come first. So the syntax is given a function call any positional arguments. These are all these fellows without any name name equals greet equals that is called the name that is the keyword. If you do not specify a keyword you have to specify them first. Is this clear? So this is important so always positional arguments come first positional arguments are underdog they are just x, y, z, a, b, c it is not of the form argument name equal to some value. For example let me give you an even simpler example def f x, y equals to print x, y I can call f 1, 2 that is fine. I can call f 1 that is fine. I can say f y equals 1 illegal because I am not given x I cannot do this but I can do this. So all the positional arguments should come first keyword arguments come last but if I am specifying everything as keyword arguments I can do f of y equals 1, x equals 2 no problem. So let me make a little more complicated maybe that will clarify things def f x, y equals 2, z equals 3, f of 1 will be 1, 2, 3, f of x equals 7, y equals 2 this is legal because I know every argument I know explicitly what every argument value is but this is illegal. Why? Because I specified a positional argument after a keyword argument which is illegal you cannot do that. So what python lets you do a quick recap you can create a function using the syntax def function name arguments optional return you need not return if you do not return by default the return value is assumed to be null, null is equivalent to c is null. You must provide a doc string as a convenience to yourself and your users the way to do that is to use a triple quoted string and put documentation relevant to this function here. Comments by the way are written using a hash there are no multi line comments there are only single line comments. Comments can also be in the same you can have inline comments the up short is if you put doc strings you can use the question mark feature for your own functions in ipython you can return multiple values by separating them by a comma here are simple examples for you to read and understand the function you can supply default arguments to any of the arguments you must always specify the default arguments last everything can have a default argument but if you have some which are positional like greet keyword arguments should only follow them then you can call the function like so in which case greet will be equal to high and name will be equal to Guido if you say welcome hello it will be the default will be used so you will get hello world keyword arguments is essentially an extension of the same idea the idea is if you say welcome greet name is world I could explicitly say welcome hello James but this is kind of it is not clear what hello is and what James is so I can say welcome greet equals hello name equals James that is all legal I can say welcome high name equals Guido or what I had said or you can swap the arguments name is Guido greet is same but this you cannot do you must always specify the keyword arguments last and not first so any questions on the function stuff that we learned so far question is can we have recursion with python functions the answer is yes there is a recursion limit only 0 has all the bits even why why not 222 the number 222 has all the bits all the digits even it is not a question of all the bits it is a question of all the digits of that number being given so if you go back let us go through the example I give you 222 it comes here percentage 2 is not false so it is divided by 2 again not false divided by 2 it becomes 0 it returns 2 that is it so it exists the while loop because n is no longer greater than 0 you get true for 150 also that is not possible because if you look at 150 you must have entered the code wrongly n is positive is there any reference variable okay so I will first answer the question on if n is greater than 0 so it is 150 it is divisible by 2 so it will not return false it will divide then it will be 150 divided by 2 it has a remainder of 1 so it should return false so unless you have made a mistake in your own typing so not only have you not understood the function you have also typed the function wrong is what I say so 2 more questions is there any reference variable in Python I do not understand what you mean are you saying are there can you call something by reference and things like that basically in Python you can think of everything as a reference okay so as a simple example if you switch to this terminal if I say a is 1, 2, 3 I say b is a and if I now change a of 0 then I b is also changed so essentially do not get confused with pointers in Python the names you see this b this a they are all names so the way you should think of the world universe in Python is that there are these objects like 1, 2, 3 this list all of these quantities are objects and these names you see here a, b all of them are names that are connected to those objects so they are names of that object for example if you look at me this person is Prabhu one of my names is Prabhu my mother may call me son my father may call me son if I have a child my child may call me father they are all different names but they refer to the same entity so in Python it is similar they are not pointers but they are essentially names that are bound to objects okay so do not get confused with pointers, pointer is not arithmetic there are no pointers in Python the next question is how to write a floating point number in a file well if you want to make sure that it is accurate you need to write out all the digits but it is a very inefficient way to write it there are other functions provided by other libraries like say NumPy which will allow you to write so there is a mechanism by which you can do save you have a NumPy array it will let you the third question is do we have par 4 like Matlab well you have lots of things better than par 4 and some things worse than par 4 I do not know what exactly par 4 is but I am assuming that it is a parallel 4 so first thing is parallelization is a non trivial issue and there are no simple default mechanisms to deal with parallelization in Python there is support for MPI there are support there is support for multi processing there is a whole variety of things that you can possibly do but it is not it is not there is no one to one correspondence between Matlab and Python so there are certain things that you can do in Python very easily certain things you can do in Matlab very easily but remember that Matlab is just a numerical processing language the third question is can we use and in conditions yes I had already discussed this in the section on Booleans so if you have true and false the result will be false so if you have two conditioners for example I say a is 1 so I can say a is greater than 0 and a is not equal to 0 that is true so yes you can use and or not all of those can certainly be used can we overwrite the library functions provided by Python yes you can but I would suggest you do not so generally the way in which you do some of these things is to make sure that you do not overwrite certain built-ins that you are used to okay and built-ins are limited there are there is other functionality that is provided beyond the built-ins for example we will look later something you can create called modules when you create a module you can actually shadow or modify to a sense when you import functionality from a module you can call it whatever name you want so it could be a name that is already built-in however that module allows you to define it in a particular scope therefore it gives you some ability to deal with this and trust me it is not a big problem because sometimes if you get into a bug you realize that oh you have overwritten a method or a function that is provided by a library but most often you can figure it out it is not too difficult to figure out when you get into this problem but yes Python is flexible enough that you can overwrite a library function provided by it there are some technical details about it that I do not want to get into and I shall not get into so the answer that I am giving you is not 100% accurate but it should serve for the purposes of this course so let us continue where we left off we have seen all of this we have looked at default arguments and keyword arguments and so far no one has had questions so I am going to skip it you have more questions I guess you can ask us on the chat session on Moodle I am going to move on to try and finish as much as I can of this material today there are several built-in functions in some of which you have already seen so there is a b s there is any there is all length max min you have not seen all but many of the other functions you have seen and as you have seen before it is easy to figure out which what these functions do by doing question mark similarly there are functions called p o w is power there is range you have already seen some you have seen type you have seen all of these are there is more information on all of these functions at this URL that is supplied the slides are available and you can find this so let us look at some more nitty-gritty details about functions before we move on to dictionaries and sets the first thing you have to understand about functions is that all the arguments are local so if you try this simple example def change q equals 10 or okay sorry q equals 10 print q and now let us say I have q equals 1 the q that is defined here has nothing to do with the q that has been passed here as an argument so if I say change 100 it still prints only 10 because the q that is defined here is completely local to this function however if I print q here it still prints 1 so understand that arguments and variables that are introduced inside a function scope are purely local okay so similarly you can look here when is 5 def change so let us try this this will print 10 if I print 10 it is still 5 so which means the variable that is introduced here inside this function is completely local inside here however if I define the function like this in this case it will print 5 the reason is now let us say now let us say n is equal to 125 what happens is if a variable is not found inside the function scope it goes to the scope that is containing this function and looks for a variable of that name if that variable is not available so to delete say the existence of n here I can say del n which case if I do change it will say name error global name n is not defined however there are often times when you need to change a variable that is global in which case there is a special keyword called global so let us say n is 5 I say def change and now I want to be able to change the n that is in the global name says why did n is equal to 10 here as you saw before it is only going to create a new variable in this name space of the function it is not going to actually modify the global n so if you want to do that you simply say global n now if you say n is 10 print n to print n n 5 print n here it will also become 10 so you have to use global if ever you need to deal with global variables but using global variables is usually a bad idea it suggests that your code is actually not very well written so there is one little detail that you have to keep in mind with functions in python if any of the arguments that you supply are mutable that is if you supply a list it does not make a copy therefore let us try this little example name is say Mr. Steve Gosling and if I say def change and now if I say name 0 is doctor and I say change name if I print name now it will change the actual mutable variable that is available similarly if I say change and supply an argument and let us say x dot append and now if I say change name now as you see it says dr. Steve Gosling creator of Java so the point is if you pass some quantity that is mutable into a function and you change that inside the function it is going to change the object that you passed in obviously this does not work for mutable now immutable objects because immutable objects cannot be changed in which case it is going to make a copy so if I pass in a string say there is nothing you can do about it so this is something that you have to keep in mind when you pass functions when you pass arguments to functions so let me quickly summarize some of these issues first thing is that arguments that you supply inside a function are completely local so if you create a shadow variable say q is 10 here or you pass an argument q this q is completely local to this name space so if you assign a new quantity to it it does not change any q that is in the name space that is containing the function similarly if you use variables inside the function they are completely local to that function however if you define a variable or if you use a variable inside a function that is not defined in that function it will look for the same variable in the containing scope so in this case it will look for an n that is outside the change if you want to change the global variable you must use the global keyword finally you have to remember that if it is a mutable argument the mutable argument is passed essentially by reference and therefore you can change it inside the function and the changes will be visible to the actual object that was passed so with that we finish a brief introduction to functions as with all programming languages the way to learn more about it is to actually program more we will try and give you some programming assignments over the course of the next week so moving on so far I have conveniently not talked about tuples at all so to create a tuple as you recall is as easiest using parenthesis so you have parenthesis 1 2.5 hello so just like this tuples can also contain objects of different types they do not all have to be the same type so here we have an integer of floating point a string another integer so on and so forth in this case t is actually a tuple now notice that it is not always necessary to use parenthesis so when I say a is equal to 1, 2, 3 implicitly it becomes a tuple so if I say a is 1, 2, 3 the type of this is a tuple so what is so different about a tuple well accessing the elements is the same so if I say a of 0, a of 1 all of that is fine however I can also do slices and all of that so if I say a of 0 is equal to 100 it will not allow you to do it so the problem with the good thing and the bad thing about tuples is that they are immutable so if you want to create an immutable collection for whatever reason you would use a tuple so one interesting thing about tuples is that it allows there is something called tuple unpacking so when I do a, b equals 1, 2 what is actually happening is it is doing what is called tuple unpacking so when I do 1, 2 it actually creates a tuple on the left hand side again there are two quantities so this is what is called tuple unpacking when I do this if I say print a, b it will be 1, 2 so in python therefore supposing I have a is 1, b is 2 if I want to swap the two quantities I can do a, b equals b, a notice that there is no need for a temporary variable this is one very interesting and convenient thing to do with swapping variables in python it is just one single line no need for all this complicated storing it in a temporary and all of that it is all unnecessary this is again the same concept tuple unpacking so with this we finish tuples they are not quite used beyond that context in this course too much but you will keep encountering tuples and they are like any other sequence you can basically iterate to them you can check for container ship you can access the elements however you cannot set the elements that is all the next major important data structure in python is what are called dictionaries dictionaries are extremely powerful and extremely useful so with lists if you remember if I had 1, a, c, a of 0 is 1, a of 1 is a, a of 2 is c now supposing I wanted to create a data structure where the index that I am supplying this a of 0 instead of 0 that I could give it actually a key let's say I could say a of something or a of a okay this unfortunately do not allow for this a dictionary is precisely such a data structure what you can do is so look at this example this is how you create an empty dictionary the dictionary now if I want to create a key value store for example here is set of extensions jpg colon jpeg image so let me type a simpler example use the curly brace jpg notice that the keys need not be strings alone so I can say 1 is notice now that we have this nice dictionary so now if I say ext of 1 it gives me 1 ext of jpg tell me jpeg ext of data 1, 2, 3 so notice that it gives me the ability to associate two different quantities okay 1 is called the key so what I supply inside this square bracket is called the key okay and what is returned by that is called the value okay please note that in all of the in this data structure there is no ordering in a list there is 0, 1, 2, 3 so here there is no ordering there is no guarantee that 1 is going to be before or after okay the ordering is completely internal and you should not rely on any of the ordering so what you need to think of when you think of dictionaries is that it is basically an association of keys and values so let us say you want to create an address book okay where you want to index it based on the person's name so I will say Prabhu Ramachandran colon supply whatever data his address or something like that or his phone number okay things like that can be done with a dictionary so how do you access elements as I had shown you in the example use the dictionary use the same operator bracket operator and supply the key so the values can be accessed using the keys the values of non-existent keys cannot obviously be accessed so for example let us say ext of bash we will say key error there is no such key bash key does not exist so how do I find out all the keys you can say ext dot keys okay this gives you all the keys and ext dot values will give you all the values okay so if I want to add a new value so if I say ext of cpp I can set it to a new value as c++ so now you notice that in the data structure this c++ code has been added cpp c++ code has been added to remove it you can say del ext so let us say I want to remove this 1 notice that in this everything else is a string the only key that is a non-string is this 1 so let me say I want to remove it so I can say del ext of 1 now let us print ext okay so this is similar to a list so if I have a list say a if I want to delete the first element of a I can say del a of 0 similarly del ext of 1 or say del ext of data now let us say cpp will remove now in addition just like this the notion of container ship also exists so which means if I want to say data in ext the answer is true but if I say jpeg in ext it will say false so you have to understand that when I do this container ship checking it only checks for the keys it does not check for the values so how would you check for the values well I could do jpeg in ext dot values and that will be true okay however container ship of a dictionary is only for checking the keys so as I have already told you extensions dot keys will give you all the keys dot values will give you all the values and you must note that the order that is given by the keys itself is arbitrary so there is no sorting or there may be sorting but you cannot rely on however the order of the values and the order of the keys will be the same order the other useful thing about extension of the dictionaries is that I can write code like this so which means I can iterate over the keys of a dictionary so notice that the same pattern is used it is used in lists it is used in tuples it is used in strings it is used for files and it is also used for dictionaries so the same pattern so therefore it becomes very easy to write and read such code so for each in extension dot keys print each extension each or for each in extensions you can do the same thing with that we finish dictionaries please note that dictionaries are an extremely powerful data structure so please look at ext.tab and you will see that it has a variety of methods please experiment with these and explore these in your spare time we will obviously not be able to go through every single one of these and it is also not important that you need to know every one of these the point being if you are ever in doubt just start up our python interpreter create a new dictionary and then type d.tab and experiment and learn for yourself so if you want to say I want to say from keys question mark it will tell you what it is and then you can experiment play around learn about it and go back this is why you actually never need a manual you can just learn python by using just the interpreter and the documentation that is available online and built in documentation you now move to the last data structure that we will cover in basic python which is sets sets are again a data type that have been introduced recently in python I think 2.5 onwards sets are available as it as a built in data structure so let us see how we can create a set so consider this list so A is equal now if I want to create a set A given any sequence type of AL is a list type of A is a set so what is the property of a set supposing there are repeated elements in a set will it show you all of the repeated elements no it will only show you one for each occurrence so let us see what set has so set has only the unique elements notice that in the list 1 is repeated as is 2 whereas in the set you will only see 1, 2, 3, 4, 1, 3, sorry 1, 2, 4, 5 and 6 so the repeated repetition is gone so conceptually it is identical to the sets that are used in mathematics duplicate elements are not allowed and there is no necessary order of the elements so let us consider 2 sets F10 is set notice that you have to give it a sequence now let us look at some interesting operations that we can perform these are the 2 sets we have first is we can do the union so F10 union P10 so when you put the R you get the union of 2 sets so in this case it should be 1, 2, 3, 5, 7, 8 which is correct F and P10 will be the intersection the difference will be F-P10 which will be 1 and 8 the symmetric difference will be 1, 7 and 8 therefore such operations can be easily defined on sets in addition one can look at subsets proper subsets so for example if I have B is set is B less than equal to F10 yes is B less than equal to P10 no why because P10 does not contain 1 so how do you say one set is contained so B contained in F10 yes B contained in P10 falls therefore you have all of these basic operations of sets and you have the ability to check if something is a set it is a proper subset or if it is just a subset what is the difference between proper subset and subset is a set a proper subset of itself but is it just a subset of itself yes okay so a proper subset is a set that has for whom all of its elements are elements of the set that it is contained in but whose parent set or who the set which is compared has elements that are not in B so if I have 2 sets A and B if A is a proper subset of B then there exist elements in B that are not elements of A which is not true for B and B because every element of B is an element of A okay so how do you check for container ship as usual you can say 1 in F10 1 in P10 okay 4 in P10 4 in F10 similarly iterating over the elements can be done as for I in F10 then tie and notice that there is no ordering 8 came before 1, 2, 3 and 5 okay so never rely on the ordering of a set set simply gives you unique set of collection of elements with a bunch of very interesting operations that you can find that are very convenient so here is a very simple example of how you can use sets for example given a list of marks 20, 23, 22, 23, 20, 21 and 23 list all of the duplicates so let us say marks so look at the code below it basically says creates a set now this set will obviously not include any of the duplicates so all you do is you iterate over every mark of the set and remove those marks from the original marks then all will be left with other duplicates because remove will only remove the first occurrence of that element does this make sense so for mark in mark set so for example what will set be m is a set of marks m will only have 20, 21, 22 and 23 so when I iterate over all the elements of the set the first time it will be 20 second time 21, 22, 23 so it will remove the first 20 then it will remove 21 then it will remove 22 then it will remove 23 so I will be left with 1 more 22 so 23, 1 more 20 and 1 more 23 so all of the duplicates will be remain but then the duplicates will be repeated so if I want the repetition of the duplicates to be removed I convert that to another set does it make sense so if you do not understand all of this example I suggest that you think of how to identify the duplicates in this mark list by yourself without using sets and then you will understand why using sets makes life so convenient so with that we finish the basic python set of slides so let us just look at some questions so the question is after sorting how to remove the duplicates yes to answer your question yes so if you want to do it without sets you can do it it is not doable just that it is a lot harder to do it without sets and you do not need to sort it all you need to do is to find out whether there are duplicates in that list and list out those duplicates so if you use sets it becomes extremely easy to write that code that is all for example one way to do the test is to take each element and count the number of occurrences so there is more than one occurrence it will work but it will be extremely inefficient to do whereas the code that we have written with the set is extremely efficient so if you do the computational time complexity you will find that it is much much more efficient to do it with a set there are many advanced issues for example with respect to performance which we have not quite covered for example if you look at a dictionary the dictionary access so accessing and key value access is actually constant time it is not it is not linear time or logarithmic time so PSD college has a question yes yes I have a doubt yes like we are attending we are attending the workshop for totally four days like yesterday and today and 17th and 18th correct yes but the participants they will be attending on five weekends if it is like totally 10 days yes yes because we will have a lot of exercises so are the course content going to be same like what yes that is the whole point the point is we want to do the yes it is the same it is the same course content the same slides will be repeated that is the reason you are doing it now so that you are already taught this material you are already familiar with the material you know you already have some reasonable idea of what is going on and you already have doubts that are cleared so you are already one level advanced to those people and therefore you will be able to train them that is the idea so if you do it once again your own doubts may be you will get better clarified when we go through it will also be a little you have five weekends therefore there is a lot of time there will be more tests there will be more opportunity and the difficulty then is remember that there will be about 500 participants so things will be a little slower actually a lot slower because 500 people may have questions and so you will have to help them we will have to help them more questions more interaction but the point is 500 people at the end of those five weeks will be trained and you will be doubly trained thank you okay then thank you very much I will sign off please finish your tests and we will evaluate them as soon as possible and I think you will get a score immediately thank you very much thanks for your patience and thank you for coming on a weekend we really look forward to seeing you on the 17th and 18th and look forward to doing this in earnest with the full participants from your respective colleges thank you very much