 Hello everyone, this is Priyanka Singh. I am working here as a project staff in blended MOOCs development team. Now I am going to some so called teaching python thing. So how many of you literally aware of python? Anything about? Okay. So this is like revision class for you and if anybody doesn't know about python then don't fear because it's very easy as compared to as you have all programmed in C, C++ or Java. So we will start with whatever content I am going to cover here next. So these are the contents. These are only basic contents I am going to cover. It was created in 1991. So it's a very old language by Guido Van Rossum. He is a guy from some Niederland. Then you have this python.org is an official website of python. You will get every kind of documentation, tutorials and we are using current versions 2.7.6 and 3.4.0. These are the current versions of python. Next. We call python a strong language because it is very robust. So much features it covers like it's very simple. If you will write python code, you will feel like I am writing simple English thing. It is not so much strictly typed. It's loosely typed language. It's easy to learn because of its syntactic nature. Then it's free and open source. Anybody can distribute it. You can modify codes. So the main advantage of open source thing is it's very robust. You can always have new things to learn and new things to see. This is general purpose high level scripting language. So why we are calling it a scripting language? Because here I have written it's interpreted. It's not compiled language. It's interpreted language. So it's a scripting language because you can make some applications very easily. You can run easily. It doesn't consume so much memory and so much time to run. Then it's user programming. Like you all know Caesar Procedure Language. Why? We make so much functions, modules there. So we call it Procedure Language. So here python has features of we can make functions independent modules. And then it has object-oriented feature also. I guess everyone is aware of object-oriented features like you have learned C++ and Java. So it has also this benefit. Then clear syntax and is expressive I have already. Then this is portable also. This is also one very benefit of interpreted language. It's portable. Why? Because it is not convertible into machine format. It's always contains some intermediate codes what we call it byte code. So if you are running it, for example, Linux or Windows or any other OS, it is very easily portable. Just byte codes portable and then you can run it. Then it's extensible and embeddable. If you know scripting languages, this is the feature you can embed it in HTML, JavaScript and all. Then it has very extensive libraries. While coding, you will learn its packages and libraries to use. How to run Python? So if you are using Linux and you will type in terminal Python, it's already installed. For example here, this is in my laptop. It's 2.7.3 version. There it's telling. When you will run Python, it's telling you the current version and also it's open. It's interpreted prompt. There's three arrows. This is interpreted prompt. Here you can run your program as it is without writing in some files or something. So now I'm going to explain about our first program. In every language you write hello world is the basic program you write to get aware. So it's like in prompt. I am writing this. I have written this in interpreted prompt. And it's like print hello world. Then I have also explained three differences. As I said, it is no strictly typed language. Syntax doesn't matter that much. So here is three style of printing hello world. If you will try it in your terminal, these three are printed same as it is. But the main syntax is first one, defined one. You can also write your program using an editor like WIM editor or G editor. So you can just open any file. For example, I have opened this WIM hello dot pi. Then again for running, you have just to then you have to just tell about what interpreter you are going to use to running your program. So for example, here I had opened. Then I have written this program close the WIM editor. Then you have to just write python hello dot pi. You are telling your system that Python is the interpreter, which will run your program. And then it has already run. Next are the common data types. Swing, int, float, long, this all you have read in C. Boolean was in Java, I guess. Then these are the new data types dictionary, list and set. So I guess Python's main feature is these things dictionary and list and string, declare some data types. So this is also one interesting thing about Python. I have never written this like int variable name or string variable name because Python already take it. You don't have to specify what data type you are going to write. You have just written it will automatically take okay this is a string, this is int, this is float and this is list or something. So I will explain later what the dictionary and list. I guess whenever you write any program or any code, string manipulation is the basic thing you need to know. There are very much string functions. For example, upper, lower, split. You have to specify string like any fundamental languages in these double quotes. This is run program in terminal. So you need to just if you want to practice, you can take this just type it and you will learn. So this is a dot upper. It has changed this string in upper case later in lower case and a dot split. It has taken splitting arguments. Here I have passed white spaces argument. So it has just splitted the string. Then there are other many more methods like strip, replace. Then for example, here it is a dot replace. What it has done? It has replaced your explanation marks with question mark. Like it always replaced first argument with second argument. Then a dot starts with it is just telling in Boolean it's true or not that your string is going to start with some characters. For example, here str then this len a it is telling you your length of string. So in C we have always seen that in any string there will be a terminal character, null character at the end. But in Python it is not like that. There is no null character. So the 16 is the length. Then you can concatenate two strings using plus. One important feature of string is that it is immutable data type. Why it is immutable data type? Because if you have specified a string in some string literal, you cannot change it further. You cannot modify it. You cannot append it. So it's a immutable data type. What its definition is a dot strip. If you are, for example, a is the string I have defined. So it just takes from the starting index to last. It is just for showing like your string is from where to where. That's it. It's like to covering off your string. That's it. Then you will not get this thing in any other language. It's a string formatting. It's very cool thing about Python. What it is, here we have format operator, this modulo sign and then format method. What is this? For example, you are writing some code. There is some specifications. You don't know what to write. You can pass arguments so that it will take after it. For example, here what I have written, character in the percent s. You all know percent s is the type specification for string. So here I have passed hat with this modulo. So what will it, when it will give output, it will automatically take hat on this place. So then they are same thing what do format function. Format function also, 0 is what your first argument. So here we have passed one argument. It will take automatically here. While all examples are very simple, you can see and will understand eventually that here for example 0 and 1. So this is replaced by that. It's simple. Here also you have specified thing and here you have in format you are giving definition of thing. So this is the good thing about Python. Then you can use Python as calculator. For example, basic mathematics operators are here. You have all known this. Then this is just simple by interpreter prompt. How can you do some basic calculations? Basically in data structures you have studied link list, stacks, queues and all. So some combined features are here in list. A list is a data structure that holds an ordered collection of items. In list you can store you know sequence of items. For example, your shopping list like that you can store here and it is mutable data type. I will explain it in next slide. For example, here I have explained the syntax of it. Shop list. Here I have this shopping list what I want to buy or something. Then whenever you are writing your list, you have to specify your list in square brackets. Because the square bracket is Python interpreter that this is going to be your list. Then these are some examples why we are calling it a list as a mutable data type. Because as I explained in the spring that was immutable. So unlike that it is mutable because we can append it, remove some items, replace some item, reverse it. We can have manipulation over list. That is why we are calling it mutable. So here I feel like append, you have added one item, then pop. It is like just stack. It will remove your top element. Then reverse, count. How much time one element is there it will say. Then shorting. These are all basic data structure methods. Then dictionary. Dictionary is like address book. In address book you are like for example, name, then corresponding address or something. So here in dictionary what we do? This consists of pairs, keys and the corresponding values. For every value you have some key to represent it. The dictionary is that you will be using our instances or object of the dict class. So in further slides I will explain class thing. So here it is like we have some dict class in our Python libraries. So dictionary whenever you declare or use it will always be a object of dict class. Python dictionaries are also known as associative arrays or hash tables. If you remember about hash tables, there also we have keys corresponding to values. So that is why we are comparing our dictionary with associative arrays or hash tables. So general syntax is what? Every pair will be attached with colon and then separated by comma and in a curly brace. It is also a syntax to aware Python interpreter. Then we have some examples of dictionary. For example, first of all I have given four items here now. Here it is what a is the value then one is the corresponding key value. Then for example three. For three, three is the key only. Three value and three key. Then for dictionary there are some keywords like in and not in. So you can ask your Python that this element is in dictionary in. I have not shown not in that is also there. You can also have not in. So if not in is correct it will return you false. So this is it returns Boolean value. Here then what is happening? This D square bracket D equals to D. What is that? D is our dictionary name. Then D inside the square bracket it is the value. It is little bit opposite. You should have key in the inside the square bracket but here it is opposite. Here we give value here and then key here. So it is like showing the updated list DD. Then again you can also append some items like this also. I have defined one E variable. Here I have created one new dictionary EE and then I just did D dot update and it got updated. Everybody knows its statements while loop and what for loop. So here are some conditional operators through which you manipulate your decision makings in control flows. Now it is if statement actually it is very similar just you have to worry about little bit. Syntax change. Here what we do? No parenthesis. We do not need parenthesis to specified in a decision. Then column for aware that these are the statements to execute and then one another major change is LF. What we do in in C they are very kind of different different if else structure. Here what we do? For example, I can write like if else if else but it would be what your program would be some bigger and yeah python in python you have to emphasis on indentation. If you have not correct indentation it will give you error. So that is why LF is very important because the headache on this indentation thing and the length of program is got little reduced. So this is only LF for some other condition than else then it got printed whatever decision was true. Then for loop. In for loop the two three different things are for example in. So we sequence through this in keyword it checks that your x is in this range or not. So it goes from the first argument up to second argument for example here one two three so one two four in not included four. Then here is other method also for example I have defined this list. Now I am sequencing for loop through this list only no range nothing and column again for making you aware that these are the statements to execute. Actually same as for loop here just you do some sequencing till some counter for example count here we will print zero one two three till we will go to four. So as I told you that python has procedural functionality also. So you can create your different different logics into different different modules what we call function. So here def is the keyword to define a function hello is the function name then we are passing the argument called name which I have defined fish initially you can change it further. Now that format method is used here okay so in name whatever argument you have passed it will print here in zero. So here it's for example hello without argument it just has taken its default argument what I have specified fish then I have changed here for example ken then I have named kenny and all so you can change it it's not like only default value. Then also some how can you return you can return more than one value one value because here for example in first statement I have written only one value a plus b but here I have written two values then next is classes you send object is the fundamental thing of object oriented languages what are the classes they are new you are defining some new type in your language or in your program then objects objects are called instance variable of classes you talk to talk through objects in your language whatever methods and functions you have defined in classes those are called the functionalities of objects you can call these functions by your object and the new thing here is self what is self whenever you will define any function any function you will have to give this self because self is the keyword to call its object itself it just call this class objects nothing else here for example and it is to initialize that class so here self first class first class is the like parameters we will have to manipulate then self it will just call the class object here I have defined like these things the some different different methods which I will use in next slide my is the object because I am passing this class parameters in it okay so joe is the value for first you have remembered the previous slide so joe is the for first smith is for second so now I have typed my dot first is joe my dot class is smith my dot full name so here it is for example this function you can refer full name okay zero one format then zero I have passed just first and last then you have just called you have you can also change something for example here what my dot status in in it method I have initialize the status with new but now I have changed it with old and it is has taken the current value then exceptions so in cc plus plus exceptions were not there but in java exceptions were there for example if you know already in code what a code statements can raise exceptions or errors so you take care of them in your code here we have this try accept statements I know that a divided by b can raise error if I will divide it by zero so I have put it in try clause okay so whenever the interpreter will go to try if that exception is raised it will automatically go to accept clause here what happened when I pass 2 comma 1 then what is happening it's a normal statement so 2 got printed when I am passing 2 comma 0 so it's raising an exception it is going to the accept clause and it's returning zero for example here what I have defined some my exception class or something what is happening if it will go in accept clause it is saying that zero division error is exception and then what it will raise it will raise this statement for example you are returning zero when you are going to some user is going to run your code he doesn't know what is the meaning of this returning zero because obviously if you're divided in 2 by zero it will not get output as zero here if you're saying divided by zero then what is happening the user will know come to know yeah your test case is wrong you should not divide by zero then this is the last slide file handling I'm not telling much about file handling in C also there is file handling it's just that you will open some file there would be some mode like read write and all read would be the default mode and dot txt would be the default type if you are not explaining any type then it will take this then you will write through this for example here what have what I have done if I'll show you example it would happen poem is for something a variable something I can write to some string or this thing then I am passing it in write it will write automatically in your file now we are closing if you will open that file then that variable value will be printed in your file