Running python programs

Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!

You can run python programs in two ways, first by typing commands directly in python shell or run program stored in a file. But most of the time you want to run programs stored in a file.

Lets create a file named hello.py  in your documents directory  i.e C:\Users\YourUserName\Documents using notepad (or any other text editor of your choice) , remember python files have ‘.py’ extension, then write the following code in the file.

In python we use print  function to display string to the console. It can accept more than one arguments. When two or more arguments are passed, print  function displays each argument separated by space.

Expected output

Now open terminal and change current working directory to C:\Users\YourUserName\Documents  using cd  command.

CHANGE CURRENT WORKING DIRECTORY

To run the program type the following command.

RUNNING HELLO WORLD PROGRAM
If everything goes well, you will get the following output.

Getting Help

Sooner or later while using python you will come across a situation when you want to know more about some method or functions. To help you Python has help()  function, here is how to use it.

Syntax:

To find information about class: help(class_name)

To find more about method belong to class: help(class_name.method_name)

Suppose you want to know more about int  class, go to Python shell and type the following command.

as you can see help()  function spits out entire int  class with all the methods, it also contains description where needed.

Now suppose you want to know arguments required for index()  method of str  class, to find out you need to type the following command in the python shell.

In the next post we will learn about data types and variables in python.

Other Tutorials

This site generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!

Leave a Reply

25 Comments on "Running python programs"

Notify of
avatar

Sort by:   newest | oldest | most voted
Alex
Guest
Alex
8 months 7 days ago

I’m confused on this. I’ve done what is has said, yet I’m only getting errors. Is it that I am using a new version? Please Help.

Thanks.

Vasya
Guest
Vasya
11 months 9 days ago

how to change directory? in my shell it is showing just >>> not C:\User\X….

Carl
Guest
Carl
1 year 4 months ago
I’ve studied several langauges, and everyone always throws in file manipulation in the prompt at the very beginning, and I ALWAYS struggle with it. Typing in exact windows filepaths correctly, in proper format is not easy to do. I suggest using: import os os.getcwd() This will tell you where you current working directory is, and just create a shortcut to that directory on your desktop so it’s handy. That being said, I still can’t get my hello world program to run without definging a hello function in IDLE and calling it form there. I get syntax errors: python hello.py SyntaxError:… Read more »
fakename
Guest
fakename
1 year 8 months ago

I am trying to learn python on windows. I will find a different guide because this guide assumes we know what the author means when the audience doesn’t. I spent 30 mins trying to find out how to make a “file” in windows through the cmd prompt. If the first “hello world” Tutorial is busted then good luck with the rest of this guys work.

juan
Guest
juan
1 year 9 months ago

I’m confused by this:
Lets create a file named hello.py in your documents directory i.e C:\Users\YourUserName\Documents , remember python files have ‘.py’ extension, then write the following code in the file.

Is this all done in the terminal & how do I create a file?

Uday
Guest
Uday
1 year 7 months ago

Step 1: Just Open a text editor say ‘Notepad’ or ‘Notepad++’ in a separate window.
Step 2: Create a new file and write something into it, say ‘ Hey! How are you!’. Step 3: Now save this file BUT while naming the filename just delete everything and name it ‘hello.py’.

The same can be done in the reverse way. Hope it helps! If not please reply.

wpDiscuz