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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones Build AI Agents That Are Ready for Production
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
Build AI Agents That Are Ready for Production

Just dropped: New 2026 “Cloud-Native Foundations” Trend Report. See how teams are tackling complexity, cost & reliability.

AI can investigate. Engineers still decide. See how both work together across incident response in this DZone + Datadog webinar on Oct. 29.

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

  • Part 1: Building Governed MCP Tool Services With Quarkus LangChain4j and Goose
  • Event-Driven AI Systems With Kafka and Autonomous Agents
  • Inside terraform-provider-archive: A Memory Pattern From 2016 That Scales With Your Lambdas
  • From ETL, ELT, and EtLT to Agent: What Is Changing in Enterprise Data Engineering?
  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 user avatar
Gaurav Gahlot
·
Oct. 19, 18 · Tutorial
Likes (9)
Comment
Save
Tweet
Share
37.8K Views

Join the DZone community and get the full member experience.

Join For Free

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 "", 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

The likes didn't load as expected. Please refresh the page and try again.

  • 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