hey guys welcome to the next blog in python tutorial for beginners in this blog I'm going to show you how you can write your first Python script in the form of dot py file and execute it but not just by idle but I'm going to show you various different ways you can execute this Python file which you will create so let's get started so first of all I'm going to show you how you can create your Python file using idle so first of all open your idle IDE and just go to file here and you will be able to see at this option here which says new file and then you will be able to see this kind of window opens which is a Python file editor now first of all we are going to save this file so let's save this file you can save this file at any directory or any path wherever you want so for this first file I'm going to name it as hello dot py so dot py is the python extension and Hello is the name of our file so I'm going to save this file first of all and you can see it's saved at my desired location so in this file let's write some Python code so let's say I want to create a program in which I will ask the user to input three numbers and as the output I want the maximum out of these three numbers which is inputted by the user so how can I do this so as we have already seen you can define a variable in Python like this so I'm going to define a variable X is equal to and we also know that there is a inbuilt function in Python called input to take the user input and then in the double quotes so we are going to ask the user that he needs to enter the first number so enter first number and in a similar way I am going to ask the user to enter the second number and the third number let me change the variable name here so instead of X this variable will be Y and the last variable will be Z and this is the third number and this is the second number but when I write the input like this this may give us some problem the problem is input function gives us a string and we need to convert the string into integer or a float so let's convert this number into a float value so you can use another inbuilt function which is float and then this string will be converted to float so I'm going to do the same for other two numbers now in order to get the maximum numbers out of these three numbers we have seen there is our inbuilt function in Python which is called Max and we are going to use this Max function but first of all we need to print something so we will just write the max value is and then we can calculate the maximum value and print it from here so we can use this comma and then we can use this inbuilt function which is Max and then we will just pass these three values which is x y and z so will this program work let's see so first of all before doing anything you need to once again save this file okay so right now you can see there is a little Asterix symbol here in front of my file which means my file is not saved so you need to first of all save your file and then you will see this Esther X is gone that means our file is saved so let's run our code so if you want to run this Python file using your idle IDE you just need to click on the Run button here and then choose the third option which says run module so I'm going to click on run module and after some time you will be able to see this output printed on the idle IDE so it says enter the first number which we have written in our code you can see this line is executed first of all so we will enter some number I'm going to just write 50 here and then press ENTER and then it asks us to provide the second number so let's say this number is 90 and now we need to give the third number so I'm going to just give 1 0 1 here and then press ENTER and it says the max value is 1 0 1 which is correct but in real world you will not have this idol ide installed everywhere so how can I run my hello dot py file so I'm going to minimize this idol ID for now and I'm going to go to the location where I have saved this hello dot py file ok so just go to the location where you have saved your hello dot py file and now you just need to double click on this hello dot py file so I'm going to just double click on this hello dot py file and you can see as soon as I double click on hello dot py it opens this program in our terminal ok so once again I'm going to close this terminal and I'm going to double click on my hello dot py file and it opens our program in our terminal so once again I'm going to give three inputs here let's say 1 1 0 enter 55 enter 200 and enter as soon as you press ENTER this window disappears so why this window disappears because after the execution of your program this terminal immediately closes so we need to provide something in our program in order to avoid the closing of this terminal and how we can do this we can do this using our input method which is an inbuilt method right and here we can just type press any key to exit so what will happen here is after printing the reserved your terminal will wait for the user input and once the input is given by the user then only the terminal will exit so let's test our program once again but before this don't forget to save this program so I'm going to just save my program and then I'm going to open the folder where I have saved this hello dot EUI file and once again I'm going to double click on this hello dot py and I'm going to give three numbers here and then I'm going to press ENTER and it prints 99 which is the maximum out of three numbers and at last it asks us to press any key so I'm going to press any key for example Z and then press ENTER and this terminal disappears so now the question is how does this Python file is executed without compiling this file so the answer of this question I have already given in the introduction
Blog and the answer is Python is a interpreted language and not the compiled language so what happens in normal languages like Java C++ C you have a source code and this source code you need to compile in order to convert this source code into an executable file for example you have a hello dot Java file you need to compile this Java file in order to convert it into a hello dot class file which is a executable file and when you run this hello dot class file hello world or whatever program you have written will be executed now in the case of Python because it's a interpreted file you just need to create this hello dot py file and you just need to run it you don't need to compile this file and that is the difference between interpreted languages like Python and the compiled language like Java C++ or C now there is one more way using which you can run your Python file and that is by using your command prompt so I'm going to open my command prompt I'm going to right-click on this windows icon and then I'm going to open this command prompt from here so make sure that this command prompt is able to understand this Python command so first of all type Python on your command prompt and then press ENTER and if it shows you this output like python and the version of Python then python is working on your command prompt so this command prompt is Windows command prompt and not the Python shell which we have installed so this is our Windows command prompt right now let me make this terminal little bit bigger so we will be able to see what I'm typing so I'm going to change the font size to let's say 24 here so now we will be able to see what's happening you can just right quit and then these two parentheses and then press Enter and you will come out of this command prompt so you need to come out of your command prompt in order to execute your python file now in here what you need to do is you need to copy the path of your Python file so I'm going to right click here and then go to the properties and then in the securities I'm going to just copy full a part of this Python file up to dot Python okay so I have copied full path of this Python file and now what I need to do is I need to just write Python here ok so just write Python and then paste the path which you have copied so I'm going to just paste the path which I have copied and then press Enter and you can see our program is executed so I'm going to just give quickly three numbers and it gives us the maximum of these three numbers now I will just type any letter here and then press Enter and I will come out of this terminal there is one more way of executing your file and this is you need to just give this command which is CD and then your a directory part so up to your directory you just need to copy this path from here okay so you just need to copy this path and then after CD you just need to give space and then paste this path without your hello dot py file okay so I haven't given the hello dot py file name here and now press Enter which will change your directory to your Python directory so now we are inside our Python directory from here you can directly give Python and the name of your file which is hello dot P Y and then press Enter and once again the program opens so we can just give three inputs here and the result is shown once again I'm going to just type any key or any letter here for example R and then press enter and now I'm out of this program so this is how you can run your python script on any terminal you don't need any IDE or any other special software in order to run your python script you can run it on your terminal or directly click on the python file so I hope you have learned something new this time I will see you in the next blog so bye for now
Post a Comment