 Again welcome to this session hi everyone welcome to this session again we will be continuing our look into the Python programming last time the last lecture we introduced the Python programming today I will continue in my the topics that I will be talking about I mentioned one thing which I did not cover during last time so I will plan to cover that plus some more topics for today so last time I talked about the scope scope of variables in Python the scope is very similar to what we have for tickle so I just wanted to just give you an example so first of all the scope of a variable is limited to the extent of a function so if you are using a particular variable that variable does not go outside the function once you exit out the function that variable is gone pretty much and the variable outside the function are not also taken into the function so if you are changing any variable which has declared outside the function in within the function the Python creates a brand new variable for fun even though it is the same name and just overload the name and then it assigns the value that you are assigning and once you leave the scope of a function that particular variable is gone and it uses the global variable once again so if you try to change anything within the function and then try to print out outside you will not see any changes it will just be the same over so here is one quick example I am going to pause it for a for a minute okay is limited to the extent of function once you change the variables essentially like I mean so any variables outside the function cannot be changed within the function so Python usually assigns a new variable or the same name and overloads that name and then uses that within the scope and then once you go out again you do not see those teams and the same thing so if you want to have a variable defined outside and then you want to use it within the function you need to declare that as with global keyword which is shown here essentially it is global keyword so here is one example this this section is actually like a Python code let us review this one so here I am declaring a global variable it is called global war and then it is assigned to a string and this is global now I am defining my function where I am defining a local war called and the we basically assign this is local to that local war and then global war I am changing from that this is global to this is local so if I print within the function it will be still the same the global war will be same as this is local and the local war is also the same so that is when I execute this now if I print outside the global war and tell me like I mean what it will print it is still going to print this value as if it never got changed by my function this is again because when you define it here this global war is a different global war it actually creates this variable assigns the local this is local and then it prints out this but once you go out it uses this so in order for us to change the global war within the function we need to declare here global and then global war so this basically like this statement enables Python to actually get through this this issue and then actually change that this is local and that is visible outside the function so now when you print this one instead of this is global it will print this is local so that explains the scope of variables of the variables for so today we will be continuing our discussions so just a recap in the first lecture we saw several topics basically I just wanted to just run through several of them I hope you remember all these things we started with like some basic stuff how to run Python programs we talked about variables we talked about how to print and then what are the operators in Python some of the banned keywords also like that we talked about and then what are the rules for having a keyword then we talked about how can we input a variable into the into the in in Python or into the program and then how to write comments and then now today we saw the scope okay so I hope those things are very clear for you now let us go to today's lecture basically with today we are going to talk about the conditionals basically true or false booleans true or false booleans comparison and logical operators and then we are also like going to look at some of these if al if else statements again remember Python is a shorthand programming language we want to minimize and get the most effective things in Python so here the main thing is like the type is what is called the main thing basically and type boolean boolean and then we say like type true that is of type boolean it does not matter like I mean if you specify the lower case team here that is not defined so essentially like that just does not an error so the booleans are defined as per case t RUE and it is FALSE so make sure that you remember the case and use that and if you specify something like this 2 plus 2 equals equals 5 this is a conditional checking basically whether this is the same and then the result is a false so the true or false are of type boolean and the capitalization is required for booleans so just remember that a boolean expression is an expression which can be evaluated true or false the expression evaluates to false if it is the it is a numerical value of 0 essentially like I mean there are different one one is the constant false the object none and empty sequence or a collection or the numerical item of value 0 think of this basically like I mean this is the primary one you have also the constant false that we saw earlier they evaluate the boolean expression evaluates false then also it returns a false the object is none it again returns a false has an empty sequence or a collection it returns a false and then finally any numerical value of 0 is also false and everything else other than these four conditions it is actually true for the boolean expression so let us look at some of the comparison operators so this one we saw in the first example there is equal equal that is actually it is a comparison to find out whether two things are equal and then the not equal to is again it is a easy one it checks if the two values two value operands are equal or not and if the values are not equal then the condition becomes true so in other words this one is evaluates to a equal equal b you can say basically and then if this basically like whether it checks whether it is true or not they are equal or not here it is basically like a not equal to be and if that becomes true then the thing is true and then you have greater than and you have less than and greater than or equal to is another operator and less than or equal to is another one then we also have the similar kind of operator that we saw in the tickle or even in Perl which is like a board operator this essentially checks if the value of two operands are equal or not if the values are not equal then the condition becomes true so in other words this is similar to the not equal to and then we also have something is the two references refer to the same object this one we will cover in chapter 6 so make a couple of one lectures and then we will go through that basically this is like one level of interaction when you talk about preferences address is the same even though the values need it so that is another thing that you can find out and in Python actually one thing that you can do is you can actually change these comparisons so you can define like the multiple inequalities multiple qualities multiple comparisons in the same line in this goes to that short hand notation that talked about so here a simple thing basically 0 less than or equal to a less than or equal to 99 and if a 42 this expression will return true but if a is say minus 10 this evaluates to false even though like this side may be true and if it is a is 100 then also it returns false because this will evaluate true but this side is going to be false so you can you can actually specify these shortcuts this actually saves a lot of time and what code now let us look at some other operators like logical operators are and or are not essentially the and or not is essentially like through the real typing the numbers typing the the actual words themselves out so we can specify say like two plus two equal equal five or one plus one equal equal to this will return true because this side is true and essentially like I mean this may be false but false or true is actually true if you say and now false and true that is actually false then you can also specify not of this which means basically like this is now true and then this and this and that becomes true as well so one thing you note that actually we do not use the amplitude ampersand or bar bar or this bang as in C so we do not do the C level stuff for using the logical operators they are explicitly specified in the plus form so even though we said basically we won't specify this form the double ampersand and all these things there will be like single ones that are that are used which are mostly used for the bitwise operators so we will talk about that in a little later now we go to the conditional one if then else so here just one example called if else the pie so you can think of you can see this code actually this is one plus one little two then print that and then print I always thought so and else print my understanding of the math is not be faulty and a simple one line add is use this so we notice that actually so column here or both if and else and which is quite important when you specify the if statement the simple one will be basically one and then see that very simple one so all these cases actually like a minute the value it is true so the first set gets executed and then the second set is ignored you feel like two things one is I mean of course this column here and then there is an intonation here in the string statement same as else the intonation is quite important in Python where the intonation gives it the framework of what is the it is almost similar to a function call or make a function statement the elit statement is actually it is very similar to else if in C or ELS IF in tickle or even Perl so here is a simple one there x equal to equal to one then print one x equal to equal to two else F or LF print two and then else print main so it is to then this particular thing will catch and then so now let us go into the more the next level so one thing on the LF is basically like you can have as many LF's as possible in this one that is a first one call and then blah blah blah LF second condition blah blah blah all columns and then there may be several LF's before you can actually go to the else so this is one thing that you may want to just say basically Python also supports loops which we will talk about in the next section there are while loop and for loop are supported so we will talk about that now let us look at functions again in function we will talk about how do we define function we already saw like some of the basic basics of functions and then we also see how to return values local variables which we will cover once again the scope and then some built-in functions some functions of functions and then how do we pass lists dictionaries and EO E words and into the function so let us begin the functions basically you define them in a file above the point where they are so you need to define it before you are using the main reason is Python is interpretive language so you need to know what it is before you can use it and the body of a function should be intended intended consistently so four spaces is typical in Python so everything for the increment so here we declare a function called square and you can see basically like definition the function name followed by column and then we leave four spaces and then we basically define the function so the usage will be you can say like print square of three is and then call the square function square three and then out will be like square of three is nine couple of things one is that depth statement which is essentially the first one first statement is for function how do we define it the depth statement is executed basically it is not like a parse and thrown away basically it is executed and that is why we need to define the functions before we can use them and what happens is when the depth gets executed the depth creates an object and assigns a name to that to reference it the function could be assigned to another name the function names can be stored in a list and we can put a depth statement inside and if statement and it can be like nested and more basically like it is all arguments to a function optional if you have multiple arguments you basically separate it forms and if there is no return statement then none is written the return values can be simple types or tuples or basically two element are in this like the a, b and with a function returns the values they may be ignored by the color itself one thing to note is the functions themselves are type less that means that we can call with arguments of any type as long as the operations in the function can be applied to those arguments and this is considered as a good thing because now you do not have that dependency of the type of the argument anymore now this is something that we saw earlier as I mentioned variables declared in a function do not exist outside the function so here is one example depth square n which is m is defined as in square then return n so if you say like square of three and then you ask it to print square three and then you print m Python will generate this error so this is the opposite of what we saw in the first slide regarding the scope because this m does not live outside of this function which is these two statements so it does not it cannot actually live outside we can capture the value of square three because it is returning the m we can capture this as this is another variable called m itself m equals square nine square three and then it will be there but this way even though this can actually this will work this does not work so the file comes up with an error the variables assigned within a function are local to that function called that we all saw and then variables assigned to the top of the module are global to that module there is only global within a module so this is the use of global to represent the global value of the the way so within a function Python will try to match a variable to one assigned locally within the function if it fails it will try within the enclosing function that defining statements that is BF if appropriate if that also fails then it will try to resolve the name in the global scope if none of this match then Python will look through the list of built-in names so what are the built-in names that we will see wrong so on the scope essentially like I mean another example here is 5 C is a plus B and then return C then if you say funk 4 what is it return and then we say print C basically then it does not return but it basically like prints 4 plus 5 is 9 so within the function we can still make it print but once it is outside the function it becomes not so here is another example a is equal to 5 and then we say like global C and then C is a plus B then return 5 so outside when we say funk 4 it is actually now use the result and then if you say like print C then now it is also defined more more it is defined to be 9 so this global variable actually helps in more straightening out some of the differences earlier and notice that actually the evaluation takes this in the function as this addition and then that you see so unlike other programming languages like Perl Python in in Python everything is by reference but please also note that there are mutable objects that are not changeable so changes to immutable objects within a function only change what the object name points to and it does not affect the value so and it also does not affect who calls this program so the immutable objects like integers strings people Python acts like a C acts like sees pass by value and for mutable objects that is example is lists Python acts like a sees pass by pointer in place in place changes to the mutable objects can affect some earlier things so here are some examples one is a pass by reference here we define the function f1 x and some places of x and y and then we just print out 1 plus 1 is plus 2 plus 1 and then we just print out all the values now we take the next function x f2 we declare x equal to x times 1 and y 0 is y 0 times 2 so this is presumably like an array function so now when we ask you to print out basically it turns out to 0 and then to 2 now we continue basically we make it a to 0 b is 1 and 2 again it needs to be an array or it to be used inside then f1 a b that is what we are going to call so that returns this value and then actually string out that too and then we say basically like print a b and then this and then we use apply the second function with a b and 0 and 2 and 2 so here we are just passing the reference into the fine to the function and then getting back and then multiple values can be written by a python program but usually they happen with packaging into a triple or array so there is a small package basically 1 2 3 x times 1 x times 2 x times x times 3 and then if you print the 1 2 3 program with the value of 3 then because now you can return three values instead of one or two now Python has a rich set of built-in functions for example math dot pi installation you should be able to see there are several functions available they are all pretty much free one is the point POWW so when we say like POWW 2 3 that is basically like stands for power and basically it computes the so if you say like I mean this is a and B this computes a to the power of B and ABS stands for absolute so this is minus 14 and then these are some of the nifty commands find the maximum or amongst these numbers and then here doubt which has three it is the highest integer there so these are some of the fun things to do with Python so you will enter this now a function of a function essentially so here is even is a function basically which is just x and f and then we do a test basically x is the same as f of minus x then we print through else and how do we determine this is because we now do a square basically so we are defining another function called square n and then which returns basically just multiplication and then we also define another one for cube and then that returns and n times n times n so now we we say okay print even to square and then print is even to cube so the output of these two are the first one is it true and then the second one is a pause because you can see basically so first thing to note is this is one function and basically this is a top level function and here essentially all we are saying same as f minus x then through else calls now when we talk about in square basically we only return in n or n times n and the same thing here so now if you look at this one actually it is a function of a function because you are calling the easy one with an argument to and then basically also we are calling the square so in for square you need only one number so basically what happens is it knows that okay it is a square and it passes this number into that square confuse the answer and two becomes x square becomes f and then it is again the easy one is evaluated with these numbers and then it comes up with this answer so now some default arguments like in c and java we can define a function to supply the default value for an argument if one is not specified so these are useful for printing out the error messages and things like that so here we have a defined a function called print error and it gets line number and some message the message is error already like it is predefined here so and then we just print basically the error message error at line number so and so and then that is a line number so when we say like print error 42 it just means basically error at line number 2 so these are like predefined values that you can supply into the function and essentially like I mean that is what we get so now let us talk about the function without any return value in general all the functions in python return something if the return value is not given then by default python returns none so this is a principal difference between python and the tickle as you know tickle actually returns last evaluated expression when you are writing like python code beware of assigning a variable to the result of a function which returns none for example here the the list append function changes the list but it does not return a value so and we say basically like okay a 0 1 2 and then we say about append 3 and then that is assigned to B and if you say print B instance none even though this is executed in correctly and basically this will be the 0 1 2 3 but it only prints none so you may assume that okay but now that this is correct it should get a value of true but actual value that you get this so this is basically like what I wanted to cover during this lecture so we talked about several things I hope you remember we talked about the scope of the variables we talked about the the the conditionals essentially that is how we started with basically through and through and false booleans then we went into like how to compare logical operators comparison and the logical operators and then we also talked about if a list and else statement then we continued our look into functions how do we define function how do we actually execute the function and then what are the other things associated with it one of which was the scope and we went into more depth rather than just mentioned it in passing last time and then we went into like this values the the the the various scope examples and then we talked about how the results in Python and then we saw some examples how to use some multiple values from functions okay so thank you once again and we will see you in the next lecture thanks okay Santosh