A computer also moves, arranges, and controls that information (or data). A program is a detailed set of instructions that tells a computer what to do with that data.
use for COMP 200. Based on CodeMirror and Skulpt. www.codeskulptor.org If you want to learn more about using Python with CodeSkulptor after this class, check out the Coursera course “An Introduction to Interactive Programming in Python”! (9/15 – 11/16) https://www.coursera.org/course/ interactivepython
the result to use later. You can keep the same name for a variable, but change the value. Some other things that we can do with variables: Get an index from a string: Do some math: >>> headmaster = “Dumbledore” >>> print headmaster[2] >>> number = 3 >>> print headmaster[number - 2]
“Whoop!” string 42 integer 3.14159 float Python can tell us about types using the type() function: >>> print type(“Whoop!”) <type ‘str’> How would we get Python to output int and float types?
interpreter? When the words ‘True’ and ‘False’ begin with upper case letters, Python knows to treat them like Booleans instead of strings or integers. >>> True >>> False >>> true >>> false >>> type(True) >>> type(“True”)
>>> True and True >>> True and False >>> False and False >>> True or True >>> False or True >>> False or False >>> not True and True >>> not True or True
code with the else clause: “If you’re hungry, let’s eat lunch. Or else we can eat in an hour.” “If you like Frisbee, let’s play! Or else we can play rugby.” >>> if state == “Texas” print “TX” else: print “[inferior state]”
code with the elif clause: “If you like Frisbee, let’s play! Or else we can play rugby. Or else we can play Bioshock, or Half-Life, or Portal…” >>> if name == “Paige” print “Hi Paige!” elif name == “Walker”: print “Hi Walker!” else: print “Imposter!”
over and over again. Counting loops repeat a certain number of times. Conditional loops keep going until a certain thing happens (or as long as some condition is True).
they keep going until they get to the end of a count. >>> for mynum in [1, 2, 3, 4, 5]: print "Hello", mynum Hello 1 Hello 2 Hello 3 Hello 4 Hello 5 The for keyword is used to create this kind of loop, so it is usually just called a for loop.
as some condition is True). >>> count = 0 >>> while (count < 4): print 'The count is:', count count = count + 1 The count is: 0 The count is: 1 The count is: 2 The count is: 3 The while keyword is used to create this kind of loop, so it is usually just called a while loop.
a flavor of ground coffee. 2. Get a coffee maker. 3. Get filter paper. 4. Get a pot of water. 5. Make sure the coffee maker is plugged in… …and on, and on, and on. But to us, it’s just “make a pot of coffee”.
a concise way to group instructions into a bundle. What it's like in our minds: “Make a pot of coffee.” bundle In Python, you could say it like this: make_coffee(coffee_grounds, coffee_pot, water, filter_paper) ^ ^-----------------^---------------^-----------------^ function name function parameters
coffee? Let’s define a function with parameters: >>> def beverage(drink): print “Have you had a cup of ” + drink + “ today?’ Now we’ll call the function: >>> beverage(“Monster Zero”) Have you had a cup of Monster Zero today?
called using parentheses (). Functions take parameters and return outputs. print displays information, but does not give a value. return gives a value to the caller.