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, August 26.

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 ¡°Project venv¡±. We

already have Python 3.11 installed as our default Python. We recently installed Python 3.12.5.

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'?

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

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

Google Online Preview   Download