• Discover
  • Collections
  • Board
  • Create
  • Profile
  • Settings
Paths

Lesson 1.3: Your First Python Program: "Hello, World!" and Basic Syntax

Lesson 1.3: Your First Python Program: "Hello, World!" and Basic Syntax

3 min read

Now that your environment is set up, it's time to write your first Python program. The traditional 'Hello, World!' program is a great starting point to understand basic Python syntax.

Writing 'Hello, World!'

  1. Open your editor: Launch VS Code, PyCharm, or your chosen code editor.

  2. Create a new file: Save it as hello_world.py. The .py extension is crucial as it tells the operating system and Python interpreter that this is a Python script.

  3. Write the code: Type the following single line into your file:

    python
    print("Hello, World!")
  4. Save the file.

Running Your Program

  • Using a Terminal: Open a terminal or command prompt, navigate to the directory where you saved hello_world.py, and run it using:

    bash
    python hello_world.py # or if 'python' refers to python2 on your system: python3 hello_world.py
  • Using VS Code: If you have the Python extension installed, you can usually click the 'Run Python File' button in the top-right corner or right-click in the editor and select 'Run Python File in Terminal'.

  • Using PyCharm: Right-click on the hello_world.py file in the project explorer and select 'Run 'hello_world''.

Understanding the Code

  • print(): This is a built-in Python function. Functions are blocks of organized, reusable code that perform a single, related action. The print() function's job is to display output to the console.
  • "Hello, World!": This is a string literal. A string is a sequence of characters, and in Python, strings are enclosed in either single quotes (') or double quotes ("). In this case, 'Hello, World!' is the argument passed to the print() function.

Basic Python Syntax Rules

  • Indentation Matters: Python uses indentation (whitespace at the beginning of a line) to define code blocks, unlike many languages that use curly braces {}. Consistent indentation (usually 4 spaces) is critical for Python code to run correctly.

    python
    if True: print("This line is indented by 4 spaces") else: print("This line is also indented by 4 spaces") # Error if not indented correctly
  • No Semicolons: Unlike C++ or Java, Python statements do not require semicolons at the end. Each line is typically a new statement.

  • Comments: Comments are non-executable lines in your code, used to explain what the code does. They are vital for readability and maintainability.

    • Single-line comments: Start with a hash symbol (#).
      python
      # This is a single-line comment print("Hello") # This prints 'Hello'
    • Multi-line comments (docstrings): While Python doesn't have a dedicated multi-line comment syntax like /* ... */, you can use triple quotes (""" or ''') for multi-line strings that aren't assigned to a variable. These are often used for documentation strings (docstrings) for functions, classes, and modules.
      python
      """This is a multi-line comment. It can span several lines. It's often used as a docstring. """ print("World")
  • Case Sensitivity: Python is case-sensitive. myVariable is different from myvariable.

By following these basic syntax rules, you're well on your way to writing understandable and functional Python code. The print() function will be your best friend for debugging and understanding program flow as you learn.

Key Takeaways:

  • Python files use the .py extension.
  • The print() function outputs text to the console.
  • Python relies on indentation to define code blocks.
  • Use # for single-line comments and """ for docstrings/multi-line comments.
  • Python is case-sensitive.
End of lesson
👏Well done!
Previous Lesson
Lesson 1.2: Setting Up Your Python Environment
Next Lesson
Quiz: Module 1: Getting Started with Python

Course Content

0% Complete0/22 Lessons

Lesson 1.1: What is Python? History, Features, and Applications

Lesson 1.2: Setting Up Your Python Environment

Lesson 1.3: Your First Python Program: "Hello, World!" and Basic Syntax

Quiz

Course Content

0% Complete0/22 Lessons

Lesson 1.1: What is Python? History, Features, and Applications

Lesson 1.2: Setting Up Your Python Environment

Lesson 1.3: Your First Python Program: "Hello, World!" and Basic Syntax

Quiz