**CIS 192 Worksheet 1: Getting Started 🐍**
**Due Wed 1/18 11:59 pm EST**
(#) Objectives - Ensure you have Python 3 installed - Familiarize yourself with the homework submission process (#) Starter files - [ws1.py](ws1.py) (#) Installing Python Let's first make sure you have Python installed. We will be using Python 3 this semester. Please download and install the latest Python 3 version [here](https://www.python.org/downloads/), which is Python 3.11.1. !!! Note If you have an M1 Mac, be sure to download the Universal2 installer of Python [here](https://www.python.org/downloads/macos/). This is to avoid hardware compatability issues with some of the packages we'll be using later in the semester. You can then verify your installation by running `python3` in your terminal. If you're greeted with the `>>>` prompt, along with an indicator of Python 3.x version, you're in good shape: ~~~~~~~~~~ bash $python3 Python 3.11.1 (main, Jan 2 2023, 15:56:16) [GCC 7.5.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> ~~~~~~~~~~ !!! Tip If you have issues getting Python installed, or have a different development environment (such as WSL on Windows), let us know on [Ed Discussion](https://edstem.org/us/courses/33747) and we can help out! (#) Task 1: The Zen of Python [0.5 points] Once inside the interpreter, we can run commands interactively just like what we saw in lecture. I encourage you to play around and familiarize yourself with the interpreter, as it is a nice way to debug and prototype snippets of code. There's a little easter egg if we run the code `import this`: ~~~~~~~~~ bash $ python3 Python 3.8.2 (default, Jul 16 2020, 14:00:26) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import this The Zen of Python, by Tim Peters ... ~~~~~~~~~ The so-called "Zen of Python" is printed to your terminal (are we sure Python programming isn't a cult?). In a later lecture, we will discuss what the keyword `import` means. But for now, after reading through the output, open up the starter file in your favorite text editor (I'm partial to [VS Code](https://code.visualstudio.com/), but use whatever you like!), and write one of the sayings that sticks out to you **as a single line comment**: ~~~~~~~~ python # single line comments begin with a # ~~~~~~~~ Once you're done with the interpreter, type `exit()` or hit `ctl-d` to exit. (#) Task 2: Saying hello [1 point] Now that you have `ws1.py` open in an editor, you'll see that we've provided a `hello()` function definition in it, with a triple-quoted comment describing the function. The triple-quoted comment that sits on the first line of a function is typically known as a **docstring**. There is more to be said about docstrings as well, but for now let's look at the next line in the definition (line 3 below): ~~~~~~~~~~~ python linenumbers def hello(): """Task 2: a function that greets the world""" raise NotImplementedError if __name__ == "__main__": hello() ~~~~~~~~~~~ Here is an example of raising an [Exception](https://docs.python.org/3/tutorial/errors.html), with the `raise` keyword operating much like the `throw` keyword in Java or C++. The `NotImplementedError` is a built-in Exception type, and indicates when...something is not implemented. All of the function stubs we will give in the homeworks will have this exception, which will be your cue to implement the function. Another thing to note is conditional statement `if __name__ == "__main__"` on line 5. The Python interpreter does a number of things under the hood, and one of the things it does is assign the variable `__name__`. If we run a `.py` from the command line using `python3`, it is as if the interpreter runs the following code before your code is executed: ~~~~~~~~ python # the interpreter implicitly does this __name__ = "__main__" ~~~~~~~~ Thus, the `if __name__ == "__main__":` conditional is the Python paradigm for a "main" function. Let's see this and the exception in action by running `python3 ws1.py` in the terminal: ~~~~~~~~ bash $ python3 ws1.py Traceback (most recent call last): File "ws1.py", line 7, in hello() File "ws1.py", line 5, in hello raise NotImplementedError NotImplementedError ~~~~~~~~ Python will usefully give the code traceback of where the error or exception occurred during runtime to help with debugging. Also note that there is no compilation process for Python programs, as the language is [interpreted](https://www.python.org/doc/essays/blurb/). Now, let's implement `hello()` with a `print()` greeting: ~~~~~~~~~~~ python linenumbers def hello(): """Task 2: a function that greets the world""" print("Hello, world!") ~~~~~~~~~~~ Running `python3 ws1.py` now prints the greeting with no error: ~~~~~~~ bash $ python3 ws1.py Hello, world! ~~~~~~~ (#) Task 3: Tell us about yourself [0.5 points] Finally, we'd like to get to know everyone in the course a little better. In the **multi-line** triple-quote comment provided, fill out your responses to the following questions: - Where is your hometown? - What do you want to get out of this course? - Is there anything you'd like us to know about? This could pertain to your learning situation this semester, or something more general. (#) Submitting to Gradescope Now that we're done with everything, let's submit this worksheet to Gradescope to get a feel of the submission process. Navigate to the [Worksheet 1 Gradescope assignment](https://www.gradescope.com/courses/477992), and upload your `ws1.py` submission. You will then see the autograder process your file, which should give you full credit for your implemented `hello()` function: ![](ws1_screenshot.png) Both homework and worksheets will be autograded whenever possible this semester, so be sure to reach out on Piazza if you have any issues with this submission. One common gotcha is that the file name must remain the same as we give it to you, so that the autograder knows which file to look for. !!! Tip On assignments going forward, sometimes functions will be dependent on other functions in their implementation. Since assignments are autograded, it is important that you do not change the names of functions in the starter files we provide you unless instructed to do so. That's it, worksheet 1 complete!