Fall 2020
-->

# chapter 2 : writing simple programs

These notes cover

• some "how to" notes
• notes on programming languages - where does python fit in?
• the material in chapter 2 of our textbook, "writing simple programs"
• a bit of other python-specific tricks.

## running python

There are a number of ways to run python, including

• the terminal interactive prompt: type "python" or "python3" to get >>> (either at jupyter.bennington or on you laptop or the lab computers)
• the terminal prompt: type "python filename.py" or "python3 filename.py"
• IDLE (python.org's all-in-one python edit & run app, if you've installed it on your laptop)
• in a cell jupyter notebook
• from within PyCharm or other IDE (integrated development environment), if you've installed one.
• at pythontutor.com
• ... and so on.

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

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

• source code (including docs, comments, tests - more on that to come)
• some what to show what running running code looks like (cut'n'paste terminal session or screenshot graphics)
• drag'n'drop your files - .pdf's or whatever. (I prefer to .pdf to .doc)

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.

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 :

• imperative (do this, then do that) : C
• object oriented (data abstraction & associated methods): Java
• functional (math-like data flow definitions): Haskell

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

• understand problem (can be harder than you think)
• create specification : inputs? outputs? (be very specific)
• choose algorithm: what is the recipe ?
• implement (write the code)
• test and debug (again, can be harder than you expect)
• maintain (if used over time, needs will usually change)

## variables

variables - named "boxes" to put data in

• legal names : x, yy23 the_data, this_is_a_long_name
• NOT legal : 3x (starts with number), x-y (no dashes), x.y (no periods)
• good names : principal, count, first_name
• not so good names: h (usually short), hpltz (too cryptic)

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()

• What is eval() ?
• What is input() ?
• Can you see how these two are nested inside each other?

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

• blocks and whitespace : Python uses indentation to mark code which is in one block, "underneath" something else. This is a fairly unusual programming style; most languages use explicit grouping symbols such as {} or ().
• loops and flow charts
• eval() is used to convert strings ("1.2") to numbers (1.2) in the text.
• ... which works, and has some flexibility
• ... but has security and perfomance issues.
• There's another usually better way to do this which we'll see in the next chapter.
https://cs.bennington.college /courses /fall2020 /introcs /notes /2_simple_programs