In today's video, I am going to discuss expressions, operators and taking user input in Python. So before we start the discussion of expressions and operators, let's discuss statements in Python. A statement is a unit of code that the Python interpreter can execute.
It's like a small unit of code which a Python interpreter can execute at a time. So far we have seen two types of statements. One print statement and the second one is assignment statement.
When we type a statement in interactive mode, the interpreter executes it and displays the result if there is one. Interactive mode in the sense, whenever we open a Python interpreter, Whatever we type in the interpreter, we call it as in an interactive mode. Otherwise, we will write a script. In a file, we will write a script and that script will give it to a Python interpreter.
Whereas, that will not be an interactive mode. Interactive mode means we will be entering the statements statement by statement. Whenever we enter any statements one by one in an interactive mode, the result will be displayed if there is one any result that will be displayed there itself okay a script usually contains a sequence of statements when i say a script say it will contain a sequence of statements in a particular file and if there is more than one statement the result appears one at a time as the statements executes like say for example say if we type print1 means if the script contains say two statements like print one x equal to two and print x the output of this script will be one and two say what i do say before i proceed further say let me show you say little bit in a interpreter i have launched a linux terminal and here i am going to launch python 3 interpreter say to launch python 3 interpreter just type python 3 in a linux terminal enter say i have launched python 3 interpreter say here Say what I do is if I write say for example x equal to 5. This is a assignment statement. Basically when I enter assignment statement the x python interpreter will make a space for x. And there it is going to store a say 5 in that particular say memory location which is allotted for x.
And when I enter it it will take it and even result it will going to display like. Say for example, when I type x, it is going to display 5. Now, say if I take one more, say y is equal to 10. Yes, it has taken. And y if I print 10. Now, if I type x plus y, it does, it will fetch x value, it will fetch y value.
It adds both and prints the result. Look at this. But this will not be the case when we write. the same expression in a script. When we write the same expression like x plus y, it will not print 15. In an interactive mode, it is going to calculate and the result will be printed.
Whereas in a script, it will not be the case. To print, we are supposed to use their print statement like print say x plus y. Then only it it is going to print in a script. Otherwise, say just x plus y will be an expression which will be calculated.
but its say result will not be displayed and dear students writing a python script is very simple what say we can do is say just you can use any editor like here in linux i will use vi editor vi space like fi rst first dot py because it is a first script that i'm writing so i'll give a name first extension of a file will be dot py python dot like say cprogram.c say javaprogramming.java here.py for python say vi is editor and first.py is a file name so when i enter it say now a new file is open now to edit a file you press i or insert okay here i can directly write say for example a simple thing print some hundred then say x equal to 5 then y is equal to say 20 then print x plus y simple script I have written. So I will say just save this to save escape shift colon wq enter Now to execute this just give this file to a python interpreter python3 Now if you see dear students python3 I am calling a python interpreter and to that I am giving a say first dot py which is a file file containing a python script Now enter say the interpreter will going to take the script and it has executed say 125 is the output of the script with this we'll proceed further to operators and operands. These ones operators are special symbols that represent computations like addition and multiplication addition subtraction multiplication something like that so means these are special symbols which are used to represent some computations like addition, multiplication, subtraction, division.
The values the operator is applied to are called operands. Basically, dear students, operands are, operand is a data on which we perform any operations. Means it's a value.
Operand is a value on which any operation will be performed. And there are different types of operators like say plus, minus, star. For multiplication, this is divided by and this is double star for exponential. Say these operators will perform addition, subtraction, multiplication, division, exponential respectively.
At this double star, he is going to calculate the exponential. Like say 2 power 3 can be calculated as 2 double star 3, something like that. Here, these are the sum of the valid expressions, 20 plus 32. r minus 1 where r is a sum variable say r into 60 plus minute say r and minutes are variables minute by 60 these are valid expressions now there has been a change in a division operator between python 2.x and python 3.x yes dear stones there is a little bit change in a python 2.x and 3.x so many changes are there one of the changes with respect to a division operator In Python 2.x, means Python 2, the division is integer division.
Whereas in Python 3, the division is floating pointer division. That I will show you if you like to see. Like say Python 2, I call Python 2. Say this is a Python 2 interpreter.
Here, when I type 7 by 5, you can see answer is 1. means this is integer division okay like say 5 by 2 answer is 2 the quotient is purely a integer okay say say in python 2 say this division operator is going to perform a integer division say whereas in case of a python 3 the division operator will perform like say 7 by 5 he's going to give 1.4 say 5 by 2, it will go into uv 2.5. Means basically what I want to tell you, there are many differences between Python 2 and Python 3. One of the difference is this division operator. In Python 2, division is integer division whereas in Python 3, say that is a floating point division.
Hope this is clear to all of you. With this, we will proceed further. to an expression. As I told, an expression is a combination of values, variables and operators. Means expression will contain values, it will contain some variables along with the values and variables some operators.
A value all by itself is considered an expression and so is a variable. The meaning dear students, even just a value written like say for example 20 is considered as an expression. Even just if you write a variable name, it is considered as an expression. Don't think that some z is equal to x plus y is only the expression. No.
Even if I write just 15 is an expression. That's what. A value all by itself is considered as an expression. And so is a variable. Okay.
Means even variable and values alone are considered as expressions. Because of this. Below all are valid expressions. 17 is a expression. x is an expression.
x plus 17 is an expression. Say this alone is an expression. 17 alone is an expression.
x alone is an expression. But remember say x should have some value. Say x should have been assigned a value.
It should be a variable. x plus 17 is an expression. If we type an expression in an interactive mode, the interpreter evaluates it and displays the result.
I told earlier also. I have. shown this also. Let's say for example x is equal to 15. When I type x plus 17 it will going to evaluate it and displace the result in an interactive mode.
But this will not be the case with the script. When I use the same expression like x plus 17 in a script it will not give any 32 hours output. For that purpose I should do this print x plus 17. Then only it will going to to print it. Otherwise, it just evaluates and sits quiet. 1 plus 1, 2 is a result in an interactive mode, but in a script, it will not produce any result by its own.
If at all you want to see the result, you should use a print statement. So, with this, we will proceed further to order of operations. When more than one operators are used, in what order those operators should be evaluated so that is very important so which will going to depend which will depends on the rules of precedence say for mathematical operators a python follows a mathematical convention like the acronym says pemdas say is a powerful way to remember this say what is this pemdas is first we are supposed to evaluate parenthesis then exponential then you multiplication and division have same precedence addition and subtraction have same precedence and same precedence are evaluated from left to right this you need to remember very properly dear students the acronym pemdas will tell that so you are supposed to first give a priority to parenthesis later to exponents and multiplication division same precedence addition, subtraction, same precedence.
Whenever operators with the same precedence will be encountered, you are supposed to evaluate from left to right. This is the order of the operations. Say with this little bit about the modulus operator, because this is one of the important operator which normally we use.
The modulus operator works on integer and yields the remainder. when the first operand is divided by the second that you know it okay in python the modulus operator is percent sign even in many other languages the modulus operator is the percent sign only the syntax is same as other operators like say here if i write one more dear students as i told in python 3 7 by 5 will going to give you integer sorry floating point division but if at all i want to obtain an integer division that can be done by 7 divided by double slash 5. Now this will give you the integer division. It's also called as a floor division. Let's 5 divided by 2. Try to remember dear students this this is a floor operator or floor division.
F L double O R floor. So this means just 5 by 2 is going to give you the floating point division whereas 5 double divided by 2 will give you the integer division say now say this quotient so as i told quotient 7 say if at all i take some q is equal to 7 mod 3 say what we get say if i type q say it is 2 right and say print quotient say if i write say just print q it will going to print it right say reminder is 7 mod 3 see if we print reminder say say reminder is equal to i can write 7 mod 2 and let me print reminder print reminder say reminder is 1 so 7 divided by 3 is 2 with 1 left over that's the meaning the modulus operator checks whether one number is divisible by another and extract the rightmost digits are the digits from the number means basically dear stones so we can use this modulus operator to extract the rightmost digit Like if I take a number say 478 and if I divide this by 10, the rightmost means 8 can be extracted from that. Just see here the result will be 8. And later what I can do is 478 say divided by 10 if I use it, I will get 47. And later 47. mod 10, I will get rightmost once again, 7. And later, say what I can do is 47 divided by 10, I will get 4. And later, 4 mod 10, lastly 4. Means whatever I have shown here, series of say expressions, these expressions can be performed repeatedly in a loop. Say what we get later is given a one number something like this it will going to give all its digits one by one.
Say here we got the first digit, here the second and here the third one. Means basically a modulus operator can be used to extract the digits from the rightmost side of the any number. Hope this is clear to all of you. With this we will see. some of these string operations string already you know dear stones it's a group of character terminated by a null character null character is slash zero okay so whose ascii value is zero itself so now uh the way plus operator works with other integers and floating point numbers it also works with strings but in addition The expression like say x plus 2 means 2 plus 3 will going to give some sum.
Whereas in case of string it will going to concatenate. Say instead of giving a sum it will going to concatenate. Say if we see say like say if we take one integer first equal to 100 second is equal to 150. When we perform print first plus second say obviously it will going to perform the addition. 100 plus 150 will be 250. It will going to print 250. Whereas when the same thing performed on strings, the answer would be different.
Like say first is equal to 100. In this case, first is string. Whereas in earlier case, first was integer. Second is 150. It is also string. Now when we perform say print first plus second, the answer will be not 250. Instead it is 100 and 150. This is concatenation. Same thing I can show you here.
Say like say first equal to 100. Say dear students here there is nothing like declaring a type which I have told earlier also. Based on the value assigned the variable type will be decided by the Python interpreter itself. Now first is an integer type because integer value has been assigned to it.
Now second is equal to 150. If I write print first. plus second it is 250. Now say here say first I'll take it as a string then second then print first plus second hundred and one fifty means this is concatenation plus operator on string is going to concatenate two strings and say for example even instead of on digits we can perform on anything like say f is equal to if i take some welcome then s is equal to 2 python now say print f plus s welcome to python hope this is clear to all of you like a plus operator there is a asterisk means a star operator which also works on strings but what it does is when we you multiply like say first is equal to best and second is equal to four when we multiply first into second it is going to multiply it is going to print this particular string which is stored in first four times means four times this string will be printed the result will be something like this best best best means this particular string will be printed this many times okay So we can see this here say I will write first is equal to best second is equal to say 4 now if i write print first into second best best best there is no space afterwards because of that no space has been given and one more thing dear students instead of first into second if i write second into first then what i get let us see the answer is unchanged okay so whether say you multiply 2 into 3 or 3 into 2 the result would be same similarly here also whether first is a string or the second one is means first operator operand is a string or second operand is a string will give the same result okay say here first is a string and the second one is integer whereas here first operator first operand is integer and second operand is a string but still the result is same now with this we'll see how to ask the user to input some value like say for example in so many scenario instead of proceeding with some value which is already stored we expect a you we expect user to enter the value means some input how to read that user input in python so this is very simple dear students the user input normally we will read from a keyboard python provides a built-in function called input that gets the input from the keyboard. So this is very simple.
Like say when this function is called the program stops and waits for the user to type something. When the user presses return or enter the program resumes and input returns what the user has typed as a string. So a very important thing is the function that is used to take the input is input itself.
Input okay. It will go into return whatever the user when this function is called the program awaits. It will stops and waits for user to enter something. When user enters and later presses a enter key or return key whatever the user has entered it will be given to the interpreter as a string.
Whether you type a float whether you type integer character anything. it will be returned as a string. So like here, say if we see, INP is equal to input.
When we type this, say the program is going to stop and wait for a user to enter something. Once the user will enter, whatever the user enters, it will be stored in INP. I will show you this.
Like here, INP is equal to input. Now it is waiting. Waiting for user to, type something say here i will type say for example 100 now it has returned if i see inp what is stored in inp say inp and enter look at this 100 is stored and that too it is a string because everything because how do i tell this as a string like say for example if i write inp plus 200 i'll get us an error because say inp is a string whatever will be entered it will be taken as a string and 200 is an integer we cannot add both but one thing say even dear stones if you see print imp plus 200 it is going to give you the error but this if i take this as a string obviously it does a concatenation concatenation Because INP is a string and even this is a string, it does concatenation. 100 and 200. Means you can understand that, say whatever the user will enter, it will be taken as a string. One more thing, say INP is equal to input, say if I write 30.67, 34.67.
Now, INP, it is considered as a string. Now, if at all we want... integer to read then what what would be the solution before that one more thing say here whatever is entered like say god is great even whatever you can you can enter anything like say god is great say if i type print i n p it will be printed right say now one of the important thing is before getting the input from the user normally we'll tell the user something like say when we want to write an addition of two integers then we want the user to enter both the integers. Normally we will print some message, enter two integers. If at all such message has to be given to the user, that can be done using the same input function.
We will write something like this. name is equal to input what is even name. Now whatever will be typed within a single or double quote within this input function will be printed to the user.
and whatever the user enters will be stored in this. I'll show you this name is equal to input what is your name? Now means whatever is written it is entered means it is typed.
Now user whatever the user is going to enter it will be stored in a name. Say if I use print name the name is stored in a means it is printed whatever is stored in a name it is printed okay say most of the time whenever we want user to give some like say before asking at the time of asking input if at all particular type of input is required we can tell that within a single or double quote here okay to enter so and so input so that user will act you can do accordingly can enter the data accordingly The sequence slash n at the end of the prompt represents a new line which is a special character and that crosses a line break. Like say for example, dear stones, here when I entered what is your name, there is no slash n. Because of that, say bkgudur has been printed here itself. If at all I want to break a new line, give a new line, we can use a slash n and then enter.
look at this whatever the user will enter it will be on a new line okay say here print now this time name will be python right so wherever you want a new line you can use slash and if we expect the user to type an integer we can try to convert the return value using a function called int means dear students as i told whatever the user will going to enter it will be read as a string if we want that as an integer say for example then we can convert that explicitly using a built-in function called int like say for example here if i write input inp is equal to input enter an integer now if i write 100 100 is not integer it is still a string but i can convert it how say for example i you I use n is equal to int INP. Now if I print n it is integer. How?
Even I can check by adding something to this. Whereas INP is not a integer. Like say if I write print INP plus 200 I get error because INP is a string.
Means you know one line I can convert like say for example say INP is equal to. Here itself whatever user will going to enter, I can convert that to an integer by using the int function. Like say 120 and now say if I want print imp plus 30, 150. Means directly whatever the user will going to enter that I am converting into integer and storing in imp. Even the same thing daystones like int you have float also. whatever the user will enter it will be converted to float like 24.5 means it is not in entire an integer you can change it later ok so means basically what i want to tell you is this the function int is going to convert if it is convertible like say for example don't expect this interpreter to convert a to integer like say for example enter an integer you are supposed to enter digits Instead of that, if you enter 1a, it cannot convert.
You can enter 10. But 1a, it cannot convert that to integer. Okay. So, there should be only, say, ds. Then only it converts. Otherwise, it cannot convert.
I hope you can understand this. With this, we will stop today. In next class, I am going to discuss comments in Python. Thank you. Thank you for watching.