CS150 - Fall 2011 - Class 1
http://xkcd.com/245/
introduction to students
- name
- major (or expected major), year
- CS background
- Why are you taking this course?
- What do you hope to get out of the course?
Discuss with your neighbor...
- What is computer science?
- computer science is more than programming
- study of computers
- solving problems using computers as the tool
- What are computers good at?
- Following well defined, unambiguous set of instructions (algorithms)
- Computers can do the same thing over and over again exactly the same way
- What aren't computers good at?
- Computers can only do what they're told (via the program)
- How have you seen computers/computer science used in science?
Goals of the class
- introduction to computer science
- introduction to programming in Python
- design, code and debug medium-sized programs in Python by the end
- CS as a tool for the sciences
- data processing and analysis
- applications in math, engineering and the sciences
- examine a few other programming languages commonly used in scientific programming
Administrative
- Administrative material
-
http://www.cs.middlebury.edu/~dkauchak/classes/cs150/administrivia.html
- Textbook
- need to get it (either online or at the bookstore)
- Schedule:
- Class M/W with a lab on Friday
- Assignments will generally be due Tue or Wed
- All handouts, labs, assignments, etc. will be posted on the course web site
- Late policy
- Grading
- keep up with the reading
- do the daily homework problems
- Honor_code and collaboration (read the class policy!)
- computer use during class
Style of the course
- interactive!
- ask lots of questions
- participate
- be expected to do group work in class
- You'll be expected to be here in class and try particularly hard not to miss Friday class/labs
- I'll post my notes and examples online. You may still want to write some things down, but you don't have to write every word down
Pacing
- I'm assuming no prior computer science, programming or science background
- The pacing may be a bit slow for some early, but it will get harder
- Some example final projects I've done in the past
- centipede:
http://www.cs.middlebury.edu/~dkauchak/classes/f10/cs051-f10/labs/TP2/TP2.html
- asteroids:
http://www.cs.middlebury.edu/~dkauchak/classes/s10/cs051-s10/labs/TP2/tp2.html
- sokoban in cs101
Wing IDE 101
- We'll be using Wing IDE for this course as our interface into Python
- What is an IDE?
- Integrated Development Environment
- Incorporates a text editor with other tools for running, debugging and navigating through the code
- When it starts, three parts of the window
- the main part is the editor
- the bottom right is the interpreter
- the bottom left has search, etc. which can be useful
Python
- Python is an interpreted language
- you can type commands and get an immediate response back
- many programming languages require you to compile the program first and then run it
- Python makes a great calculator
- the ">>>" is the interpreter prompt where you type statements
- when you hit enter/return, Python executes the statements and give you the answer
- Python has all of the standard mathematical operations
- What operations might you want?
- +, -, *, /
- ** (power or exponentiation)
- % (mod aka remainder)
>>> 4+4
8
>>> 15*20
300
>>> 20/4
5
>>> 10-20
-10
>>> 10+4*2
18
>>> (10+4)*2
28
>>> 2**10
1024
>>> 2**30
1073741824
>>> 2**-2
0.25
>>> 10%4
2
>>> 11 % 7
4
- Python follows the normal operator precedence you're used to for math
- things in parenthesis get evaluated first
- ** is next
- %, * and / next
- + and - last
- What should be the answer for 11/2?
>>> 11/2
5
- Why 5?
- Expressions
- anything that represents a value (e.g. a number) is called an expression
- contrast this with a statement, which tells the computer something to do
- for example, "walk over there" is a statement in English
- All the things we've seen so far have been expressions
- In Python (and many programming languages) all expressions have a type
- 11 is an expression
- 2 is an expression
- what are the types of these expressions?
- they are both numbers, but Python makes a distinction between integers and floating point numbers (i.e. numbers with decimals)
- Does this explain why 11/2 gave us 5?
- Because both 11 and 2 are integers, the result is also an integer
- Python rounds down (i.e. towards -infinity or to the smaller number) when doing integer division
- What should be the answer off 11/-2?
- -6
- We can make a number a float by adding a decimal point
>>> 11.0
11.0
>>> 2.0
2.0
>>> 11.0/2.0
5.5
>>> 11.0/2
5.5
>>> 11/2.0
5.5
- for a given operation, if one of the numbers is a float, then the other is converted to a float and the result will be a float
- what is the result of
- (11/2)*4.0?
- 20.0
- the parenthesis will get evaluated first (11/2) will give us the integer 5
- we'll then do floating point multiplication by 4.0, giving us a float of 20.0
- (11.0/2)*4
- 22.0
bbq party
- you're having a party and you're trying to figure out how many hot dogs to buy. Here's what you know:
- tim isn't a big fan of hot dogs, so he'll only eat 1
- amy generally eats 2
- todd always eats twice as many as amy
- brenda eats one less than todd
- mark eats half as many as brenda, but likes to take an extra on his way home
- try and do this on paper
- 13 (assuming that if someone eats half a hot dog, we still have to count the whole thing)
- how did you do it?
- how could you figure this out in Python?
- might be able to do it, but would require a lot of remembering (or writing things down)
variables
- variables allow us to store things and then use them in other expressions
- a variable is storage for a value
- it holds a value
- we can change it's value
- we change the value of a variable using '='
- changing the value of a variable is a statement. It tells the interpreter to do something, but does not represent a value
tim = 1
amy = 2
todd = 2 * amy
brenda = todd - 1
mark = (brenda+1)/2 + 1
total_hotdogs = tim + amy + todd + brenda + mark
total_hotdogs
- if we typed this into the interpreter, we'd get the answer
- naming variables
- generally you want to give good names to variables (x and y are not unless they represent x and y coordinates)
- variables should be all lowercase
- multiple words should be separated by an '_' (underscore)
- what if you get a text from amy and she now says she's planning on eating 4?
- we'd have to re-enter each of the lines (well except the first one)