 Good afternoon. Let us begin. I have a question where someone has asked to explain how to add more than one function in one file in our own module. So, I am writing a file called utils.pi. So, we define the function here. What function? Let us say gcd. There is gcd in this, then let us add another function called LCM. I have called it utils.pi. So, now what are the things imported? We can take a look. See gcd and LCM. We can use gcd. Of course, this will give an error. We say utils.gcd. We got it. So, there is no real difference to using multiple functions in a module. Simply type them. Now, of course, we have not added any test code here, but we can and we should do it with. So, that is about it. Any other questions anybody? Some people are asking something very specific. I must show on the gray scale. Please send the question to Moodle. We will answer it there or rather I will I cannot answer it now because I do not know. We will have someone who knows answer it. We have a question. When and where do we need an else in the for loop? Let us first understand what is an else in a for loop because such a construct does not exist in other languages. And by the way, it is not specific to the for. The while also supports an else. It is probably a little easier to understand in the context of the while. So, let us start with that. Let us look at this scenario. There is a loop which is there on a condition and inside the loop you break based on another condition. Then such a thing happens. How do you know at the end of the loop? Let me write a fictitious loop which says while just so that there is no error, I will create some artificial values. But I write some statements here and then inside that I check something else. If I have such a loop, I have written a dummy function now so that it will work. In this loop what happens? When the loop is over you do not know why or when the loop ended. It could have ended because the condition a less than b changed or because the condition is perfect square of a star a plus b star b changed. Obviously, there is no working statement in between that while loop and the if. But assume there are other statements there which modify a, b or both. Now, sometimes at the end of the while loop you need to know how it actually changed, how it at the while loop actually ended. In that case you use a while. Let us look at one of the classic examples given in the tutorial for that. Let us say you want to identify the primes. How are you going to loop? You have to check. You are wanting to check. A number is a prime. Let us say that is a function you want to write. You will start by dividing by factors from 2 onwards up to square root of n, because there cannot be a if there are no factors below square root of n there cannot be a factor larger than that. I cannot use a break here, sorry. Give me a minute please. Let me organize my example. Sorry, that was okay. We want to test up to square root of n. What we do know is if it is 0, we know it is not a there is no need to check for factors after that. We are trying to check whether something is a prime. If it is 0 by 1 number that is all that needs to be done. Now, here now I have a problem. If control comes here it could have come because of two reasons. It could have come because f star f is greater than n, in which case n is a prime or it could have broken before the loop ended. Now, the classic way to check this is to put one variable here. In the if condition you reset that variable and outside the loop you check. Now, you know that if break is true the control came out of the loop because of the break statement rather than anything else. Then we know this is one classic way to deal with this sort of problem in normal languages. In other words we are trying to check whether we broke because the condition is true or because the condition is false. The point is if n percentage f equal to 0 if for example, n is 22 we know that f is 2 and n percentage f equal to 0. So, f star f is not equal to n. But it has broken even though f star f is sorry not greater than n still it has broken. Now, this checking setting a condition inside the while loop and then checking it outside is definitely looks like a rather poor style of programming. So, it is a good idea to use the else here. So, the else gets activated if the while loop exits normally. Think of it as a if statement if f star f is less than n else means f star f is not less than or equal to n which will happen only if the while loop continued all the way and then came out. So, that is the easiest way to understand how else in a loop. The idea is similar for a far once again the else statement makes only sense only if there is a break statement inside the loop. If there is no break statement inside the loop then it does not make sense to use an else. The else is used to discover whether the far or the while ran all the way to the end and then the loop exited or if you exited the loop by a break in between. So, to summarize you use the else in a looping statement under two conditions. One there is a break there is an if and a break inside the loop. Two just immediately after the loop it is important to know whether the loop ran all the way or whether the loop was exited via the break statement. When these two are true you use an else and thanks for a nice question that was from VIT Velour Institute of Technology. Another question how to use python in a web development? You need to use a python web development library the most the ones with shall we say most buzz or Django spelt with a D Django or there is a later framework called Pramed. There is a simple web 2 pi the programming test you enter which gets validated is built using Django by the way. Then web 2 pi one of the most famous website built during using web 2 pi is a site called reddit.com. It is a very simple news aggregation site with a very minimalistic interface it is a lot of traction. So, that is a good example for the type of sites which are very good for web 2 pi. Django is lot more large little more complex but it is generally useful for this big web applications. You also have something called turbo gears then Zope. Zope is considered very complicated even by experienced developers. So, these are a few choices but like any open source product or any open source project the number of choices for doing web development in python is very large. So, it is not like that the Java world where you have to use something specific or no. There are too many choices but you are likely to see one of these Django or Zope or turbo gears more often than not. But definitely those would not be the only ones you will see you will see lot more others. Next question please can we make a class in python and inherit that. Yes python is a multi-paradigm language and it does not insist you write in a single paradigm. The code we have been so far writing have all been good procedural code. The good part may be debatable in some cases. Procedural code we can write good reasonable functional code which in my opinion is better than writing object oriented code. But yes you can write object oriented code and inherit etcetera from there. Instead of using the keyword def use the keyword define a class I am not writing the exact syntax I am just telling you how it works define whatever you want to do here I am doing a dummy statement. Then you want to instantiate another class sorry you want to define another class y which derives from x do this. So, I have just written a very very high level picture of what you do. So, this is the way to inherit from a given class. The next question is how to find the execution time of a python program. I think yesterday Somaya asked me a question and in response to that question I showed the answer. But anyway I will do it again. This is nothing to do with python any program can be timed in the Linux world if you are on windows I would not know how to do it. I do not even know whether it can be done without some complicated extra fitting from either a third party or Microsoft. The way to find out the execution time of any program is to call time on that program. So, I can say python I want to run a program that is semi-couple dot pi. So, you will find first the program is executed there is no change to the program output yes Bhopal you got the answer right that is a way to do it. So, that we will finally tell you what is the real time what is the user time and what is the system time. So, effectively the program spent about you can find out you can do man time to find out what the three different times are and what is the relevance and so on. But this is the way to measure a execution time of any program in Linux not necessarily python program. Next question when you are on the subject of measuring please remember this measures more than the run time of the python program. It also measures the time taken for the python interpreter python language to load itself and so on. And given that it is an interpreter there is no way that time can be not taken into account. But in some other sense the same program is going to run twice the program may not take the same amount of time. So, may be it is worth figuring out how much time does it take to. So, here we are printing all 5 digit ones now we will print the whole thing. So, the next question is is there any efficient way to count words in a file. Again the answer depends whether you are on windows or Linux if you are on windows it is hardly likely you will be interested in doing anything efficient in the first place. So, we will answer for Linux the answer is yes you just type w c minus l sorry w c minus w. So, all these files. So, this is the somebody given example saying cat file name pipe to w c minus w l that cat file name is redundant you should probably do directly this or this one of the two. What is the difference between the two is something I will leave you to explain since you already done shell scripting and basically nox you should know what is the difference between the two. Because there is a question on multimedia modules I have no idea I do not like writing code about any graphical user interface or on graphical user interfaces. So, this please explain using python program to count words in file all right we should that be a difficult thing it is going to pivot very simply on how you define a word. So, assuming you define a word as something that is separated by what your split command gives you can simply say we will try and write it as useful program. So, that the file for which you have to count is come in the command line. So, each line how many words are there. So, line dot split line dot split gives you breaks up a line into its constant components separating on any combination of white space. So, number of words is simply the length of that tells you how many different words are there and in the end of the file when you finish processing you know the total number of words in the file. So, to run 10 times it took 3 minutes and 52 seconds. So, that gives you an idea of a more slightly better idea of the time than anything else all right. Is Bhopal does it is this what you are looking for or were you looking for something else when you ask the question. There is a question about python database connectivity well python has separate modules for connecting to all norms standard databases MySQL, PostgreSQL as well as to some of the modern no SQL databases like MongoDB and so on. So, but that is a separate module and these are third party modules not part of the python standard library. For that matter please remember sci-pi is also a third party module not a part of the standard python because the standard libraries are just those which we think in day to day usage every person will need. If a person needs anything more than that person can import an additional library that is the idea. So, database connectivity is part of an external program we import sorry external module you import and then use. Next question is there any exception mechanism in python you know people are confusing me I am a man not a manual you are adding extra letters yes there is a exception mechanism in python and if you are just spend 5 minutes reading up the tutorial you will see that, but yes there is an exception mechanism in python you have seen it often whenever you try to execute anything you hear the you see that exception you see that give a type error is capitalized in a particular way you see name error errors and exceptions are part of python and you use. So, you can catch a named exception alone that is why I showed you the names you can catch you can have multiple except statements each one catching a different exception and dealing with it as appropriate you have the usual final a classes and so on. Please remember python is a modern full fledged programming language. So, if you before you think of asking does it have a assume it has probably a b c d x y z and a little bit of alpha beta gamma and a lot of delta for any value of a b c. If you want to use python as your primary programming language for doing any job you have to do you would not be far off. People used to normally talk about performance is slower on interpreted languages. So, compared to see definitely python is slower compared to java it is probably a little slower, but normally you do not worry about at least today in 90 percent of the cases you do not worry about the amount of computer time that is taken this discussion of performance of code is a throw back to the 1960s and 70s main frame when you are paying some 500 rupees per second of computer time. Today what is lot more expensive is programmer time your time and effort is lot more expensive than any amount of computer time. If you have to figure out something over two hours in order to get a program to run faster by one second forget one second even if you save a run time of one minute and it takes you one hour to find out you better be expecting to run the program at least 60 times to break even. So, remember that and as practicing programmers yes we may have concerns on certain areas of performance because there are other parts of the system there are performance mechanisms and so on, but by and large if you are trying to do some computation for your purposes particularly scientific and engineering computation and general purpose scripting general purpose programing there is absolutely no need to look beyond python it contains everything many many things you are not even thought of asking are there in python. Next question is why are his and some other commands called magic commands they are called magic commands because they had to find some name to distinguish they are not python commands they are not operating system commands, but they use some internal mechanism in i python to find out some of these things. For example, I am saying c d here and normally if you do I am sorry I am running c d here to change the directory using p w to find the home the current directory and so on. So, like this there is it has built in lot of hooks. So, percentage is the percentage commands are generally referred to as a magic commands and that plus this you can see even p w d is referred to as a magic command because it has to go outside of itself and do something and come back. So, some name was required and please remember the open source community is a little interested in humor. So, they chose magic how to create g a applications in python like web development you decide which g a application toolkit you want again you have lot of choices some of the choices are oldest and the most probably most often used by anyone coming new to python is called t k and t k inter then one of the largest and most comprehensive is w x python then you have q t yeah this would be the three most well known g a tool kits in which you can write code to build g a applications in python. Of course, if you are on the windows environment and you are fond of paying the microsoft tax and you have either paid it and got visual studio or magically manage to have visual studio appear in your hard disk you can build the usual windows based microsoft u a based applications as usual by using the version of python packaged as part of the c l r called iron python then you are using the usual windows g a tool kits available these are operating system independent tool kits all three of them w x python q t and t k next question alright the question is as follows. So, I do not know what the question is yet, but I will attempt to answer the situation explain in the question at least your function f of x for convenience we will simply f of x just returns x could have written anything complicated does not matter. So, we know we can call it using f of 10. So, we have so this is a way to use it now what happens if I say f equal to 10 nothing happens, but you can no longer use f as a function because now you have over written the value of f now it is just a type in let us try that again now look at what is type of f it is a function now the moment you say f equal to 10 it is like a same variable being assigned two different values you assigned a equal to 1 assign a equal to 1 and then assign a equal to 2 the value 2 is lost sorry the value 1 the first assigned value is lost, but here you have simply change the type of the value itself. So, you can no longer do anything with it that is it if you delete now you can no longer use f as a function if you tried you will get a big complaint integer object is not callable. In fact, you often people end up over writing built in variables because of this I will give you one standard example we often see let us say there is a there is a built in function sum which will give you the value the sum of the values of a list often sum is such a natural variable name we say something like sum equal to 0 now we can no longer do this it will complain, but since sum is anyway a built in object you are only over shadowing it. So, you can recover the use of that sum by doing a del sum that cannot delete the built in internal function object will only delete a variable defined in the local scope and you will get it back. So, this is nothing peculiar to your own defined once, but the difference with your own defined once is once it is lost you cannot delete whatever is covering it and get it recover the other one. Next question, there is a question is python used for programming in artificial intelligence. I would like to ask a question which book textbook they will consider the standard reference today for artificial intelligence. Which textbook would you consider the standard reference today for artificial intelligence? Do you know Stanford university recently opened the their course on standard so, artificial intelligence for everybody or a still Norvig. Do you know what Norvig is doing? I am asking do you know what is Norvig's role, what does he do? Is he a college teacher? Is he a developer? What does he do? Do you have any idea? Do not know. He is director of search quality in Google and the latest edition of his the standard textbook you recommended uses python as a language to implement all the algorithms. In other words Russell Norvig uses python to implement all the algorithms. Does that answer your question? In fact, please go to Norvig's site you will get some wonderful insight into python as a language pitanovic.com right. You can see pitanovic is not a great believer in graphical interfaces. So, he has some wonderful articles on very many things. You will see his thoughts on python. The best is to look at python for list programmers. It is a wonderful article to read. I understood more about list from this article than any books I read. Norvig is worth looking at and he will also give you some very good ideas for writing code. Possibly the best is how to write a spell checker, spelling corrector. Please read this article. It tells you how to work on the level at which people like pitanovic think. You use python to write a simple spell checker to explain the algorithm behind Google correcting your spelling. When you search for something and you make a spelling mistake, you will often find Google asking you did you mean something else and yes, he is giving you an algorithm which does most of the work of the same in python. But, unlike us we simply jump in and write code. You will see he explains some probability theory. He uses some corpus text. He uses a testing corpus then an actual corpus in order to do everything and the way it is written, it is corrected. The whole thing is approached with a level of rigor that will make us think we know nothing about programming. And Bhopal, if you think rich and might is the ultimate reference in artificial intelligence, you are a little behind times or you are spoiled by UGC and its book recommendations. Alright, next question. Fine. There are no questions. I will do one last bit of show about the real power available to a python programmer if he or she is interested in writing good code. And when somebody asked me about classes, I made a comment in python you can write in multiple paradigms. You can write in the pre-or procedural code like we have been writing or I said something about we can write functional code. I am sure many of you, some of you at least know what pure functional programming is. But, just to show you how easy it is to write such very powerful code in python, I will take two examples and then finish off for the day. The first is a very simple fairly ordinary program. If I remember right it is the first program in project Euler website. By the way, if you want to have a supply of good programs to test your understanding of any new language, the best place is project Euler dot net. Go there, take a look. There are some 300 odd problems. All of them are centered around mathematics as the name project as the name Euler should tell you. But, they form good exercises to hone your basic understanding and skill in a language. That said let us look at what is this first problem I am talking about. It is a problem like find the total of all multiples of 3 and 5 below 100. I think it is below 1000 in the, yeah it is below 1000. So, it is not a exciting or a difficult program by any stretch of imagination. But, there are some interesting things you can do. Classically you are likely to do something like this. A procedural program will do something very similar to this. We will assume below 1000 means 1000 is not to be included. Like I said nothing very fantastic, very nice to even talk about. But, does it mean we would not make mistake in these type of programs? Yes, we can. Already I made one in the code. Forgot to increment n. Now, here there are many things that are characteristic of a good imperative program. There is a while loop, there are if conditions and there are two variables namely n and total whose value change, keep on changing. Now, let me rewrite it in a slightly different way. I will leave it here and go to the interpreter to do it my way. Now, what is interesting about this program? If this one line can be called a program, do you see any variable which is being modified? Do you see any if condition? Nothing. Such a style is characteristic of what we call a functional style of program. We can write code, we use we write code using recursion lot more and we do not use variables whose values change. If you recall the collats I wrote that was written in a very functional style. It called itself and there was not a single variable which was changing. Now, did you do the Armstrong problem by any chance? We have done the Armstrong numbers also. So, let us look at I will just write the code. I am sure you know how you will write Armstrong. So, if I am going to write Armstrong, how am I going to do it? Let us start from the top. I will assume I am going to be given two numbers a and b and I have to find all the Armstrong numbers in that range. How will I do it? Simple. That means, I have to write the e's Armstrong function. So, how do I know whether a given number is Armstrong? So, if the sum of the cubes of the digits of n which are two different functions, sum of cubes obviously returns the cubes of this and digits returns the digits of a number as a list. Once again no variable which changes its value anywhere in the program. The way you would have written probably you would have taken a variable, stopped it, chopped off the last digit, front digit, back and so on. This style is something that for many people is lot easier to think about. In fact, we can improve this program by saying you know what sum of cubes there is nothing about cubes in Armstrong. If I say 100 to 8000, I must do fourth powers. So, I would not say sum of cubes. I will change it to sum of powers and make the power another argument d comma n and say instead of this, return this and here say power equal to d equal to digits of sum of powers of d comma length of d. So, this solves the generalized Armstrong problem. If I give something like say 100 to 10,000 this will work. Let us save it and see whether it works or whether I am talking through my head. Like I said the most notable thing about the program is there is not a single variable that changes value anywhere. You could also look at the collats I wrote. So, this is one of the real powers of python as a language. It is a genuine multi paradigm language does not insist you write in one particular style. You could write good procedural code, you could write good object oriented code or you could write a mix of procedural and functional code or you could write a predominantly functional code. Whatever suits your style and whatever suits the nature of the problem under development. That is about what I wanted to share about python. Thanks a lot for sparing your weekend. We will stop here.