COMSC 101 Programming Exercises FA15

COMSC 101 Programming Exercises FA15

Programming is fun! Click HERE to see why. This set of 10 exercises is designed for you to try out programming for yourself. Who knows ? maybe you'll be inspired to pursue study in computer science. At least you'll gain an appreciation for discipline, exactness, patience, and attention to detail that it takes to be a programmer. In any case, learning to program can help you develop problem-solving skills.

As you go through these assignments, take the time to: ? understand the problem before you start doing any programming ? read all instructions first, ? complete one step at a time and make sure it's correct before starting the next step, ? know when to take your losses ? remember you can always start over if you get hopelessly lost.

In order to develop any useful understanding of programming, it is necessary to actually do some programming. That's what this series of exercises does ? it provides a structured, step-by-little-step sequence for learning basic programming steps. If you are diligent, read thoroughly, and precise, you will go easily from one exercise to the next, learning a bit more each time.

Python 3

The exercises in this series are designed using the "Python 3" computer programming language. There are two ways to work with Python ? one that requires only a browser and an Internet connection, and another that requires installation of software on your computer but needs no internet connection after that. Your instructor will guide you in making your choice between the two.

To use a browser, you'll go to repl.it/languages/Python3, and you'll create your programs and run them directly on that website's home page. The advantage of using a browser is that you can work anywhere there is an Internet connection. The disadvantage is that Internet connections can be lost or can time out. We'll refer to this way of working with Python as repl.it.

To use installed software, you'll first have to download and install Python itself, and a program called PyCharm which does what the browser page does. In DVC's computer labs, these are already installed.

If You Get Stuck

You may struggle with some of these assignments. That's normal ? it's part of the learning process. When it happens do this: start the assignment over from scratch, and repeat this process until you complete the assignment without struggle. Do this before going to the next exercise, because each new exercise builds on what you learned from the previous ones. And click HERE for FAQs that may help you get unstuck.

Online students: Contact your instructor for help before spending a lot of time in trial and error and becoming frustrated. Remember that this is supposed to be a positive experience of learning some basics of how computer programming works. There are many conceptual hurdles to overcome throughout this series of exercises, and your instructor is here to help you over them.

Inperson students: These exercises are to be completed during the lab period of your regular scheduled class time, with the help and supervision of your instructor. Seek your instructor's help whenever you find yourself struggling with understanding the requirements of an exercise or if things are not working for

Comsc 101

Page 1 of 21

Programming Exercises

you. These exercises are not designed for you to do outside of class on your own, so if you work on any at home and run into difficulty, bring it to class so that you can get it resolved.

EXERCISE 1a: Using The Internet Browser to Write And Run Python Programs [hello.py]

Purpose. The purpose of this exercise is to make sure you can access and use repl.it. Your instructor may direct you to skip this exercise and go to exercise 1b instead, using only PyCharm.

Requirements. On a Windows PC or on a Mac, open the Internet browser of your choice. Make sure you have access to the Internet, and go to this URL:

repl.it/languages/Python3 You should see something very much like this:

Exploring. If so, type this in the "edit box" next to the

"1": print ("Hello World") including the quote

marks and parentheses. Then click

to "run" the

program, and you should see this in the "output box"?

note "Hello World!":

So already you have seen what a Python program looks like, you've "run" a program that you typed into what we call the "edit" box. And we saw what actual output looks like, in what we call the "output" box. Good start!

Experimenting. Play around with this ? try typing different text in the "print statement" of the code, and run ? observe the output. Add more print statements above or below the one with "Hello World!" ? see what output you can produce. Be sure to fully left-justify each print statement!

HINT: To add another print statement, "copy/paste/markup" the existing "print ("Hello World")" statement ? do not type it from scratch! This helps to avoid typing errors.

Comsc 101

Page 2 of 21

Programming Exercises

YOUR TURN. Here's the exercise ? in the "edit" box, insert two blank lines above the print statement you typed earlier, just like you would insert a line in any text editor. In the new line "1" type a hash symbol (#) and a space, and type your name and student ID.

Starting in line 2, type additional comments as directed by your instructor. Remember to use the hash symbol at the start of each comment! Write as many comments as necessary, and leave one blank line after the last comment for spacing.

Starting after the blank line that follows the comments, enter one or more "print statements", printing the text that your instructor assigns. It may include your name, something about yourself, something about the class or the assignment -- however your instructor directs you.

About that print statement ? the space before the opening parenthesis is not required. And the double-quotes can be single quotes. These are your choices ? pick a way and stick with it.

Submitting Your Work. Your instructor may just want to see your working program in the lab, and check you off without "submitting" the file or they may require that you submit the program in printed or electronic form. In any case, you will certainly want a backup of the program you wrote, because once you close your browser, all you typed will be lost! So save it to a file:

To save your work to a file, open Notepad++ or Windows 8 Notepad or Mac TextEdit (formatted to "make plain text"). With that running in one window, navigate back to Python browser page and select and copy the contents of the "edit" box. Navigate back to Notepad or TextEdit and paste. Save it with a name as specified by your instructor. That's the file you will submit.

Avoid Microsoft Word, or any rich-text editor (like Mac TextEdit formatted to "make rich text"). These can lose line breaks or insert extra ones when copied back into the "edit" box.

Comsc 101

Page 3 of 21

Programming Exercises

If you have any further work to do on the file ? any more programming to do ? anything your instructor wants you to fix ? then open the file, select/copy its Python program, and paste it all into the "edit" box of the browser page, and just continue from there.

HINT: To clear the output box, click at the top middle of the output box.

HINT: If you cannot see the button, then widen your browser window, or make the edit box wider by grabbing and dragging its border with the output box.

TIMING OUT. If you wait too long without doing anything, repl.it times out. If this happens, select your program in the edit box using ctrl-A (or command-A) and copy it using ctrl-C (or command-C), then refresh the page with ctrl-R (or command-R). When the page reloads, click in the edit box and do ctrl-P (or command-P) to paste and restore your program. Then proceed as normal.

EXERCISE 1b: Using PyCharm To Write And Run Python Programs [hello.py] Purpose. The purpose of this exercise is to make sure you can access and use PyCharm. Your instructor may direct you to skip this exercise and use only repl.it instead, per exercise 1a.

PyCharm Installation for Online Students. Before you can use PyCharm, you'll need to install it. There are two separate installations ? one for Python 3 and one for PyCharm. To install Python 3, go to and choose "Download Python 3.4.3". Then scroll down to the "Files" section and select the "installer" file that matches your system. There's one for older Macs (32bit) and one for newer Macs (64-bit). There's one for older Windows PCs (x86 MSI) and one for newer (x86-64 MSI). Select all defaults during the installation process.

Once that's done, go to and download the Windows or Mac version of the Community Edition. Select all defaults during the installation process. Then you should be able to find PyCharm in your Windows PC start menu or your Mac Applications folder.

Starting PyCharm In Windows 7. The DVC computer labs are Windows 7 PCs. An easy way to start PyCharm is to use the start menu and type pycharm in to the search box. Then click "JetBrains PyCharm Community Edition 4.0.6".

On any other Windows system, find the (JetBrains) PyCharm icon and double-click that. It could be on the desktop or in the start menu or in the Program Files folder on the C drive.

On a Mac, double-click "PyCharm CE.app" in the Applications folder.

There are "Python" icons, too, but you'll not need to use them.

Comsc 101

Page 4 of 21

Programming Exercises

Creating a PyCharm "Project". You may be prompted to create a new project. If so, choose "Create New Project". Good "Locations" for projects are your flash drive or your desktop in a new folder named "python". And make

sure that the "Interpreter:" is 3.4.3 or 34:

Creating a Python Program. If all this works, you should see something like this, with the name and location of the folder you selected when you created your project:

Right-click over the folder name aened

popup- windosw. Choose

"New" a?nd

"nFoitle""Python

File". Nhaemlleo.py.

it

you'll the

Then choose the file named "hello.py" and select it in the left-side navigation window of PyCharm:

You are now ready to create and run your first Python program! See the window on the right side of the screen with the "hello.py" label? That's where you'll type your program. It will be stored in a file named hello.py in your python folder.

Exploring. Type this in the "hello.py" box: print ("Hello World") including the quote marks and parentheses. Then click to "run" the program. Actually, the might be "grayed out" and not work the first time you try it. In that case, choose "Run..." from the "Run" menu:

Comsc 101

Page 5 of 21

Programming Exercises

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

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

Google Online Preview   Download