Laboratory Exercise #0

Laboratory Exercise #0

This assignment focuses on the mechanics of installing and using Python. The deadline for Codio submission is 11:59 PM on Monday, January 8.

1. Complete the steps given below to download and install Python. 2. Submit your completed "Hello World" program to Codio.

1. Get Python and Install PyCharm

You can skip this step if you already followed the steps-by-step instructions on how to get python and install PyCharm provided in the following tutorial:

2. Getting started with PyCharm

Microsoft Windows To start the PyCharm under Microsoft Windows, go to Start Menu ==> JetBrains ==> PyCharm. You can also just search for PyCharm from the search bar.

Then wait because it takes minutes to load while you look at this.

Mac (OS X) On a Mac (OS X), Run the PyCharm app from the Applications directory, Launchpad, or Spotlight.

3. Using PyCharm

When you open PyCharm for the first time, you are presented with the Welcome Screen.

On the Projects tab, you can create, open, or clone projects to start working with. Click on the "New Project" to start a new project. A new window will open. Before you create a file, you need to select the folder/directory where you will be working, i.e. saving your programs. In the top bar, "Location" is a one-line window indicating which folder/directory you will be working in. Here I have specified that I will be working in the folder /directory named D:\Teaching\CSE_courses\CSE231\Projects (I'm on PC, but Mac people will see something similar). The file folder allows you to browse for your desired folder/directory.

Next, in Python projects, it's best practice to do your work by creating a virtual environment. Each project then acts like it has its own Python. If you install some packages in one project, it won't break another project. PyCharm takes care of this part by creating and activating a virtual environment for your new project. Back to our New Project screen, we have a section called "Python Interpreter: New Virtualenv environment". In our case, we are going to create a new one using virtualenv. We already have Python 3.11 installed as our default Python. PyCharm detected it, so we can click "Create". PyCharm will now create a new Python project for you as well as creating and activating a virtual environment, downloading all the required dependencies, generating an empty Python project with configuration files, creating run and debug configurations, and more.

If you close this project, you go back to the PyCharm Welcome Screen where you can see that the Projects tab has changed slightly. Now you see your recent projects, and this list will grow as you have more projects.

4. Understanding PyCharm interface

When you open a project in PyCharm, the default user interface looks as follows:

There are three parts to the window:

? The top half is where you write your programs. ? The bottom left region is the Python shell where you can enter code to test. Note that you have

to click on the icon highlighted in blue to see The Console (or Shell). ? The lower right region is where you can observe the values of variables that you create--

essentially the current namespace, a term you will learn about soon. Finally, in the middle of the top bar is an important region where you can specify which file to run or which mode. An important capability of PyCharm is that it has a built-in debugger--something that means nothing to you now but will be covered later in the semester and will be useful later.

Several new widgets are located in the main window header (the top bar):

(1) Main menu (Windows only): The main menu is now hidden under the hamburger icon. To access menu categories, click the icon. The elements will appear horizontally over other header widgets.

(2) Project widget: The widget shows the current project's name, allows switching between recent projects, creating new projects, and opening existing ones.

(3) Run widget: The widget allows you to start run/debug configurations, select other configurations to run, and change the mode for the current configuration (run or debug). You can edit, pin, or delete configurations using this widget. When a process is running, you can restart or stop it using the widget.

5. Working with the shell

The window in the lower left corner is the Python shell. The shell is interactive: you can type Python commands in the shell and Python will execute them, generating a result. In the Python shell try typing:

x = 1 + 1

print( x )

print( "Hi Mom" )

What output do you get? It should look like this:

>>> x = 1 + 1 >>> print( x ) 2 >>> print( "Hi Mom!") Hi Mom!

What you type shows up after the >>> prompt. When you type something and hit the Enter key, the result shows up on the next line(s). Sometimes you can get some surprising results, and sometimes an error. For example, try entering:

1 + 1.2

or perhaps

print( hello )

The results would look like the following:

>>> 1 + 1.2 2.2

>>> print( hello ) Traceback (most recent call last):

File "C:\Program Files\JetBrains\PyCharm 2023.2\plugins\python\helpers\pydev\pydevconsole.py", line 364, in runcode

coro = func() ^^^^^^

File "", line 1, in NameError: name 'hello' is not defined. Did you mean: 'help'?

The last lines show that an error occurred. Since hello was neither a variable nor had quotes around it, the print operation failed (we'll learn more about this later).

5. Developing a program

Typing into the shell is useful, but the commands that you write there are not saved as a file so they cannot be reused. We need to save our commands in a file so we can run the program again and again, and more importantly turn it in!

The big window on the top is an editor window into which you can type your first program.

To create a file, select the Menu icon (hamburger icon) ==> New (or figure out the shortcut) ==> File. Give the file a name. In our case, we will call the file lab00.py

Always put a ".py" at the end of the filename and notice the "dot" (a.k.a. period) ? it is very important. Hint: always check the directory/folder that you are saving into is the one you want to use because your operating system settings may default to somewhere unexpected.

There is a tradition in computer science: the first program you create is the HelloWorld program. This program does nothing but display "Hello, World!" on the screen. It is a useful tradition because it does very little except focus on the mechanics of writing your first program and running it.

In Python, the HelloWorld program is easy. Type the following in the editing window (i.e. the big window on the top half of PyCharm).

print( "Hello, World!" )

The phrase inside the parentheses should be in quotes (either single quotes or double quotes). Because your code will be checked for accuracy when you submit it remember to include the space after the comma and the exclamation point.

Save the program (as above).

You can then run your module to see what your program produces. To run the program, you need to first specify the run configuration to always run the "Current File" as below:

Then, click on the icon (or Shift + F10 function key on your keyboard).

The result is new output displayed in your Run window (bottom left window), as shown below.

You can see "Hello, World!" printed near the bottom.

6. Submit Your Assignment

The final step is to submit your "lab00.py" program using Codio system. You need to access the submission link through D2L. How do you find the link to the assignment Lab00 in Codio? there is a link to Lab00 in Codio (as shown in the Figure 1 below). The first time you access Codio, you will be asked to pay for access. You need first to activate your Codio subscription. To do that, you should navigate to the "Course Content Module" in D2L "Content" Bar, then from the "Week 0 (before class starts)" sub-module, click on the "Lab00" activity ( as see in the picture below). This link D2L to Codio. Follow the steps to pay for the subscription. Once you pay, you will be redirected to the rest of the assignment where you can submit your code. Note that after the first time, you do not need to follow the same link. You will need to use the link for each specific assignment.

Once you are in the assignment window, you can copy and paste your code into the Codio IDE (Coding window) as shown below (Figure 2). To check your answer, click on the "Check" button. You can click on that button as many times as you want. To submit your final version, click on the "Mark as Completed" button.

Figure 1: where to find the link to submit your work.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download