Assignment 4

Introduction to Python Programming

Objective:

In this assignment, you will explore basic Python programming concepts, including arithmetic operations, variable assignments, data types, lists, and user inputs. You will create and run simple Python scripts to apply these concepts and document your results.


Instructions:

Folder Structure

  1. Create a main folder named Assignment4.
  2. Inside Assignment4, create the following subfolders:
  3. scripts: for storing your Python scripts.
  4. figures: for storing screenshots of your outputs.
  5. documents: for any additional notes or explanations.

    1. Task 1: Basic Arithmetic Operations

      • In the scripts folder, create a Python file named arithmetic_operations.py.
      • Write a Python script that performs addition, subtraction, multiplication, division, modulo, and exponentiation using different integer values.
      • Use the following format for printing each result in the script:
      a = 7
      b = 3
      print(f"{a} + {b} = {a + b}")
      print(f"{a} - {b} = {a - b}")
      print(f"{a} * {b} = {a * b}")
      print(f"{a} / {b} = {a / b}")
      print(f"{a} % {b} = {a % b}")
      print(f"{a} ** {b} = {a ** b}")
      
      • Run the script in the terminal:
      python3 scripts/arithmetic_operations.py
      
      • Redirect the output to an arithmetic_operations.txt file:
      python3 scripts/arithmetic_operations.py > documents/arithmetic_operations.txt
      
      • Take a Screenshot:
      • Use the screen command to split your terminal into two panes.
      • In the left pane, open arithmetic_operations.py with vim.
      • In the right pane, use cat to display the contents of documents/arithmetic_operations.txt.
      • Take a screenshot showing both panes side by side and save it as arithmetic_operations.png in the figures folder.

      Task 2: Command-line Output to File

      • In the scripts folder, create a Python file named output_to_file.py.
      • Write a script that performs various arithmetic operations. Use the following example format:
      print((2 + 3 * 5) % 7)
      print(10 * 2)
      print(2 * 10**-4)
      
      • Run the script and redirect the output to a file named output_to_file.txt:
      python3 scripts/output_to_file.py > documents/output_to_file.txt
      
      • Confirm the output by displaying the contents of output_to_file.txt:
      cat documents/output_to_file.txt
      
      • Take a Screenshot:
      • Use the screen command to split your terminal into two panes.
      • In the left pane, open output_to_file.py with vim.
      • In the right pane, use cat to display the contents of documents/output_to_file.txt.
      • Take a screenshot showing both panes side by side and save it as output_to_file.png in the figures folder.

      Task 3: Basic Print and Input Functions

      • In the scripts folder, create a Python file named print_and_input.py.
      • Write a script that includes print and input functions. Use the following format:
      print("Hello World!")
      print("This is written in Python.")
      user_answer = input("How old are you? ")
      print("Okay. You are " + user_answer + " years old.")
      
      • Run the script:
      python3 scripts/print_and_input.py
      
      • When prompted, enter a sample age (e.g., 20) to see the result.

      • Take a Screenshot:

      • Use the screen command to split your terminal into two panes.
      • In the left pane, open print_and_input.py with vim.
      • In the right pane, run the script and enter a sample input to display the output.
      • Take a screenshot showing both panes side by side and save it as print_and_input.png in the figures folder.

      Task 4: Data Types in Python

      • In the scripts folder, create a Python file named data_types.py.
      • Write a script demonstrating different data types. Use the following format:
      print("Integers")
      print(int(2.1))
      print("Floats")
      print(float(2.1))
      print("Strings")
      print(str(2.1))
      print("Booleans")
      print(bool(2 > 3))
      
      • Run the script:
      python3 scripts/data_types.py
      
      • Take a Screenshot:
      • Use the screen command to split your terminal into two panes.
      • In the left pane, open data_types.py with vim.
      • In the right pane, run the script and display its output using cat.
      • Take a screenshot showing both panes side by side and save it as data_types.png in the figures folder.

      Task 5: Working with Lists

      • In the scripts folder, create a Python file named lists_operations.py.
      • Write a script to create and manipulate lists. Use the following format:
      firstList = [2, 3, 5, 7, 11]
      print(firstList)
      secondList = ['A', 'B', 'C']
      print(secondList)
      thirdList = [13, 17, 'D', 'E', 19.2, 23.3]
      print(thirdList)
      print("First item:", firstList[0])
      print("Last item:", secondList[-1])
      
      • Run the script:
      python3 scripts/lists_operations.py
      
      • Take a Screenshot:
      • Use the screen command to split your terminal into two panes.
      • In the left pane, open lists_operations.py with vim.
      • In the right pane, run the script and display its output using cat.
      • Take a screenshot showing both panes side by side and save it as lists_operations.png in the figures folder.

      Finally, create a README.md file in the Assignment4 folder:

      vim README.md
      

      In the README.md file, briefly describe what each file and folder is as follows:

      # Assignment 4: Introduction to Python Programming
      
      ## Objective
      
      This assignment focuses on learning basic Python programming concepts, including arithmetic operations, data types, lists, and user inputs. The project is divided into five tasks, each with scripts and documentation.
      
      There are four components (3 `folders` and 1 `.md` file) here:
      
      1. **`scripts`**
      
         This folder contains 5 Python scripts:
         - `arithmetic_operations.py`: Demonstrates basic arithmetic operations.
         - `output_to_file.py`: Performs arithmetic operations and redirects output to a file.
         - `print_and_input.py`: Showcases the use of `print` and `input` functions.
         - `data_types.py`: Illustrates different data types like integers, floats, strings, and booleans.
         - `lists_operations.py`: Creates and manipulates lists in Python.
      
      2. **`figures`**
      
         This folder contains screenshots documenting the Python scripts:
         - `arithmetic_operations.png`: Shows `arithmetic_operations.py` and its output.
         - `output_to_file.png`: Displays `output_to_file.py` and its output file.
         - `print_and_input.png`: Shows `print_and_input.py` with sample input/output.
         - `data_types.png`: Displays `data_types.py` and its output.
         - `lists_operations.png`: Shows `lists_operations.py` and its output.
      
      3. **`documents`**
      
         This folder contains:
         - `arithmetic_operations.txt`: Output of `arithmetic_operations.py`.
         - `output_to_file.txt`: Output of `output_to_file.py`.
      
      4. **`README.md`**
      
         This `README.md` file provides an overview of the assignment and its components.
      

      Save and exit the README.md file. (:wq)

      Submit your Assignment 4:

      Before running the submitAssignment.sh file, make sure you are in the "Assignment 4" folder. Running this script in any other folder or subfolder may result in missing files being uploaded. When executed in the correct folder, all files in the "Assignment 4" directory will be included in the submission.

      • If everything is ready, type the following command in your terminal to submit your assignment:
      submitAssignment.sh