Chapter 1: Python Setup Practical Exercises

Hands-on exercises to test your Python installation and basic understanding

Exercise 1

Installation Verification

Verify that Python is correctly installed on your system.

  • Open terminal/command prompt
  • Run python --version or python3 --version
  • Check that it returns a version number (e.g., Python 3.9.0)
  • Hint

    If you get an error, you may need to add Python to your system PATH during installation.

    Exercise 2

    Hello World Program

    Create your first Python program that prints "Hello, World!" to the console.

    print("Hello, World!")
  • Create a file named hello.py
  • Write the print statement
  • Run the program using Python
  • Exercise 3

    Python as a Calculator

    Use the Python interpreter interactively to perform calculations.

    # Start Python interpreter
    $ python
    >>> 5 + 3
    8
    >>> 10 / 3
    3.3333333333333335
    >>> 2 ** 4
    16
  • Start Python interpreter
  • Try basic arithmetic operations
  • Exit with exit() or Ctrl+Z
  • Exercise 4

    Create a Simple Script

    Create a Python script that asks for your name and greets you personally.

    name = input("What is your name? ")
    print("Hello, " + name + "! Welcome to Python.")
  • Create greet.py file
  • Use input() function
  • Run and test with your name
  • Exercise 5

    IDE Setup

    Install and configure an IDE (Integrated Development Environment) for Python.

  • Choose an IDE (VS Code, PyCharm, or IDLE)
  • Install the IDE
  • Create and run a simple Python file
  • Install Python extension if needed
  • Hint

    VS Code is a popular free option. Install the Python extension for enhanced features.

    Exercise 6

    Project Structure

    Create a well-organized directory structure for your Python projects.

    python_projects/
      ├── chapter1/
      │  ├── hello.py
      │  └── greet.py
      └── README.txt
  • Create a main projects folder
  • Add a subfolder for Chapter 1 exercises
  • Move your existing files there
  • Add a README file
  • Exercise 7

    Help System

    Learn how to use Python's built-in help system.

    # Start Python interpreter
    >>> help(print)
    >>> help(str)
    # Press 'q' to exit help
  • Start Python interpreter
  • Try help() on print function
  • Try help() on str type
  • Exit help with 'q'
  • Exercise 8

    Error Exploration

    Intentionally create errors to understand common Python error messages.

    # Try these in Python interpreter
    >>> print("Hello"
    >>> 10 / 0
    >>> variable_not_defined
  • Try code with syntax error
  • Try division by zero
  • Try using undefined variable
  • Read and understand error messages
  • Exercise 9

    Run from Command Line

    Run a Python script from the terminal or command prompt.

    # Navigate to your script directory
    $ cd path/to/your/script

    # Run the script
    $ python hello.py
  • Open terminal/command prompt
  • Navigate to script directory
  • Run script with python command
  • Exercise 10

    Multi-line Program

    Create a program that uses multiple lines of code and variables.

    # Calculate area of a circle
    radius = 5
    pi = 3.14159
    area = pi * radius ** 2
    print("The area of the circle is:", area)
  • Create circle_area.py
  • Define variables for radius and pi
  • Calculate area
  • Print the result