CS30 - Spring 2015 - Class 1
Example code in this lecture
Goals of the class
Course Administrative (
- T/Th class with W lab
- Roughly weekly assignments, often due on Thursday nights
- All material will be posted on the course web page
- We have a discussion board setup on piazza linked from the course web page
- Use this to ask any content-based questions
- Check it regularly (or sign up for e-mail feeds)
- Late Policy
and collaboration (read the policy!)
Other misc. announcements
- Make sure you're registered for a lab section
- We will have our first lab session tomorrow
- First assignment (assignment 0) will be posted later today and due on Thursday
Style of the course
- ask lots of questions
- be expected to do group work in class
- You'll be expected to be here in class and lab
- 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
- I assume no prior computer science, programming or science background
- The pacing may be a bit slow for some early, but it will get harder
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, a number of different parts of the window
- For now (and most of this course), we're only going to be using two main components:
- Python Shell
- Main editor window
- You can rearrange them however you'd like, but I prefer main editor on left and Python Shell on right
- 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 gives you the answer
- Python has all of the standard mathematical operations
- What operations might you want?
- +, -, *, /
- ** (power or exponentiation)
- % (mod aka remainder)
>>> 11 % 7
- What is operator precedence?
- 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?
- Why 5?
- 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
- or, for computers, something like, "print this out" or "draw that on the screen"
- 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
- the "type" of an expression determines how Python interprets and understands an expression
- Python is a "strongly typed" language: every expression in Python has a type
- what are the types of the expressions above?
- 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?
- We can make a number a float by adding a decimal point
- 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
- 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
- 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 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 its value
- we change the value of a variable using '=' (also know as assigning to a variable)
- 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
- When assigning to a variable, Python evaluates what is on the right hand side of the equals sign and then assigns this value to the variable
- if I then type amy = 4, what happens to todd?
- todd stays the same
- generally you want to give good names to variables (x and y are not good names 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)
programs in Python
- besides interacting with the shell, we can write statements in a file and then run them
- the top window in the Wing IDE allows us to do this
- on many levels, it behaves a lot like a text editor (e.g. Word). You can:
- create new files
- open files
- save files
- edit files
- I've typed in the same code we'd typed into the shell, but now in the text editing section
- I've saved it in a file called bbq.py
- we'll use the extension .py by convention to indicate a Python program
- Anything different from what I'd typed before into the shell?
- In a program (vs. typing it into the shell) if you want to see the value of a variable or an expression, you need to "print" it
- If I didn't include this, I wouldn't get any answer (in fact I wouldn't see anything)
- I've used whitespace
- you can use spaces without affecting how the code executes
- we use blank lines to make the code more understandable
- I've included "comments"
- comments in Python are designated using '#' (the pound sign)
- python ignores everything from the # to the end of the line
- you can put them on lines by themselves
- or if you have short comments, you can add them at the end of a line
- comments are VERY important
- they allow you to communicate in plain English (to others and to yourself when you look at the code later)
- you will be required to put them in your programs for this course
running the code
- With an IDE, not only can you edit code, you can also run it
- the green arrow, runs the program
- when you run a program, you get a brand new shell session (in the python shell)
- Any variables, etc. you may have manually created in the shell window will NO LONGER EXIST
- it executes your program a line at a time from the top
- it's like you typed all of those commands in the shell
- the one difference is that you don't get line-by-line feedback
- for example, if you put 4+4 in a program on a line by itself, you won't see anything
- if you want to see the value of a variable or an expression, you need to "print" it
- if we run this program, we see 13 printed out, like we would expect
What is the syntax of a language (say English)?
- It describes what are valid things to say in that language
- "I like dogs" is syntactically correct (it's a determiner followed by a verb followed by a noun)
- "I dog like" is not syntactically correct (it's a determiner followed by a noun followed by a verb)
- programming languages also have there own syntax
- it describes what is valid in a language
when syntax fails...
- have we seen any examples of syntax so far?
- all of the math operations have implicit syntax
Traceback (most recent call last):
File "<string>", line 1, in <fragment>
invalid syntax: <string>, line 1, pos 3
- this big mess tells us that we had a syntax error
- assignment as well
>>> dave =
Traceback (most recent call last):
File "<string>", line 1, in <fragment>
invalid syntax: <string>, line 1, pos 7
- the nice thing about using the WingIDE is that when you save a file, it checks the syntax and tries (but doesn't always succeed) at highlighting where the syntax error is
- sometimes look at the line right before it if you can't find it on that line
- how did we convert an int to a float?
- another way is to tell the shell you want the value represented by that expression to be a float
- float is a "function". What is a function in mathematics?
- A function in Python
- has a name
- has zero or more parameters
- how many parameters does float have?
- generally does something
- often gives us a value
- what type of value does float return?
- a float :)
- Python has many built-in functions. What are some that might be useful from a math standpoint?
- abs (absolute value)
- int (throws away the decimal part)
- many more...
- Another interesting function: type
- gives you the type of the expression