Friends in this video
we are going to see how to provide input for your Python
Script while running, and how to provide output from your
Python Script,
and also we are going to see the usage of 'eval' function.
Right. So guys, here I'm explaining this concept from my Linux
box. I'm opening my Linux terminal, right. From there
I am going to explain about this.
And you can also try the same thing, whatever
I am trying from Unix, or Unix-like systems, or Linux, even
on your Windows as well without any change. The only thing
on our Windows we configured Python as simply 'python', but
here Python is like 'python 3' name, right. Fine.
Let me write simply a script called 'input_output' script,
'.py'. So guys,
I am going to design a simple calculator, arithmetic calculator,
so "simple arithmetic calculator".
So I am fixing numbers like 'a=', and 'b=', right,
then I am finding the 'result=a+b', and I am
trying to print.
[no audio]
"The addition of and is". So here
I am trying to print my 'result'.
[no audio]
Fine. So this is our script, whatever we have. Now
I am going to print the 'result' with the help of Python 3, not with,
so if you see simply 'python', then it will work from Python 2.
In Python 2 you don't have like this syntax "f", formatted
string you don't have. Only in Python 3 it is there. So
'python3 input_output'. It's working perfectly.
But if you observe our script is like "simple arithmetic calculator",
or simply addition. Let me give only option as addition,
"simple addition calculator" because I'm not doing here as
of now addition, subtraction, multiplication, division,
let me do the only simply addition.
[no audio]
Right. So whatever the script you have as of now that script if
you run with any number of times your output would be always
12, for 4 and 8.
Right. But my requirement is, if it is an addition calculator
while running in this way, our script has to ask "Enter your
so-and-so number", "Enter your b number", and then it has to
give the 'result'. Each time
it has to ask. Each time
it has to take two numbers, then it has to find the 'result'.
But as of now we are fixing our values 'a' and 'b' inside
of your script, but I don't want to do in that. Then what I
can do is, instead of 'a=4' always,
I am going to read my number from command line.
So for that this is the syntax. "Enter a value".
So whatever you're writing inside of your input parenthesis
in terms of quotations, that will be printed as it is
on your command line. Then let me take even for 'b' also input,
"Enter b value".
[no audio]
Right. So as of now, I am not going to perform your 'result' because
I want to show you something. 'print(
'The value of a is: )', and 'type is', I mean, what is
the data type, I want to print that. So 'type(a)'.
[no audio]
Right. Okay, let me check it.
You can check even for 'b' also. I am taking only for 'python',
sorry spelling mistake, 'python'.
Something wrong.
Where is it?
[no audio]
'type(a)', 'print', okay.
Sorry, we are using 'python'.
It's not 'python', 'python3' because we are working with Python 3
version. Only in Python 3 version
we have 'f', formatted strings.
Okay, see that?
Yeah, before that let me show you your script once. So this is your script.
You know, whenever if you run your Python Script it will
run from top to down, and while running if any line starts
with a '#' symbol that line won't execute, simply it will skip.
Now you have only three lines in your program, these two and this line.
So because of 'a=input', first your script will take
'input' with this command some value from command line.
Let me enter some value 45 and some 'b' value, then see the
output. You are getting value 45 like a string but you know
that is a number. We want to feel that as a number but your
Python 3 is taking with the help of 'input', right, that as a
string. So whatever the 'input' you are providing that is always
a string if you read that with the help of 'input'.
Right. But whereas in Python 2 you have 'input' to read numbers
and you have 'raw input' to read strings but here there is a common,
common input is there, common input statement is there that
is 'input' through which whatever you are reading from command
line that is always a string. Then how you can convert to your
required data type?
We have different things,
I mean different types.
Let me open my Python 3 terminal. Guys, whatever we are going
to discuss now that will work only with Python 3 not with Python 2.
Let's say 'x= input', right, "enter x:".
[no audio]
So I'm entering something. What happened?
Yeah, I have started with single quotation and ended with double
quotation. That's wrong.
[no audio]
'Enter x: ', I am entering something called 56.
[no audio]
Right. Then, see that. 'type(x)' is string but I want to feel that
as a number integer.
So that's why while reading, you know type conversion we
have, right. So simply
[no audio]
you can write in this way.
[no audio]
Read and then convert into as a integer. This is one way.
[no audio]
So whatever you are entering from your command line, something wrong.
[no audio]
Indentation. Yeah, no problem.
[no audio]
Read something and convert that into integer. Now see that,
'print(type(x))',
[no audio]
integer, right. Or else you have one more default function
called 'evaluation'. Using this based on whatever the data you
are entering your Python will identify
what type of data you are entering.
Let me enter some 3.4, right. Now 'print(type(x))',
[no audio]
float. So 'evaluation' function will automatically convert your given
data into particular data type.
Anyway, 'input' is only to read strings.
But if you want to read some other values you have to use
evaluation, right.
Fine. Let me do one thing.
I am going to repeat same.
[no audio]
'input("Enter x")'. Now I am going to enter a string called
"hi", right. See, 'hi' is not defined. It is not taking. So whenever
if you are using evaluation and whenever if you are providing
string. Anyway by default input is read
read to string. But whenever if you're using evaluation
[no audio]
you have to provide your string as a quotations. That's it.
Now 'print(type(x))'.
[no audio]
That's it.
Okay. So be clear. Use 'x=eval(input("Enter"' your required
thing, and whenever if you are entering string please provide
in terms of quotations.
Otherwise, if you want to read only strings simply use this
one, enter your string.
[no audio]
And in this case, you don't need to provide anything inside
of your quotation. Simply you can write.
Hi, Hello, that's it, right. Now see 'print(x)'. It will work.
So whenever if you use evaluation function for your 'input',
then you have to write your quotation inside of double quotes.
Right. Okay. Now, let me design your simple calculator.
[no audio]
so 'input'
[no audio]
'evaluation'
Okay. This is the syntax to read your numbers.
Of course, you can also use type conversion but I am using
this one. This is of course somewhat better, right?
So I will show you that as well guys.
[no audio]
So first of all observe your script. You're having now this many
number of lines, 1, 2, 3, 4, 5, because of first line if I run
my Python Script with python3, right, because of first line
it is reading something, second line
it is reading something, then you are getting 'addition', 8, and
type is integer for your 'a'. Even 'b' is also same, right. So same program
you can also write in this way without any confusion.
Let me comment first all these lines.
[no audio]
So what I am doing is 'a=int', but this syntax
will work only to read integer numbers, but you may enter
integer or, right, some float or maybe some complex sometimes,
so that's why I don't prefer this one.
[no audio]
Right. So I don't prefer this actually, but just there is
one more option in case if you want to work purely with
integers, right. Then at that time you can take this one.
[no audio]
But by using evaluation automatically it will identify whatever
the data you are entering, right?
[no audio]
So I'm entering 4, 5, see that 9, integer, but if I
enter something like
5.7,
right,
[no audio]
it is not accepting because
it need only integer values.
Right. But if I use simply with evaluation it will work
perfectly. So that's the reason if you want to read anything
from command line just try to use evaluation of your input
and at that time in case if you want to provide any strings,
you have to write your strings in terms of quotations.
But anyway, as of now, we are going to work with numbers
only. Let me take 4.5 and 9.,
some 2 see the result.
It's working perfectly.
So that's why guys this is my suggestion, try to use because
while going forward anyway, definitely all organizations will use
Python 3 only that's why just try to remember in this way
to read inputs. Okay.
Okay guys, thank you for watching this video.
[no audio]