* getting Python * getting into Python (on your system) * getting out of Python Some things to try right off: print "hello world" print 2+2 print 'hello world' print "hi"+"there" print "hi" + "there" print len("EFF") print [1,2,3] What is programming? * thinking about problems (mathematics, text processing, user interface, networking, ...) in a formal way, so that you can not only implement specs but also develop them * practicing implementation and debugging * learning algorithms * learning system architecture/implementation details for systems which interest you (e.g. if you want to learn C or OS programming, you will need to learn about bytes and binary arithmetic; if you want to program for Unix, you will need to learn about Unix file permissions and pipes; if you ) - different programmers have different amounts of system knowledge - for example, some web programmers who use only PHP or Perl have developed sophisticated web sites but don't know anything about binary arithmetic * becoming proficient with [...] * learning particular languages, libraries, or toolboxes There's also "computer science" * various disciplines of design and analysis at all levels of abstraction * an emphasis on principles which are independent of any language or system (for example, algorithm efficiency, or computability) -- hence the saying that "real computer scientists don't program in anything less portable [i.e. system-independent] than a number two pencil" Python is a very high level language, so that it will hide details of the system from you. Programming is creative and programming is expression, but * people who only _use_ programs may not appreciate this * people who only implement others' specs may not appreciate this (software as a product in a packaged box, as opposed to software in a book or article) abstraction layers the "veil of abstraction" very high level languages Perl, Python, Scheme C assembly languages low level languages machine code "close to the machine" (also the title of a book about the experience and culture of programming by Ellen Ullman -- San Francisco: City Lights Books, 1997) Python is an interpreted language. So you can run source code directly -- using a Python interpreter. Or you can type in commands interactively, or you can give the interpreter a pre-written file of source code. This is a text file which has one command per line. (Many languages have a rule different from "one command per line", e.g. using semicolons to separate statements. The "one command per line" rule fits in with the general way Python separates and groups statements, which will we talk about sometime around the third class.) Here's a longer program, which you should try to customize for yourself (both by adding your own information, and by making any changes which interest you.) #!/usr/bin/python my_name = "Seth" my_birth_year = 1979 my_favored_law = "FOIA" my_disfavored_law = "CDA" # This is a comment, which means that Python ignores it. # So I can include messages for a human being who might be # reading this program, explaining something about how it # works, or just saying "hi", without confusing Python. print "Hello, my name is " + my_name + "." print print "I'm about" print 2001 - my_birth_year print "years old." print print "I like the " + my_favored_law + ", but not the " + my_disfavored_law + "." print print "The law I like has" print len(my_favored_law) print "letters in its acronym, as opposed to" print len(my_disfavored_law) print "letters in the acronym of the one I don't like." For next time (Thursday, December 13) some errors ----------- what happens if you ask Python about len(1776)? how about "I'm " + 6 + " feet tall" how about print something what other kinds of errors can you produce? variables --------- what happens if you say my_name = "Mudd" and later on my_name = "Legion" and then my_name = "Jon Johansen" Is this an error? What does it mean? What does it do? what if you then do my_name = 17 how about length = len(my_name) under various conditions? (if you haven't set my_name, if you've set it to your name, if you've set it to 17) or kelly = 17 print kelly * kelly kellysquare = kelly * kelly print kellysquare + 1 what's the meaning of hello = 1 hello = hello + 1 hello = hello * 2 what will the value of hello be after this? does "hello = hello + 1" make sense to you? (it's a common idiom in many programming languages) case sensitivity ---------------- is "EFF" the same as "eff"? EFF = "a civil liberties organization" print eff eff = "cool" print EFF why does this do what it does? how does it compare to other computer systems you've used? how about Print "Hello" complex numbers --------------- skip this part if you don't remember your high school algebra. (we'll get back to some math stuff at some point.) z = (1+1j) print z*z z = (0+1j) print z*z z = (3-4j) print -z wasn't Guido van Rossum nice to include support for arithmetic with complex numbers in Python? when you learn object-oriented programming, you'll be able to make up your own kinds of numbers and add support for them to Python (e.g. vectors) big numbers ----------- can you get Python to "overflow" by giving it or making it process a number too big to handle? one hint for generating big numbers quickly: x = x * x will square x, or x = x * x * x will cube x. how large a number does Python seem to be willing to handle? How about floating-point numbers (that is, numbers with decimal points)? are the rules different from the rules for whole numbers? a small amount of user interaction ---------------------------------- print "Who are you?" person = raw_input() print "Hi, " + person + "." What are the uses and limitations of this? Can you write a program that uses raw_input() this way to ask the user a question? What happens if you use raw_input() in several different places in your program? (It is less confusing to use raw_input() in programs saved to files on disk, instead of interactively at the Python prompt.) Does it make sense to use raw_input() to ask the user for a number? Could we write a program which asks the user for a number and then says what you get when you square that number (multiply it by itself)? Can you imagine writing a program which makes decisions based on what the user says? Can it be done using only commands and language features that have been described so far?