Tag: Lesson 2: Writing Your First Python Program

  • Lesson 2: Writing Your First Python Program

    In this lesson, we will introduce the fundamentals of Python syntax and structure, walk through writing a simple Python script, and show you how to run your Python code.

    1. Understanding Syntax and Structure

    Before we dive into writing your first Python program, let’s understand some basic concepts related to Python’s syntax and structure:

    What is Syntax?

    • Syntaxrefers to the set of rules that defines the structure of a programming language. In Python, this determines how you write commands, how variables are declared, and how various operations are performed.

    Python is designed to be readable and intuitive, making it easier for new programmers to learn.

    Key Points About Python Syntax:

    • Case sensitivity: Python is case-sensitive, meaning that helloand Hello would be treated as two different identifiers.
    • Indentation: Python relies on indentation (spaces or tabs) to define code blocks, unlike other programming languages that use curly braces {}or keywords like begin and end.
      • Code inside a block (e.g., within loops, functions, classes) must be indented at the same level. This makes Python code clean and easy to follow.

    Example:

    python
    print(“This is indented”)
    • Comments: Comments are used to explain the code and make it more readable. In Python, comments are indicated by a hash (#) symbol.
      • Anything following #in a line will be ignored by Python during execution.

    Example:

    python
    # This is a comment

    print(“Hello, World!”)  # This prints Hello, World! to the screen

    2. Writing a Basic Python Script (Hello World)

    Let’s start by writing your first Python program. The classic “Hello, World!” program is typically the first program that anyone writes in a new programming language.

    Step-by-Step Process:

    1. Open your IDE or Text Editor:
      • If you’re using an IDE like VS Code, PyCharm, or Thonny, create a new Python file with the extension .py(e.g., py).
      • If you’re using a simple text editor, save the file as py.
    2. Write the Code:
      • The print()function in Python is used to display output to the console. In this case, we want to print the text “Hello, World!”.
      • Here’s the simplest Python code to achieve this:
    python
    print(“Hello, World!”)
    1. Explanation of the Code:
      • print()is a built-in Python function that outputs whatever is inside the parentheses to the console or terminal.
      • The text “Hello, World!”is a string, which is a sequence of characters enclosed in quotation marks.
      • When this script runs, the Python interpreter reads the print()function and displays the string on the screen.

    Example:

    python
    # Hello World Program

    print(“Hello, World!”)

    3. Running Python Code

    Once you’ve written your Python script, it’s time to run the code and see the output. Running Python code can be done in several ways depending on your setup. Here’s how to execute the script:

    Running Python Code in an IDE (Integrated Development Environment):
    1. In VS Code or PyCharm:
      • Open the .pyfile you just created.
      • In VS Code, press Ctrl+F5or click the Run button (a green triangle) to execute the script.
      • In PyCharm, click the Runbutton (usually at the top of the window) or press Shift+F10.
    2. In Thonny:
      • Open Thonny IDE.
      • Write your code in the editor.
      • Press the Runbutton at the top of the window or press F5 to execute the code.
    Running Python Code from the Command Line (Terminal/Command Prompt):

    For Windows:

    1. Open Command Prompt.
    2. Navigate to the folder where your Python file is saved using the cd(change directory) command.

    Example:

    bash
    cd C:\Users\YourUsername\Documents\PythonPrograms

    Run the Python script by typing the following command:

    bash
    python hello_world.py

    For macOS/Linux:

    1. Open Terminal.
    2. Navigate to the directory where your Python file is located.

    Example:

    bash
    cd /Users/YourUsername/Documents/PythonPrograms

    Run the script using:

    bash
    python3 hello_world.py
    Expected Output:

    After running the script, you should see the following printed on the screen:

    output
    Hello, World!

    4. Modifying the Script

    Once you’ve successfully written and run the basic script, try modifying it to make your own variations! Here are a few exercises:

    Change the Text: Modify the string to print something else, like:

    python
    print(“Welcome to Python Programming!”)

    Add More Print Statements: You can have multiple print statements to display different outputs.

    python
    print(“Hello, World!”)

    print(“Python is fun!”)

    Use Variables: Variables are used to store data. You can use a variable in a print() statement.

    python
    message = “Hello, Python!”

    print(message)

    Key Concepts Covered in This Lesson:

    1. Syntax and Structure: Understanding the importance of indentation and how Python handles code blocks.
    2. First Python Script: Writing and executing a simple Python script (Hello World).
    3. Running Python Code: How to run Python code using different methods (IDE, terminal/command prompt).
    4. Modifying Scripts: Exploring basic variations to modify and experiment with Python code.
  • Lesson 2: Writing Your First Python Program

    In this lesson, we will introduce the fundamentals of Python syntax and structure, walk through writing a simple Python script, and show you how to run your Python code.

    1. Understanding Syntax and Structure

    Before we dive into writing your first Python program, let’s understand some basic concepts related to Python’s syntax and structure:

    What is Syntax?

    • Syntaxrefers to the set of rules that defines the structure of a programming language. In Python, this determines how you write commands, how variables are declared, and how various operations are performed.

    Python is designed to be readable and intuitive, making it easier for new programmers to learn.

    Key Points About Python Syntax:

    • Case sensitivity: Python is case-sensitive, meaning that helloand Hello would be treated as two different identifiers.
    • Indentation: Python relies on indentation (spaces or tabs) to define code blocks, unlike other programming languages that use curly braces {}or keywords like begin and end.
      • Code inside a block (e.g., within loops, functions, classes) must be indented at the same level. This makes Python code clean and easy to follow.

    Example:

    python

    CopyEdit

    if True:

    print(“This is indented”)

     

    • Comments: Comments are used to explain the code and make it more readable. In Python, comments are indicated by a hash (#) symbol.
      • Anything following #in a line will be ignored by Python during execution.

    Example:

    python

    CopyEdit

    # This is a comment

    print(“Hello, World!”)  # This prints Hello, World! to the screen

    2. Writing a Basic Python Script (Hello World)

    Let’s start by writing your first Python program. The classic “Hello, World!” program is typically the first program that anyone writes in a new programming language.

    Step-by-Step Process:

    1. Open your IDE or Text Editor:
      • If you’re using an IDE like VS Code, PyCharm, or Thonny, create a new Python file with the extension .py(e.g., py).
      • If you’re using a simple text editor, save the file as py.
    2. Write the Code:
      • The print()function in Python is used to display output to the console. In this case, we want to print the text “Hello, World!”.
      • Here’s the simplest Python code to achieve this:

    python
    CopyEdit
    print(“Hello, World!”)

    1. Explanation of the Code:
      • print()is a built-in Python function that outputs whatever is inside the parentheses to the console or terminal.
      • The text “Hello, World!”is a string, which is a sequence of characters enclosed in quotation marks.
      • When this script runs, the Python interpreter reads the print()function and displays the string on the screen.

    Example:

    python

    CopyEdit

    # Hello World Program

    print(“Hello, World!”)

     

    3. Running Python Code

    Once you’ve written your Python script, it’s time to run the code and see the output. Running Python code can be done in several ways depending on your setup. Here’s how to execute the script:

    Running Python Code in an IDE (Integrated Development Environment):
    1. In VS Code or PyCharm:
      • Open the .pyfile you just created.
      • In VS Code, press Ctrl+F5or click the Run button (a green triangle) to execute the script.
      • In PyCharm, click the Runbutton (usually at the top of the window) or press Shift+F10.
    2. In Thonny:
      • Open Thonny IDE.
      • Write your code in the editor.
      • Press the Runbutton at the top of the window or press F5 to execute the code.
    Running Python Code from the Command Line (Terminal/Command Prompt):

    For Windows:

    1. Open Command Prompt.
    2. Navigate to the folder where your Python file is saved using the cd(change directory) command.

    Example:
    bash
    CopyEdit
    cd C:\Users\YourUsername\Documents\PythonPrograms

    Run the Python script by typing the following command:
    bash
    CopyEdit
    python hello_world.py

    For macOS/Linux:

    1. Open Terminal.
    2. Navigate to the directory where your Python file is located.

    Example:
    bash
    CopyEdit
    cd /Users/YourUsername/Documents/PythonPrograms

    Run the script using:
    bash
    CopyEdit
    python3 hello_world.py

    Expected Output:

    After running the script, you should see the following printed on the screen:

    CopyEdit

    Hello, World!

     

    4. Modifying the Script

    Once you’ve successfully written and run the basic script, try modifying it to make your own variations! Here are a few exercises:

    Change the Text: Modify the string to print something else, like:
    python
    CopyEdit
    print(“Welcome to Python Programming!”)

    Add More Print Statements: You can have multiple print statements to display different outputs.
    python
    CopyEdit
    print(“Hello, World!”)

    print(“Python is fun!”)

    Use Variables: Variables are used to store data. You can use a variable in a print() statement.
    python
    CopyEdit
    message = “Hello, Python!”

    print(message)

    Key Concepts Covered in This Lesson:

    1. Syntax and Structure: Understanding the importance of indentation and how Python handles code blocks.
    2. First Python Script: Writing and executing a simple Python script (Hello World).
    3. Running Python Code: How to run Python code using different methods (IDE, terminal/command prompt).
    4. Modifying Scripts: Exploring basic variations to modify and experiment with Python code.