Intro to
Computer
Science

Spring 2021
course
site
-->

chapter 2 : writing simple programs

... start recording.

checking in

First: any questions about anything so far?

Second: here are my answers to the unix shell lab. We'll discuss some of the tricks, especially grep -ri oxygen . .

Third: the assignment due next Thursday is posted.

And also : tutoring hours are posted ; see the zoom times link in the left menu.

where we're going

These notes cover

running python

At this point you should have seen this, but just to recap ... here are some of the ways to run a python program.

We'll be doing graphics next week, but for now - chapter 2 & 3 in the text - the terminal or IDLE is all we need. I encourage you to try this stuff yourself at an interactive python prompt.

$ python
>>> 

python versions

Be aware that there are several versions of python currently in use, and you may need to type different things on different computers, depending on what's installed.

You can find the version in a terminal with the --version option. On my laptop (a mac powerbook with a customized python installation), I see

$ python --version
Python 3.8.4

In a jupyter.bennington terminal, I get

$ python --version
Python 3.6.6

On some systems, "python" gives version 2.something, while "python3" gives 3.something.

You should be working with something more recent (i.e. a higher number) than 3.6 .

submitting homework

I've had several folks asking about specifics for turning in assignments.

First, everything should be submitted here, through the "submit work" or "your work" link on the assignments page . I walk through that in one of my videos.

The jupyter.bennington server is one place that you can do work, but you should still either copy/paste the code from there, or download .py or .ipynb or .html files to your computer, then upload them to the assignments submission page.

Depending on the assignment, typically you should turn in

If for some reason you can't access the website, email me (jimmahoney@bennington.edu) with your code as attachments, and I'll upload it to the website.

programming language paradigms

Just to put the python programming language into context, it's worth knowing that are several distinct programming approaches. See for example programming paradigms . The big categories are :

There's a lot of overlap between these styles and many languages span several categories.

Python programs can be written in all three of these styles.

This term we'll be doing mostly the first two ... and a little of the third.

chapter 2

software development cycle

variables

variables - named "boxes" to put data in

assignment statement (i.e. how to put data into a variable)

name = "Jim Mahoney"
age = 57
data = [2.3, 3.2, 16.23]

The part on the RIGHT is evaluated first. Then that is put INTO the thing on the left. Consider

x = x + 1

What is going on? Run this pythontutor.com to see.

example 1 : temperature convert

# convert.py
# A program to convert Celsius temps to Fahrenheit 
# by: Susan Computewell

def main():
    celsius = eval(input("What is the Celsius temperature? ")) 
    fahrenheit = 9/5 * celsius + 32
    print("The temperature is", farenheit, "degrees Fahrenheit.") 

main()

Notice the difference between first defining a "main" function, and then actually running it later.

We may do breakout rooms and have you try something like this yourself. Please share your screen with the others in your group, and take turns writing a program that asks for a number, and then prints that number and twice that number, like this :

  $ python double.py
  What is your number? 23
  You said 23
  Double that is 46

I've attached my version of that program ; scroll down to the bottom of the page to see it. I created it in jupyter using the "New ... Text File" menu. Running it in the jupyter terminal looked like this. (I copy-pasted from the terminal into this window with three backtick marks before and after.)

jim@jupyter:~$ python double.py
What is the number? 21
You said  21
Twice that is  42
jim@jupyter:~$

example 2 : future value

# futval.py
# A program to compute the value of an investment 
# carried 10 years into the future

def main() : 
    print("This program calculates the future value") 
    print("of a 10-year investment.")
    principal = eval(input("Enter the initial principal: ")) 
    apr = eval(input("Enter the annual percent interest rate : "))
    for i in range(10):
        principal = principal * (1 + apr/100)
    print("The value in 10 years is: ", principal)

This "accumulator pattern" - initializing a variable, then putting more stuff into it in a loop, "accumulating" a result - is a programming technique that we will use many times in different ways throughout the semester.

I encourage you to run this yourself at pythontutor.com to see what exactly is going on, especially in the loop.

other things to understand


https://cs.bennington.college /courses /spring2021 /introcs /notes /2_simple_programs
last modified Thu February 25 2021 11:53 am

attachments [paper clip]

  last modified size
TXT double.py Thu Feb 25 2021 11:37 am 230B