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 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 (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!


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

        Reply
          1. Andries

            Hi Guys, also new to Python.

            Admin is stating that the ‘python hello.py’ 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 “hello.py” and “python hello.py”

            hello.py
            File “C:…hello.py”, 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 hello.py
            ‘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 hello.py file was

            print “Hello, World”

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

  1. juan

    I’m confused by this:
    Lets create a file named hello.py 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?

    Reply
    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 hello.py # this will create a new file in you current working directory

      Reply
    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 ‘hello.py’.

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

      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.

    Reply
  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
    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: 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.

    hello.py

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

    Where am I going wrong here?

    Reply
          1. admin Post author

            use pastebin.com to share your code .

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

    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

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

    Thanks.

    Reply
      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 hello.py” in my Python Terminal I get a syntax error. Am I missing something ? I have already created a document called “hello.py” with the writing in it. Please help when you can

        Thanks.

        Reply
  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 hello.py (with Sublime Text) in my desktop (Mac OS). The file’s directory is :(/Users/Myname/Desktop/hello.py)

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

    I am confused, please help. Thanks!:)

    Reply
  6. Tuan

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

    Reply

Leave a Reply

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