Week 1: Introduction

Introduction to CS21

Welcome to CS21! This course is designed to have a mix of lecture and hands-on, in-class exercises. This page will review some of the tools we will use during the labs and help you get set up with the system in the CS department. This week, you should read the course syllabus, these notes, and the Lab 0 writeup.

Staff Introductions

  • Professors: Andrew Danner, Rich Wicentowski

  • Lab Lecturers: Jocelyn Corey, Gautam Mohan

  • Academic support coordinator: Lauri Courtenay

Who should take this course?

This course is designed for students with little or no programming experience. If you have taken a computer science course previously or taught yourself basic programming skill in any language, even if it wasn’t Python, you may find this course too slow. Students with prior programming experience are encouraged to contact Lila Fontes to take the CS Placement exam to see if CS21 is the proper fit.

Adding the course, switching sections

If you are looking to add this course or switch to a different section, please contact Lauri Courtenay. Lauri manages the waitlist for all sections.

Course Webpage

The Course Website is updated regularly and contains recent course announcements, links to in-class exercises, lab assignments, quiz topics, and other less dynamic course information including office hours, ninja sessions, and Python tips. Bookmark the website in Firefox or Chrome and refer to it regularly. All sections have the same quiz dates and lab assignments.

EdSTEM

We use EdSTEM to manage course discussions and announcements. If you are registered for the course, you should be automatically enrolled on EdSTEM. If you’re unable to log in or access the course discussion forum, please contact your course instructor — it’s possible something may not be configured correctly for you, especially if you joined after the initial registration phase. Waitlisted students will not have access to EdSTEM until they have been added to the course.

You’re encouraged to ask questions on the EdSTEM forums instead of sending emails directly to course staff. Please review the EdSTEM guidelines for using the forums.

Lab 0 will walk you through using EdStem, and give you some practice posting and reading posts.

Labs

This course has a mandatory lab section. Attendance is required unless you have completed and submitted the lab for the week prior to the start of lab. Labs started Tuesday and Lab 00 is due Saturday night.

Creating accounts

The CS machines in the Sci 256, Sci 240, the overflow lab (Sci 238) and Clothier Lab are on a separate network than the machines managed by ITS. You need a separate account with a separate password to access these machines. If you did not get information about setting up your account remotely or changing your password, please contact us. Please note, while your username may be the same as as your ITS account, your CS account should have a different password. If you ever forget your password, you can reset it by going to CS password reset page.

Your student ID has an NFC tag that can allow you access to the building and the labs after hours. You should have automatically have access to the lab space by enrolling in the course. If you are having trouble with the card readers, please let us know.

What is Computer Science?

Computer science focuses on two primary questions; what can be computed, and how efficiently can computers solve problems? The answers are more nuanced than "everything", and "really fast". At the core of the discipline is algorithms. Algorithms are concise descriptions of how to solve a problem computationally. Algorithms can be implemented in a programming language and interpreted by computer hardware to automate computation. Programming is NOT the core of computer science. Programming is a way to automate and test the creative thought process that resulted in an algorithm. Programming is one tool at the disposal of computer scientists, but it is not the only tool. This course will teach you how to discover, develop, and write clear computational solutions to, often times non-computationally themed, problems. To check your thinking, you will also learn programming, debugging, and testing skills.

What is Python?

Python is the programming language we will use to implement and test our algorithms. It is relatively easy to learn, even for people not in computer science or related fields. It is free to download and it runs on many platforms including Linux, Mac OSX, and Windows.

update21 and handin21

Eventually, we will be saving our Python code in files. You will also be using two commands regularly through the course to get and submit files for the course: update21 and handin21. The instructions in Lab 0 show how to run these commands. You should run update21 before starting each lab assignment and handin21 to turn in lab assignments. You can run them as often as you like. update21 will not overwrite files you modify after running update21. handin21 can submit the same lab multiple times. Each handin submission is stored separately, but only most recent copy submitted prior to the deadline will be graded. You may continue to submit after the deadline, but these submissions will be ignored.

If update21 or handin21 does not work for you or it says you are not allowed to run these programs, email me. It’s usually my fault, not yours. We may need to add you to the class roster, change handin or change permissions on file.

Writing our first full Python program

First, open a terminal to start typing linux commands.

Then run update21. If update21 worked for you, it should have created an welcome.py file in your ~/cs21/inclass/ directory. Let’s practice some basic Linux commands to:

  1. navigate to the correct folder using cd

  2. list the contents of a folder using ls

  3. open a file for editing using code

  4. editing, saving, and running our first Python program.

cheese[~]$ cd

cheese[~]$ ls
Desktop/  Documents/   cs21/

cheese[~]$ cd cs21

cheese[cs21]$ ls
inclass/  labs/

cheese[cs21]$ cd inclass/

cheese[inclass]$ ls
welcome.py

cheese[inclass]$ code welcome.py

Inside the vscode editor, we are view our first Python program. Edit the file to use your name instead of A. Student to take credit for your work.

"""
 My first Python program
 A. Student
 January 2022
"""

def main():
    print("Welcome to CS21!")

main()

Save the file using Control-S

Back at the command prompt, you can run the program by typing the following in the terminal:

cheese[~]$ python3 welcome.py

Did it work? If not, what errors did you observe? Try adding a second print statement to your program, saving your program, and running it again.

Variables and types

We use computer programs to process data, so a program must have a way of storing information and referring to it later. Our primary way of doing this is through the use of variables. A variable is a named container that stores a value of a particular type. In the example below, place is a variable that stores the name of a place. The data type currently stored in place is a string containing a sequence of characters between quotes.

place = "Paris"

The syntax

<var> = <value>

assigns the variable name on the left hand side the value of the expression on the right hand side. It is illegal, or a syntax error to have a constant value on the left and a variable name on the right.

Variable Types

In addition to the string type (str), we will initially focus on the integer (int) data type for whole numbers, for example:

numStudents = 35
year = 2021
diff = -6

and the floating point (float) data type for decimal data, for example:

tempF = 34.1
gravity = 9.8

I encourage you to read Chapter 2 of the online text book for a longer discussion on topics regarding

  • what are valid variable names?

  • what are valid operations between strings, ints, and floats?

  • can you use an operator with values of different types, e.g., a float plus a int? What about a string plus a float?

Another good way to experiment is just to open a python shell and try a few one liners.

$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> 3+2
5
>>> "hello"-7
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unsupported operand type(s) for -: 'str' and 'int'
>>> "hello"+"there"
'hellothere'
>>> 7.16-4
3.16
>>> x=4
>>> y=3
>>> x+y
7
>>> x=y
>>> y=7
>>> x
3
>>> y
7
>>>

Converting between types.

Sometimes you may find it helpful or necessary to change a value of one type to another type. For example the value "7" is a string because it enclosed in quotes. But the contents of the string, the character 7 is also a valid integer. You can convert a value to an integer using the int() built in function. Try these examples in the python shell.

int("7")
int(3.2)
int("3.2")
int(5.7)
int("puppy")
ans="12"
int(ans)

Similarly, you can use float() to convert a value to a float, or str() to convert a value to a string.

Getting input from the user.

One of the most helpful built-in functions in the first few weeks of the course will be the input() function. When provided a string prompt as input argument, this function prints the prompt to the screen and waits for a user to type something. When the user presses the Enter key, their response is returned as a value by the input() function as a string. A sample usage might be

"""
A simple greeting program
your name here
September 2021
"""

def main():
  name = input("What is your name?: ")
  print(name)

main()

The program above is pretty terse. Can you modify it so it prints "Hello" followed by the entered name on the same line?

Designing a full program

Suppose we are given the following problem: Have the user enter the current year as a number and the year he or she plans to graduate and print how many more college years he/she has left. We want to design an algorithm that can solve this problem (this one is a bit easy) and then implement and test our solution in python.

Here are some steps a computer scientist might use to solve the problem:

  1. First think about how you would solve the problem. no keyboards, mice, or even writing utensils. Just think.

  2. Write or sketch a solution on paper. Discuss you solution with a friend or neighbor. This solution does not have to be in python. Pseudocode is fine.

  3. Write your solution in python using your editor (atom).

  4. Save, run, and test your solution. Go back and make changes if needed.

The real innovation is in steps 1 and 2. Steps 3 and 4 are sometimes skipped, argued logically/mathematically, or handed off to new hires, grad students, or little brothers. Always do step 4 if you do step 3.

cd
cd cs21/inclass/w01-intro
atom ./
python3 grad.py
Enter the current year: 2021
Enter your graduation year: 2024
You have 3 year(s) until graduation