Intro to
Computer
Science

Fall 2020
course
site
-->

chapter 2 : writing simple programs

These notes cover

running python

There are a number of ways to run python, including

We'll be doing graphics next week, but for now - chapter 2 & 3 in the text - the terminal is all we need. I encourage you to try this stuff yourself at an interactive 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.

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 /fall2020 /introcs /notes /2_simple_programs
last modified Wed September 2 2020 2:07 pm