From the previous text we started to learn Python. From this text I’m going to show you how to execute a Python program in different ways. I’m doing all these practicals on UNIX based OS.
- You can use the python command on terminal. Then it will show the >>> where you can start executing any python command.
Then in front of >>> you can type the command and hit enter to execute it.
- The second way you can try is, save the code in a separate file and save it using the ‘.py ‘extension. Then you can execute it using the ‘python path-to-the-file.py‘ as well.
- The third way is, you can use an IDE such as IDLE or pycharm. I’m using the pycharm IDE.
These are some ways how you can execute a python program and get the output. From here on I’m using the pycharm IDE for examples.
- You can use redirection in python as well on the terminal.
- Also we can use piping on the terminal with python as well.
WHAT IS import?
When you create a python source code, you can use the variables, methods created in this source code in another source code as well. For that you can use ‘import path-to-source-code‘ command on top of the new source code file. This will import the entire source code. So when we want to use a specific variable, we have give it as ‘imported-file-name.variable-name‘.
There is another command similar to import known as ‘from file-name import variable-names‘. If you try to import in this way it will import only the given things. Also after importing, you can directly use them without giving the ‘file-name.variable-name‘ way as we did earlier. Just ‘variable-name‘ is enough in this way.
I hope you have understood hoe to execute a python source code in different ways, the use of piping and redirection with python and how to import things in python. Hope to see you soon with another interesting topic. Thank You!