Running python programs

(Sponsors) 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  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.


To run the program type the following command.

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.


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 (Sponsors)

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!

32 thoughts on “Running python programs

      1. Ed

        Outside the shell? Where should I put the command? I can’t think of any location except the shell.. CMD and in the program itself wont work..

          1. Andries

            Hi Guys, also new to Python.

            Admin is stating that the ‘python’ must be run in cmd.exe on Windows (Standard Command Prompt). When you do this, you must first reach the directory where the file is stored using cd command. If you are in the Python shell, you will get a syntax error… I figured it out after digging. 🙂 Happy learning…

          2. Carl

            Well that explains the “python” cammand in front of the file name. I’ve been playing in the cammand prompt for a few now.

            I’ve moved to the appropriate directory. But I’m still getting errors when I imput the cammands “” and “python”

            File “C:…”, line 1
            Python 2.7.111 (v2.7….yada yada yada…
            it (Intel) on win 32
            SyntaxError: invalid syntax

            and when I put python before it

            ‘python’ is not reocognized as an internal or external cammand, operable program or batch file

            So do we need to use the python keyword/cammand?
            What else might I be doing wrong here?

          3. Carl

            Eureka! I got it to work without the python keyword/cammand.

            Somehow my new file got a header, the same one that appears at the top of the shell, my IDLE.

            Once I removed the excess text and the only code/text in my file was

            print “Hello, World”

            I could summon it from the windows cammand prompt and get the desired
            “Hello, World”

  1. juan

    I’m confused by this:
    Lets create a file named in your documents directory i.e C:UsersYourUserNameDocuments , 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?

    1. admin Post author

      Use notepad to create file, if you are in windows as save it with .py extension, If you are on linux use

      touch # this will create a new file in you current working directory

    2. Uday

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

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

  2. fakename

    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.

  3. Carl

    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

    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:
    SyntaxError: invalid syntax

    It doesn’t seem to recognize python as a keyword. So I figured you just meant python (as in code in IDLE). Which lead me to try.

    Traceback (most recent call last):
    File “”, line 1, in
    NameError: name ‘hello’ is not defined

    Where am I going wrong here?

          1. admin Post author

            use to share your code .

            If you are on windows use snipping tool to take a snapshot of console and share image on

    1. admin Post author

      To change your directory use type cd command followed by pathname for e.g lets say you want to change your current working directory to C:UserXDocuments to do this type the following

      cd C:UserXDocuments

  4. Alex

    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.


      1. Alex

        When I type Python into my Command Prompt it says “‘Python’ is not recognized as an internal or external command, operable program or batch file.” I changed my directory to C:UsersPythoDocuments in my Command Prompt but it doesn’t send over to my Python Terminal. When I type in “python” in my Python Terminal I get a syntax error. Am I missing something ? I have already created a document called “” with the writing in it. Please help when you can


      2. vinod

        This is the problem we are getting…..
        i have created a file in notepad and saved as, the cmd promp showing the error whatever i mentioned below (cmd window displayed)

        Microsoft Windows [Version 10.0.17763.615]
        (c) 2018 Microsoft Corporation. All rights reserved.

        C:\Users\vinod>cd documents

        ‘python’ is not recognized as an internal or external command,
        operable program or batch file.


        1. PythonGuru

          Looks like you forgot to add python.exe to the PATH envionment variable.

          Copy the python.exe path and append it to the PATH environment variable.

  5. Nana

    Hi, thanks for the tutorial. I am Just a very very beginner who wants to learn Python. But I have so many troubles understanding this “Now open terminal and change current working directory to C:\Users\YourUserName\Documents using cd command.”

    I have created a file named (with Sublime Text) in my desktop (Mac OS). The file’s directory is :(/Users/Myname/Desktop/

    And what should I do next? I open the terminal and change what working directory to the file?

    I am confused, please help. Thanks!:)

  6. Tuan

    I have created a file named ”” and did same thing you instructed, but when i typed ” python “, a new CMD window appeared and it disappeared immediately. Is there any problem

  7. Siddh

    Nice! What I learned to do was run programs by changing directories until the current working directory then using “py ./Insert_Program_Name”

  8. Ravinder Murthy

    The basic trouble with all you tutors is you are not trying to comprehend the difficulty the user is facing to run python programme. He does not have the python programme. Start from ther. Download the Python Compiler or interpreter and then start with notepad environment variables etc; none of asked him if he has the python executable. He is very clearly mentioning that it gives an error “not an recognised internal command ” etc; Please read, understand the query and then explain. Thanks ad regards.

  9. Doug

    Question. I’m running a simple Python program to print an alphanumeric input. When using the online Python 3 editor at to run this code:
    1. ET = input(“What is the engine type? “)
    2. print(ET)
    the alphanumeric code entered, C6, gets printed to the screen as expected.

    Why am I getting a Name Error when I run the same code in Terminal on Mac OS (using either Sublime Text 3 or Geany), saying that that “C6” is not defined? I’m at a loss. Thanks!!


Leave a Reply

Your email address will not be published. Required fields are marked *