LibraryRunning your first Python program

Running your first Python program

Learn about Running your first Python program as part of Python Mastery for Data Science and AI Development

Running Your First Python Program

Embarking on your Python journey begins with writing and executing your very first program. This fundamental step involves understanding how to write code, save it, and then instruct your computer to run it. We'll cover the essential tools and processes to get your Python code up and running.

Setting Up Your Environment

Before you can run Python code, you need Python installed on your system. Python can be downloaded from the official Python website. Once installed, you'll typically interact with Python through either a command-line interface (CLI) or an Integrated Development Environment (IDE).

Writing Your First Python Script

A Python script is simply a text file containing Python code, usually with a

code
.py
extension. Let's create a classic 'Hello, World!' program. Open a text editor (like VS Code, Sublime Text, or even Notepad) and type the following line:

python
400">print(400">'Hello, World!')

Save this file as

code
hello.py
in a directory you can easily access.

Executing Your Python Script

To run your script, you'll use the Python interpreter from your command line. Navigate to the directory where you saved

code
hello.py
using your terminal or command prompt. Then, type the following command and press Enter:

bash
python hello.py

If everything is set up correctly, you should see the output:

code
Hello, World!
displayed in your terminal.

Understanding the Process

The Python interpreter reads your script and executes each command sequentially.

When you run a Python script, the interpreter acts like a translator, converting your human-readable code into machine instructions. It processes each line, performing the actions specified, such as printing text to the console.

The python command invokes the Python interpreter. This interpreter then locates the specified file (hello.py in our case). It reads the file line by line. The print() function is a built-in Python function that outputs text or other data to the standard output, which is typically your terminal screen. The interpreter executes this print() command, displaying the string 'Hello, World!' as requested.

The .py extension is a convention, but it's crucial for the interpreter to identify your file as a Python script.

Using an Integrated Development Environment (IDE)

IDEs provide a more integrated experience for writing, running, and debugging code. They often include features like syntax highlighting, code completion, and built-in terminals. Popular Python IDEs include PyCharm, VS Code (with Python extensions), and Spyder.

The process of running a Python script involves several key steps: writing the code in a text file (e.g., my_script.py), saving it, opening a terminal or command prompt, navigating to the script's directory, and then executing it using the python command followed by the filename. The interpreter then reads and executes the code, producing output or performing actions.

📚

Text-based content

Library pages focus on text content

What is the standard file extension for Python scripts?

.py

What command do you typically use in the terminal to run a Python script named 'my_script.py'?

python my_script.py

Learning Resources

Python Tutorial: Your First Program(documentation)

The official Python documentation provides a clear, step-by-step guide to writing and running your first Python program, covering installation and basic execution.

Getting Started with Python(documentation)

This page on the official Python website offers a comprehensive overview of how to get started with Python, including installation and running your first scripts.

How to Run a Python File(blog)

A practical blog post from Real Python that details various methods for running Python files, including command-line execution and using IDEs.

Introduction to Python (Video Tutorial)(video)

A beginner-friendly video tutorial that walks you through setting up Python and running your first 'Hello, World!' program.

VS Code for Python Developers(documentation)

Learn how to set up Visual Studio Code, a popular IDE, for Python development and run your scripts directly within the editor.

PyCharm: Your First Python Project(documentation)

A guide to creating and running your initial Python project using PyCharm, a powerful IDE specifically designed for Python.

Python Basics: Running Code(tutorial)

An interactive tutorial that covers the 'Hello, World!' example and explains how to execute Python code in a web-based environment.

Command Line Basics(tutorial)

Essential for running Python scripts, this tutorial teaches fundamental command-line operations like navigating directories and executing commands.

What is an Interpreter?(wikipedia)

Understand the role of an interpreter in programming, which is crucial for executing Python code line by line.

Python Installation and Setup Guide(blog)

While specific to Ubuntu, this guide provides excellent foundational knowledge on installing Python and setting up a local environment, applicable to understanding the prerequisites for running code.