Code icon

The App is Under a Quick Maintenance

We apologize for the inconvenience. Please come back later

Menu iconMenu iconData Analysis Foundations with Python
Data Analysis Foundations with Python

Chapter 3: Basic Python Programming

3.4 Practical Exercises Chapter 3: Basic Python Programming

Exercise 1: Your First Script

  1. Create a Python script named print_even_numbers.py.
  2. Make the script print even numbers from 2 to 20.
    # print_even_numbers.py
    for i in range(2, 21, 2):
        print(i)
  3. Run your script from the terminal to verify it works.

Exercise 2: Command-Line Arguments

  1. Create a Python script named greet_user.py.
  2. Modify the script to accept a username as a command-line argument.
    # greet_user.py
    import sys

    username = sys.argv[1]
    print(f"Hello, {username}!")
  3. Run the script from the terminal, passing in different usernames to make sure it works.

Exercise 3: CSV File Reader

  1. Create a CSV file with columns NameAge, and Email and populate it with sample data.
  2. Write a Python script named read_csv.py.
    # read_csv.py
    import csv

    with open('sample.csv', 'r') as file:
        reader = csv.reader(file)
        for row in reader:
            print(row)
  3. Run the script to make sure it works.

Download here the sample.csv file

Exercise 4: Simple Task Automation

  1. Create two text files, file1.txt and file2.txt, and put some text in them.
  2. Write a Python script named concat_files.py.
    # concat_files.py
    with open('file1.txt', 'r') as f1, open('file2.txt', 'r') as f2, open('combined.txt', 'w') as combined:
        combined.write(f1.read() + '\\n' + f2.read())
  3. Run your script and check that combined.txt contains the text from both files.

Exercise 5: Debugging Practice

  1. Add a logical bug to one of your previous scripts.
  2. Use Python's built-in debugger pdb to debug the script.
    # Insert the following line where you want to start debugging
    import pdb; pdb.set_trace()
  3. Document your debugging steps and the solution to the bug.

Exercise 6: Script Logging

  1. Pick one of your existing scripts.
  2. Add logging to the script using Python's logging module.
    # Add these lines at the beginning of your script
    import logging
    logging.basicConfig(level=logging.INFO)
  3. Include different types of log messages (e.g., infowarningerror).
  4. Run the script and inspect the generated log messages.

3.4 Practical Exercises Chapter 3: Basic Python Programming

Exercise 1: Your First Script

  1. Create a Python script named print_even_numbers.py.
  2. Make the script print even numbers from 2 to 20.
    # print_even_numbers.py
    for i in range(2, 21, 2):
        print(i)
  3. Run your script from the terminal to verify it works.

Exercise 2: Command-Line Arguments

  1. Create a Python script named greet_user.py.
  2. Modify the script to accept a username as a command-line argument.
    # greet_user.py
    import sys

    username = sys.argv[1]
    print(f"Hello, {username}!")
  3. Run the script from the terminal, passing in different usernames to make sure it works.

Exercise 3: CSV File Reader

  1. Create a CSV file with columns NameAge, and Email and populate it with sample data.
  2. Write a Python script named read_csv.py.
    # read_csv.py
    import csv

    with open('sample.csv', 'r') as file:
        reader = csv.reader(file)
        for row in reader:
            print(row)
  3. Run the script to make sure it works.

Download here the sample.csv file

Exercise 4: Simple Task Automation

  1. Create two text files, file1.txt and file2.txt, and put some text in them.
  2. Write a Python script named concat_files.py.
    # concat_files.py
    with open('file1.txt', 'r') as f1, open('file2.txt', 'r') as f2, open('combined.txt', 'w') as combined:
        combined.write(f1.read() + '\\n' + f2.read())
  3. Run your script and check that combined.txt contains the text from both files.

Exercise 5: Debugging Practice

  1. Add a logical bug to one of your previous scripts.
  2. Use Python's built-in debugger pdb to debug the script.
    # Insert the following line where you want to start debugging
    import pdb; pdb.set_trace()
  3. Document your debugging steps and the solution to the bug.

Exercise 6: Script Logging

  1. Pick one of your existing scripts.
  2. Add logging to the script using Python's logging module.
    # Add these lines at the beginning of your script
    import logging
    logging.basicConfig(level=logging.INFO)
  3. Include different types of log messages (e.g., infowarningerror).
  4. Run the script and inspect the generated log messages.

3.4 Practical Exercises Chapter 3: Basic Python Programming

Exercise 1: Your First Script

  1. Create a Python script named print_even_numbers.py.
  2. Make the script print even numbers from 2 to 20.
    # print_even_numbers.py
    for i in range(2, 21, 2):
        print(i)
  3. Run your script from the terminal to verify it works.

Exercise 2: Command-Line Arguments

  1. Create a Python script named greet_user.py.
  2. Modify the script to accept a username as a command-line argument.
    # greet_user.py
    import sys

    username = sys.argv[1]
    print(f"Hello, {username}!")
  3. Run the script from the terminal, passing in different usernames to make sure it works.

Exercise 3: CSV File Reader

  1. Create a CSV file with columns NameAge, and Email and populate it with sample data.
  2. Write a Python script named read_csv.py.
    # read_csv.py
    import csv

    with open('sample.csv', 'r') as file:
        reader = csv.reader(file)
        for row in reader:
            print(row)
  3. Run the script to make sure it works.

Download here the sample.csv file

Exercise 4: Simple Task Automation

  1. Create two text files, file1.txt and file2.txt, and put some text in them.
  2. Write a Python script named concat_files.py.
    # concat_files.py
    with open('file1.txt', 'r') as f1, open('file2.txt', 'r') as f2, open('combined.txt', 'w') as combined:
        combined.write(f1.read() + '\\n' + f2.read())
  3. Run your script and check that combined.txt contains the text from both files.

Exercise 5: Debugging Practice

  1. Add a logical bug to one of your previous scripts.
  2. Use Python's built-in debugger pdb to debug the script.
    # Insert the following line where you want to start debugging
    import pdb; pdb.set_trace()
  3. Document your debugging steps and the solution to the bug.

Exercise 6: Script Logging

  1. Pick one of your existing scripts.
  2. Add logging to the script using Python's logging module.
    # Add these lines at the beginning of your script
    import logging
    logging.basicConfig(level=logging.INFO)
  3. Include different types of log messages (e.g., infowarningerror).
  4. Run the script and inspect the generated log messages.

3.4 Practical Exercises Chapter 3: Basic Python Programming

Exercise 1: Your First Script

  1. Create a Python script named print_even_numbers.py.
  2. Make the script print even numbers from 2 to 20.
    # print_even_numbers.py
    for i in range(2, 21, 2):
        print(i)
  3. Run your script from the terminal to verify it works.

Exercise 2: Command-Line Arguments

  1. Create a Python script named greet_user.py.
  2. Modify the script to accept a username as a command-line argument.
    # greet_user.py
    import sys

    username = sys.argv[1]
    print(f"Hello, {username}!")
  3. Run the script from the terminal, passing in different usernames to make sure it works.

Exercise 3: CSV File Reader

  1. Create a CSV file with columns NameAge, and Email and populate it with sample data.
  2. Write a Python script named read_csv.py.
    # read_csv.py
    import csv

    with open('sample.csv', 'r') as file:
        reader = csv.reader(file)
        for row in reader:
            print(row)
  3. Run the script to make sure it works.

Download here the sample.csv file

Exercise 4: Simple Task Automation

  1. Create two text files, file1.txt and file2.txt, and put some text in them.
  2. Write a Python script named concat_files.py.
    # concat_files.py
    with open('file1.txt', 'r') as f1, open('file2.txt', 'r') as f2, open('combined.txt', 'w') as combined:
        combined.write(f1.read() + '\\n' + f2.read())
  3. Run your script and check that combined.txt contains the text from both files.

Exercise 5: Debugging Practice

  1. Add a logical bug to one of your previous scripts.
  2. Use Python's built-in debugger pdb to debug the script.
    # Insert the following line where you want to start debugging
    import pdb; pdb.set_trace()
  3. Document your debugging steps and the solution to the bug.

Exercise 6: Script Logging

  1. Pick one of your existing scripts.
  2. Add logging to the script using Python's logging module.
    # Add these lines at the beginning of your script
    import logging
    logging.basicConfig(level=logging.INFO)
  3. Include different types of log messages (e.g., infowarningerror).
  4. Run the script and inspect the generated log messages.