DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Instant Integrations With API and Logic Automation
  • Iptables Basic Commands for Novice
  • Traceroute Command in Python and How to Read a Traceroute
  • Automate Sign-In To Any Website Using Selenium Web-Driver

Trending

  • The Request Timed Out, But the Payment Succeeded: Building Retry-Safe Mobile APIs
  • Android Application Security: Android Application Architecture
  • Multi-Agent Orchestration on AWS With AgentCore Runtime and A2A
  • How Docker Is Becoming an AI Development Platform

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. Running Your First Python Script

Running Your First Python Script

Python is a powerful and popular language for data science and big data. In this article, we show you how to get started using the language.

By 
Gaurav Gahlot
·
Oct. 19, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
37.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Since you are here, I'll assume that you have a working Python setup along with a working Python interpreter, and are ready to run your first Python script. If not, check out my previous articles, where I walk you through each step to set up your Python environment. Here are the links to those articles:

  • Setup Python environment on Linux based systems
  • Setup Python environment on Windows

In this article, we will be talking about:

  • using a Python interpreter to run our code.
  • executing Python script contained in a file from the command line.

The Python Interpreter

The Python interpreter is the most commonly used way to get started with Python for beginners. The Python interpreter is a Read-Eval-Print-Loop (REPL) that simply takes commands, evaluates them, and prints the output.

  • Reads the command.
  • Evaluates and executes the command.
  • Prints the output.
  • Loops back and repeat the process.

The interpreter continues until we instruct it to terminate using the exit() or quit() command.

Starting the Interpreter

The easiest way to start the interpreter is to open up a terminal and use the interpreter from the command line. You can open the terminal:

  • on Windows, search for Command Prompt or Powershell.
  • on Linux or Mac, search for Terminal.

Once the terminal is open, you can start the interpreter by typing python and hitting return. If the paths have been set by the Python install process, you should see a response from the Python interpreter. Given below is an example from Ubuntu terminal:

Image title

Python Interpreter in Ubuntu

The >>> shows that we are interacting with the Python interpreter. If you don't see that, you may want to revisit the installation steps given in the above links.

Hello, World!

Now that we are all set, let's continue following the long-standing tradition of writing a program that prints Hello, World! on the console. In its simplest form, the following Python steps display Hello, World!:

  • We need to ensure that we see the >>> prompt.
  • Type the command print("Hello, World!")
  • Hit return/enter key.

The output from the interpreter is shown in the next line. Note that the output line does not begin with >>>.

>>> print("Hello, World!")
Hello, World!

It's important to remember that Python does take indentation into account while interpreting a command. So if we have some extra space before the print command, we will get an error as shown below:

>>>   print("Hello, World!")
  File "<stdin>", line 1
    print("Hello, World!")
    ^
IndentationError: unexpected indent

Running Python Script From Command Line

Using the interpreter is great for quick exploration of features and functionality. However, when we work on a larger problem, we write much more code and we do not want to type that code over and over again. This is where we want to create a script file.

A Python script is a reusable set of code which is essentially a Python program — a sequence of Python instructions — contained in a file. You can run the program by specifying the name of the script file to the interpreter. It's time to create our first script file.

On your system, open up your favorite text editor, create a new file called hello.py, and add the following code:

print("Hello, World!")

Start the terminal and navigate to the directory where you had saved the script file. We can now execute the script by simply specifying the filename as a command line argument to the Python interpreter: python hello.py.

Image title

Running a Python Script

An interesting fact is that our script file does not require an extension. The Python interpreter will run the script no matter what it is called. However, giving Python script files an extension is a useful convention as it makes them more easily identifiable.

Summary

The Python interpreter is very useful, whether we want some help or we want to run some Python script. However, to increase code reusability and to solve a large problem we are required to write a Python script. This article was to just get you started with the Python interpreter and Python scripts. In the next article, we will see how helpful the Python interpreter is and how we can effectively use it.

Python (language) Command (computing)

Opinions expressed by DZone contributors are their own.

Related

  • Instant Integrations With API and Logic Automation
  • Iptables Basic Commands for Novice
  • Traceroute Command in Python and How to Read a Traceroute
  • Automate Sign-In To Any Website Using Selenium Web-Driver

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook