Day 2. Plus, beginning of Day 3. Go over syllabus == Python as a calculator == *SHOW THEM* that I put up the python shell on the schedule. Review those quickly, then move on. Python can be used like a calculator. - To try statements one at a time, without writing a whole program, use the "Python Shell". - [Maximize the shell part of Wing.] Basic operators: 11 + 56 # This is a comment. It's meant only for humans. 23 - 52 # The "#" tells Python to ignore the rest of the line. 2 ** 5 # 2 to the power of 5 7 / 3 # The / is division, but where did the extra 1 go? 7 % 3 # The % is the remainder operator About division - with "/" you only get the quotient 8/3 gives you 2 - What if you want "real" division? 8.0 / 3.0 - These are called 'floating point numbers', and they're only approximations; we'll go over these more deeply next week - What if you combine an integer and a floating point number? 8 / 3.0 8.0 / 3 # Both have a floating-point result =====BEGIN DAY 2 Order of operations 4 + 5 * 3 # Python understands precedence (4 + 5) * 3 # Override with parentheses Negative numbers 5 - 2 5 - -2 What if you do something that can't work? 4 / 0 - You'll see lots of messages like that. For now, the last line is the only really important one. - Not all error messages are so useful 4 + Finding out what else you can do with numbers - two functions are really helpful: dir and help - dir: gives you a list of all the functions (and some other things) that are available. Eg: dir(__builtins__) All the functions *ETC* that are built in to basic Python. Ignore the ones with "__". The rest are the ones you can use.] You can use dir on libraries that you import. Eg: dir(media) - help: tells you all the details about using a function or module. Eg help(abs) help(media) help(media.set_green) == Variables and Assignment == Concept - A variable is a name and an associated value. - variables let us hang on to values so we can use them in several places Into shell: x = 45.6 y = 'yes' Draw them as names for boxes. [open numbers.py in the editor] ***NOW: I'll have you go through this in LAB on Friday, so don't worry about trying to take notes on this now. Just try to follow it; the lab will remind you what I did. *** Move stack data to right tool box, so they can see it. Set a breakpoint on line 3 DEBUG - that means run the program, but stop at the red dot, and show me information. See Stack Data? So far, only __...__ variables are defined. The system defines these. Don't worry about them for now. STEP INTO - means do the next step See? "students" is now defined. It is a variable that holds the value 132. [STEP THROUGH THE REST, SHOWING THE VALUES CHANGING AS WE GO] labs needed: 132 / 32 = 4.125. This is doing integer division, so this gives you 4. == Types == - Variables also have a type: the kind of value they are holding. - So far we've seen lots of numbers. - Let's find out their Python types: 7 type(7) 34.64 type(34.64) type(4 ** 1000) x = 7 type(x) 8e+2 # Exponential notation: 8.0 * 10 ** 2 type(8e+2) 8e-2 # 8.0 * 10 ** -2 = 8/100 type(8e-2) - Each type has things you can do with it (*, % etc) - Soon we'll see types that are not numbers, and things you can do with them. Eg "strings" of characters. - sometimes you need to convert to a different type Eg, you have an integer num and you want to do real division. Solution: turn it into a float - there are functions to do type conversion: int(7.0) int(7.99999) # Chops off the fractional part. float(4) x = 7.25 x type(x) y = int(x) y x == Python vs Math == - in math, or in a Python program, we could say p = 5 q = p * 7 But in a programming language, the = is very different. It is assignment, not equality! That is, the thing on the right hand side is evaluated, then the resulting value is placed into the variable. The variable is a box which holds values. p = 5 q = p * 7 [q = 5 * 7 = 35] p = q + 3 [35 + 3 = 38] In math, that would be weird - how could p be both 5 and 38? But in programming, it makes sense. You can change a variable's value as many times as you like. Difference: Equality vs assignment - In math, "p = q + 10" is a stating a fact about the value of p and the value of q + 10: that they are equal. - In python, "p = q + 10" is something completely different: an assignment statement. It asks Python to (1) calculate the value of q + 10, and (2) Change p's value to that. - This is why it makes sense to say x = x + 1 which makes no sense in math! - programming languages usually have different symbols for equality and assignment. Python: t = 3 # Assignment t t == 3 # Equality comparison t == 5 t - Another example, to get a feeling for what programming variables are (rather than math variables) x = 37 y = x + 2 x y x = 20 y # y is 39; x is 20; even though we said y = x + 2 above QU: What does y = x + 2 mean in programming? figure out what x + 2 is, and place that value in location y we were NOT saying that y is always 2 more than x! - In math, these mean the same thing: i = j j = i - In Python, they do not! The first puts j's value into i The second puts i's value into j a = 3 b = 4 sum = a + b # same in python and in math a + b = sum # illegal in Python! Naming variables - Python doesn't know x from my_average from steelers - We can name variables whatever we want Actually, there are a few rules: must start with a letter (or underscore) can include letters, digits and underscores, but not other characters (and case matters, btw) - And we choose meaningful names that are easy to read Eg If you are adding something up, "sum" is better than "x" We'll learn some guidelines for this over the term == Expressions vs Statements == * You could type these into the editor - Consider these in English: "John" "The computer in my office" "The third computer science class I took" Each is an expression Each refers to something (a person named Laureen Harper, etc.) - Contrast with these in English: John ate a hotdog The third computer science class I took was good The computer in my office is a MAC Each is a full sentence, with a subject and a predicate - Python has a similar distinction - Consider these in Python: "max(2,3,4)" "98.6 * 2" Each is an expression Each refers to a value - the value they evaluate to. - Contrast with this in Python: "temperature = 98.6" This is a full Python statement (like a sentence) It is a command to do something - in Python, you normally make full statements. - but the shell lets you just type expressions and it will tell you their value -- acting like a calculator. x = 5 x * x # Shows us 25, but doesn't change x's value x x = x * x # This changes x's value == Producing output in the editor == - The shell prints the value of expressions, as we've seen. - But in the editor, expressions by themselves are pretty worthless. x * x The computer will evaluate the value...and do NOTHING with it. [new program x = 4 x * x save the program breakpoint at the last line debug step into - see, x didn't change! - In the editor, how do you get python to print the value of an expression? "Print" print x * x print 99 - x - QU: What do you think this will print? print "99 - x" The quotes mean print literally this. - Stuff inside quotation marks is called a /string/. Think of it as a string, or sequence, or characters. - Here's something more useful: type it in: print "Hello!" mark1 = 78 mark2 = 91 # Comma is used here for a list of things to print: print "The average is", (mark1 + mark2) / 2.0 This prints the string "the average is" and then the result of evaluating the expression - We'll learn much more about strings in a bit. === BEGIN DAY 3. I'm going to go through slides now: they cover Python basics. Kind of a summary, after the first week. And, a look to our next topic. THESE are slides2.ppt ***Python history Centrum...National research institute for mathematics and CS Audience: so ABC was designed to be usable by non-programmers as much as possible Who knows that Monty Python is? Benevolent - well-meaning and kindly BDFL - he makes final decisions about what features the language will have; he has remained closely involved from the beginning. ***How Python is managed You too can contribute once you understand more programming Python is widely used **** Python types all python values have types. some are built in, like int, float, and str some are defined in modules like media - Picture, Pixel, Color Later in the course, you will learn how to define your own types! **** Sizes in Memory ints and floats have fixed amount of space because of speed and the ease of making the hardware work operations with longs take MUCH longer than floating and int operations **** Function definition This will be our next big topic - defining functions. **** Strings - spaces, punctuation, and everything are fair game in a string - except what? "this is my string" you can't put a " inside it! 'this is my string' you can't put a ' inside it! **** Last slide - you've seen all of these kinds of statements. Next time, we will go on to ....