Beginning Python for Non-Programmers
  • Cover
  • About the Author
  • Introduction to Programming
    • What is Programming?
    • What Programmers Do
    • The Mind of a Programmer
    • How Does a Programmer Think?
  • Programming Languages
    • Python
    • HTML / CSS
    • JavaScript / TypeScript
    • C, C++, C#
    • Java
    • SQL
  • Introduction to Python
    • What is Python?
    • Types of Python Programs
    • Core Tools for Python Programming
      • Python Interpreter
      • Jupyter Notebooks
      • Visual Studio Code
      • Python Playground
      • Pycharm Community Edition
  • Installing Python
    • Step-by-step guide for Windows Users
    • Step-by-step guide for Mac Users
  • Installing Visual Studio Code
    • Step-by-step guide for Windows Users
    • Step-by-step guide for Mac Users
  • Writing and Running Your First Python Program
  • Basic Concepts of Python Programming
  • Control Flow in Python
  • Functions and Reusability
  • Debugging and Problem Solving
  • Exploring More Python Concepts
  • Adding Python Packages with pip
  • Python Web Frameworks
  • Resources for Continued Learning
    • CodeAcademy.com
    • Python.org
    • SoloLearn.com
    • coursera.com
    • udemy.com
    • youtube.com
    • Code With Mosh
    • GameDev.tv
  • Staying Motivated and Practicing Regularly
  • Appendices
    • Beginner Project Ideas for Learning
    • Python Cheat Sheet
    • Frequently Asked Questions for Beginners
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

Writing and Running Your First Python Program

Now that you have Python and Visual Studio Code installed, it’s time to write and run your first Python program. Writing code may seem intimidating at first, but don’t worry—Python is designed to be beginner-friendly. In this section, you’ll create a simple program that prints a message to the screen, commonly known as the "Hello, World!" program.

Step 1: Create a New Python File

  1. Open Visual Studio Code.

  2. From the menu at the top, select File > New File to create a new file.

  3. Save the file by selecting File > Save As. Name the file hello.py and make sure to save it with the .py extension, which indicates that it is a Python file.

Step 2: Write the "Hello, World!" Program

In the hello.py file, type the following code:

print("Hello, World!")

Step 3: Run the Python Program

  1. Once you’ve written the code, you can run the program directly from within VS Code.

  2. First, make sure that the Python interpreter is selected. If it’s not already selected, press Ctrl+Shift+P (or Cmd+Shift+P on macOS), type Python: Select Interpreter, and choose your installed version of Python.

  3. Now, you can run the program by opening the integrated terminal. Go to Terminal > New Terminal from the top menu.

  4. In the terminal window that opens at the bottom of VS Code, navigate to the folder where you saved hello.py. If the file is saved on your Desktop, you can type:

       cd Desktop
  5. Finally, run your Python program by typing the following command in the terminal:

       python hello.py
  6. You should see the message Hello, World! displayed in the terminal. Congratulations! You’ve just written and executed your first Python program.

Step 4: Understanding the Output

When you run the program, Python executes the print() function, which outputs whatever is inside the parentheses. In this case, it prints the text "Hello, World!". This simple example demonstrates how Python interprets and runs code.

Step 5: Experimenting with Python

Now that you’ve successfully run your first program, feel free to experiment! Change the message inside the print() function to something different, like:

print("Welcome to Python!")

Save the file, and run the program again using the same steps as before. You should now see the new message displayed in the terminal. By modifying small parts of the code and running it, you’ll start to understand how Python works and how to make the computer perform different tasks. This is just the beginning of what you can create with Python, and as you continue, you’ll learn to build more complex and interesting programs.

Some content generated with AI

PreviousStep-by-step guide for Mac UsersNextBasic Concepts of Python Programming

Last updated 9 months ago

Was this helpful?