EXECUTE PYTHON PROGRAMS

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.

1

Then in front of >>> you can type the command and hit enter to execute it.

1

  • 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.

2

4

  • The third way is, you can use an IDE such as IDLE or pycharm. I’m using the pycharm IDE.

3

5

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.

SOME TIPS?

  • You can use redirection in python as well on the terminal.

6

7

8.png

  • Also we can use piping on the terminal with python as well.

9.png

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‘.

10

11

12

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.

13

14

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!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s