1 00:00:00,000 --> 00:00:01,800 Friends in this video 2 00:00:01,800 --> 00:00:05,900 we are going to see how to provide input for your Python 3 00:00:05,900 --> 00:00:11,000 Script while running, and how to provide output from your 4 00:00:11,000 --> 00:00:12,000 Python Script, 5 00:00:12,200 --> 00:00:15,500 and also we are going to see the usage of 'eval' function. 6 00:00:16,300 --> 00:00:21,700 Right. So guys, here I'm explaining this concept from my Linux 7 00:00:21,700 --> 00:00:25,900 box. I'm opening my Linux terminal, right. From there 8 00:00:25,900 --> 00:00:27,700 I am going to explain about this. 9 00:00:28,600 --> 00:00:30,800 And you can also try the same thing, whatever 10 00:00:30,800 --> 00:00:35,000 I am trying from Unix, or Unix-like systems, or Linux, even 11 00:00:35,000 --> 00:00:38,000 on your Windows as well without any change. The only thing 12 00:00:38,500 --> 00:00:41,900 on our Windows we configured Python as simply 'python', but 13 00:00:41,900 --> 00:00:46,800 here Python is like 'python 3' name, right. Fine. 14 00:00:47,000 --> 00:00:51,000 Let me write simply a script called 'input_output' script, 15 00:00:52,000 --> 00:00:54,000 '.py'. So guys, 16 00:00:54,000 --> 00:00:58,500 I am going to design a simple calculator, arithmetic calculator, 17 00:00:58,500 --> 00:01:03,000 so "simple arithmetic calculator". 18 00:01:03,900 --> 00:01:08,500 So I am fixing numbers like 'a=', and 'b=', right, 19 00:01:08,500 --> 00:01:13,800 then I am finding the 'result=a+b', and I am 20 00:01:13,800 --> 00:01:15,000 trying to print. 21 00:01:15,000 --> 00:01:16,900 [no audio] 22 00:01:16,900 --> 00:01:25,300 "The addition of and is". So here 23 00:01:25,300 --> 00:01:27,000 I am trying to print my 'result'. 24 00:01:27,000 --> 00:01:30,000 [no audio] 25 00:01:30,000 --> 00:01:34,400 Fine. So this is our script, whatever we have. Now 26 00:01:34,500 --> 00:01:38,800 I am going to print the 'result' with the help of Python 3, not with, 27 00:01:38,800 --> 00:01:42,300 so if you see simply 'python', then it will work from Python 2. 28 00:01:42,300 --> 00:01:46,100 In Python 2 you don't have like this syntax "f", formatted 29 00:01:46,100 --> 00:01:49,800 string you don't have. Only in Python 3 it is there. So 30 00:01:49,800 --> 00:01:54,000 'python3 input_output'. It's working perfectly. 31 00:01:55,000 --> 00:01:58,500 But if you observe our script is like "simple arithmetic calculator", 32 00:01:58,500 --> 00:02:02,700 or simply addition. Let me give only option as addition, 33 00:02:02,700 --> 00:02:06,100 "simple addition calculator" because I'm not doing here as 34 00:02:06,100 --> 00:02:09,000 of now addition, subtraction, multiplication, division, 35 00:02:09,000 --> 00:02:10,800 let me do the only simply addition. 36 00:02:10,800 --> 00:02:13,500 [no audio] 37 00:02:13,500 --> 00:02:18,500 Right. So whatever the script you have as of now that script if 38 00:02:18,500 --> 00:02:21,600 you run with any number of times your output would be always 39 00:02:21,600 --> 00:02:23,400 12, for 4 and 8. 40 00:02:24,500 --> 00:02:29,900 Right. But my requirement is, if it is an addition calculator 41 00:02:29,900 --> 00:02:33,700 while running in this way, our script has to ask "Enter your 42 00:02:34,200 --> 00:02:37,000 so-and-so number", "Enter your b number", and then it has to 43 00:02:37,000 --> 00:02:39,100 give the 'result'. Each time 44 00:02:39,100 --> 00:02:41,100 it has to ask. Each time 45 00:02:41,100 --> 00:02:44,600 it has to take two numbers, then it has to find the 'result'. 46 00:02:45,000 --> 00:02:50,500 But as of now we are fixing our values 'a' and 'b' inside 47 00:02:50,500 --> 00:02:53,400 of your script, but I don't want to do in that. Then what I 48 00:02:53,400 --> 00:02:55,800 can do is, instead of 'a=4' always, 49 00:02:56,300 --> 00:02:59,300 I am going to read my number from command line. 50 00:02:59,300 --> 00:03:01,700 So for that this is the syntax. "Enter a value". 51 00:03:03,200 --> 00:03:05,800 So whatever you're writing inside of your input parenthesis 52 00:03:05,800 --> 00:03:08,900 in terms of quotations, that will be printed as it is 53 00:03:08,900 --> 00:03:13,700 on your command line. Then let me take even for 'b' also input, 54 00:03:15,200 --> 00:03:16,900 "Enter b value". 55 00:03:16,900 --> 00:03:19,400 [no audio] 56 00:03:19,400 --> 00:03:24,900 Right. So as of now, I am not going to perform your 'result' because 57 00:03:24,900 --> 00:03:27,500 I want to show you something. 'print( 58 00:03:32,100 --> 00:03:40,700 'The value of a is: )', and 'type is', I mean, what is 59 00:03:40,700 --> 00:03:45,500 the data type, I want to print that. So 'type(a)'. 60 00:03:45,500 --> 00:03:49,200 [no audio] 61 00:03:49,200 --> 00:03:51,400 Right. Okay, let me check it. 62 00:03:52,100 --> 00:03:59,300 You can check even for 'b' also. I am taking only for 'python', 63 00:03:59,300 --> 00:04:02,600 sorry spelling mistake, 'python'. 64 00:04:04,200 --> 00:04:05,200 Something wrong. 65 00:04:06,100 --> 00:04:07,500 Where is it? 66 00:04:07,500 --> 00:04:10,600 [no audio] 67 00:04:10,600 --> 00:04:13,900 'type(a)', 'print', okay. 68 00:04:14,300 --> 00:04:15,800 Sorry, we are using 'python'. 69 00:04:15,900 --> 00:04:18,600 It's not 'python', 'python3' because we are working with Python 3 70 00:04:18,600 --> 00:04:20,100 version. Only in Python 3 version 71 00:04:20,500 --> 00:04:22,399 we have 'f', formatted strings. 72 00:04:22,500 --> 00:04:24,000 Okay, see that? 73 00:04:24,200 --> 00:04:28,600 Yeah, before that let me show you your script once. So this is your script. 74 00:04:29,200 --> 00:04:31,900 You know, whenever if you run your Python Script it will 75 00:04:31,900 --> 00:04:35,300 run from top to down, and while running if any line starts 76 00:04:35,300 --> 00:04:38,400 with a '#' symbol that line won't execute, simply it will skip. 77 00:04:38,400 --> 00:04:41,900 Now you have only three lines in your program, these two and this line. 78 00:04:42,100 --> 00:04:46,400 So because of 'a=input', first your script will take 79 00:04:46,700 --> 00:04:49,500 'input' with this command some value from command line. 80 00:04:49,500 --> 00:04:54,900 Let me enter some value 45 and some 'b' value, then see the 81 00:04:54,900 --> 00:04:59,000 output. You are getting value 45 like a string but you know 82 00:04:59,500 --> 00:05:02,800 that is a number. We want to feel that as a number but your 83 00:05:02,800 --> 00:05:07,600 Python 3 is taking with the help of 'input', right, that as a 84 00:05:07,600 --> 00:05:12,600 string. So whatever the 'input' you are providing that is always 85 00:05:13,000 --> 00:05:16,100 a string if you read that with the help of 'input'. 86 00:05:18,600 --> 00:05:22,100 Right. But whereas in Python 2 you have 'input' to read numbers 87 00:05:22,300 --> 00:05:27,400 and you have 'raw input' to read strings but here there is a common, 88 00:05:27,600 --> 00:05:32,200 common input is there, common input statement is there that 89 00:05:32,200 --> 00:05:35,000 is 'input' through which whatever you are reading from command 90 00:05:35,000 --> 00:05:40,300 line that is always a string. Then how you can convert to your 91 00:05:40,300 --> 00:05:41,400 required data type? 92 00:05:42,900 --> 00:05:44,100 We have different things, 93 00:05:44,200 --> 00:05:45,200 I mean different types. 94 00:05:45,300 --> 00:05:50,500 Let me open my Python 3 terminal. Guys, whatever we are going 95 00:05:50,500 --> 00:05:53,900 to discuss now that will work only with Python 3 not with Python 2. 96 00:05:54,700 --> 00:05:58,500 Let's say 'x= input', right, "enter x:". 97 00:05:58,500 --> 00:06:00,500 [no audio] 98 00:06:00,500 --> 00:06:02,300 So I'm entering something. What happened? 99 00:06:02,400 --> 00:06:04,800 Yeah, I have started with single quotation and ended with double 100 00:06:04,800 --> 00:06:05,800 quotation. That's wrong. 101 00:06:05,800 --> 00:06:09,000 [no audio] 102 00:06:09,000 --> 00:06:13,200 'Enter x: ', I am entering something called 56. 103 00:06:13,200 --> 00:06:15,300 [no audio] 104 00:06:15,300 --> 00:06:22,200 Right. Then, see that. 'type(x)' is string but I want to feel that 105 00:06:22,200 --> 00:06:23,500 as a number integer. 106 00:06:23,600 --> 00:06:27,800 So that's why while reading, you know type conversion we 107 00:06:27,800 --> 00:06:30,500 have, right. So simply 108 00:06:30,500 --> 00:06:33,500 [no audio] 109 00:06:33,500 --> 00:06:34,700 you can write in this way. 110 00:06:34,700 --> 00:06:36,600 [no audio] 111 00:06:36,600 --> 00:06:40,500 Read and then convert into as a integer. This is one way. 112 00:06:40,500 --> 00:06:45,100 [no audio] 113 00:06:45,100 --> 00:06:49,500 So whatever you are entering from your command line, something wrong. 114 00:06:49,500 --> 00:06:52,100 [no audio] 115 00:06:52,100 --> 00:06:53,800 Indentation. Yeah, no problem. 116 00:06:53,800 --> 00:06:58,700 [no audio] 117 00:06:58,700 --> 00:07:02,400 Read something and convert that into integer. Now see that, 118 00:07:02,400 --> 00:07:04,100 'print(type(x))', 119 00:07:04,100 --> 00:07:06,200 [no audio] 120 00:07:06,200 --> 00:07:12,300 integer, right. Or else you have one more default function 121 00:07:12,400 --> 00:07:17,500 called 'evaluation'. Using this based on whatever the data you 122 00:07:17,500 --> 00:07:19,400 are entering your Python will identify 123 00:07:19,400 --> 00:07:20,900 what type of data you are entering. 124 00:07:21,900 --> 00:07:28,000 Let me enter some 3.4, right. Now 'print(type(x))', 125 00:07:28,000 --> 00:07:32,700 [no audio] 126 00:07:32,700 --> 00:07:37,200 float. So 'evaluation' function will automatically convert your given 127 00:07:37,200 --> 00:07:38,900 data into particular data type. 128 00:07:40,100 --> 00:07:42,300 Anyway, 'input' is only to read strings. 129 00:07:42,900 --> 00:07:45,800 But if you want to read some other values you have to use 130 00:07:46,000 --> 00:07:48,100 evaluation, right. 131 00:07:49,500 --> 00:07:51,100 Fine. Let me do one thing. 132 00:07:51,300 --> 00:07:52,500 I am going to repeat same. 133 00:07:52,500 --> 00:07:54,300 [no audio] 134 00:07:54,300 --> 00:08:01,100 'input("Enter x")'. Now I am going to enter a string called 135 00:08:01,100 --> 00:08:07,400 "hi", right. See, 'hi' is not defined. It is not taking. So whenever 136 00:08:07,400 --> 00:08:11,200 if you are using evaluation and whenever if you are providing 137 00:08:11,400 --> 00:08:16,300 string. Anyway by default input is read 138 00:08:16,300 --> 00:08:21,900 read to string. But whenever if you're using evaluation 139 00:08:21,900 --> 00:08:24,000 [no audio] 140 00:08:24,000 --> 00:08:27,500 you have to provide your string as a quotations. That's it. 141 00:08:28,900 --> 00:08:31,900 Now 'print(type(x))'. 142 00:08:31,900 --> 00:08:33,900 [no audio] 143 00:08:33,900 --> 00:08:34,900 That's it. 144 00:08:35,700 --> 00:08:41,500 Okay. So be clear. Use 'x=eval(input("Enter"' your required 145 00:08:41,500 --> 00:08:44,299 thing, and whenever if you are entering string please provide 146 00:08:44,299 --> 00:08:45,500 in terms of quotations. 147 00:08:46,100 --> 00:08:49,200 Otherwise, if you want to read only strings simply use this 148 00:08:49,200 --> 00:08:51,000 one, enter your string. 149 00:08:51,000 --> 00:08:53,700 [no audio] 150 00:08:53,700 --> 00:08:56,200 And in this case, you don't need to provide anything inside 151 00:08:56,200 --> 00:08:58,100 of your quotation. Simply you can write. 152 00:08:59,100 --> 00:09:06,000 Hi, Hello, that's it, right. Now see 'print(x)'. It will work. 153 00:09:07,200 --> 00:09:11,400 So whenever if you use evaluation function for your 'input', 154 00:09:11,400 --> 00:09:14,400 then you have to write your quotation inside of double quotes. 155 00:09:16,000 --> 00:09:20,600 Right. Okay. Now, let me design your simple calculator. 156 00:09:20,600 --> 00:09:29,900 [no audio] 157 00:09:29,900 --> 00:09:30,900 so 'input' 158 00:09:30,900 --> 00:09:33,900 [no audio] 159 00:09:33,900 --> 00:09:35,600 'evaluation' 160 00:09:36,700 --> 00:09:39,300 Okay. This is the syntax to read your numbers. 161 00:09:40,300 --> 00:09:42,900 Of course, you can also use type conversion but I am using 162 00:09:42,900 --> 00:09:46,400 this one. This is of course somewhat better, right? 163 00:09:47,900 --> 00:09:49,500 So I will show you that as well guys. 164 00:09:49,500 --> 00:09:52,200 [no audio] 165 00:09:52,200 --> 00:09:55,600 So first of all observe your script. You're having now this many 166 00:09:55,600 --> 00:10:00,200 number of lines, 1, 2, 3, 4, 5, because of first line if I run 167 00:10:00,200 --> 00:10:06,000 my Python Script with python3, right, because of first line 168 00:10:06,000 --> 00:10:07,900 it is reading something, second line 169 00:10:08,000 --> 00:10:11,900 it is reading something, then you are getting 'addition', 8, and 170 00:10:11,900 --> 00:10:17,200 type is integer for your 'a'. Even 'b' is also same, right. So same program 171 00:10:17,200 --> 00:10:19,800 you can also write in this way without any confusion. 172 00:10:20,300 --> 00:10:22,600 Let me comment first all these lines. 173 00:10:22,600 --> 00:10:32,400 [no audio] 174 00:10:32,400 --> 00:10:37,200 So what I am doing is 'a=int', but this syntax 175 00:10:37,200 --> 00:10:40,900 will work only to read integer numbers, but you may enter 176 00:10:40,900 --> 00:10:49,000 integer or, right, some float or maybe some complex sometimes, 177 00:10:49,300 --> 00:10:50,900 so that's why I don't prefer this one. 178 00:10:50,900 --> 00:10:57,100 [no audio] 179 00:10:57,100 --> 00:11:00,200 Right. So I don't prefer this actually, but just there is 180 00:11:00,200 --> 00:11:03,100 one more option in case if you want to work purely with 181 00:11:03,100 --> 00:11:06,200 integers, right. Then at that time you can take this one. 182 00:11:06,200 --> 00:11:09,400 [no audio] 183 00:11:09,400 --> 00:11:13,500 But by using evaluation automatically it will identify whatever 184 00:11:13,500 --> 00:11:16,000 the data you are entering, right? 185 00:11:16,000 --> 00:11:18,600 [no audio] 186 00:11:18,600 --> 00:11:23,200 So I'm entering 4, 5, see that 9, integer, but if I 187 00:11:23,200 --> 00:11:24,400 enter something like 188 00:11:25,600 --> 00:11:26,600 5.7, 189 00:11:28,200 --> 00:11:29,200 right, 190 00:11:29,200 --> 00:11:33,200 [no audio] 191 00:11:33,200 --> 00:11:34,600 it is not accepting because 192 00:11:34,600 --> 00:11:36,500 it need only integer values. 193 00:11:37,500 --> 00:11:42,300 Right. But if I use simply with evaluation it will work 194 00:11:42,300 --> 00:11:46,400 perfectly. So that's the reason if you want to read anything 195 00:11:46,400 --> 00:11:50,600 from command line just try to use evaluation of your input 196 00:11:50,800 --> 00:11:55,000 and at that time in case if you want to provide any strings, 197 00:11:55,000 --> 00:11:57,600 you have to write your strings in terms of quotations. 198 00:11:57,600 --> 00:11:59,700 But anyway, as of now, we are going to work with numbers 199 00:11:59,700 --> 00:12:02,800 only. Let me take 4.5 and 9., 200 00:12:02,800 --> 00:12:04,700 some 2 see the result. 201 00:12:04,800 --> 00:12:05,800 It's working perfectly. 202 00:12:07,100 --> 00:12:11,100 So that's why guys this is my suggestion, try to use because 203 00:12:11,100 --> 00:12:15,500 while going forward anyway, definitely all organizations will use 204 00:12:15,500 --> 00:12:18,700 Python 3 only that's why just try to remember in this way 205 00:12:19,100 --> 00:12:21,500 to read inputs. Okay. 206 00:12:22,900 --> 00:12:24,900 Okay guys, thank you for watching this video. 207 00:12:24,900 --> 00:12:34,800 [no audio]