Class 2
The Universal Machine What is a computer program? A detailed, step-by-step set of instructions telling a computer what to do. If we change the program, the computer performs a different set of actions or a different task. The machine stays the same, but the program changes! Python Programming, 3/e
Hardware Basics Python Programming, 3/e
Hardware Basics Fetch-Execute Cycle First instruction retrieved from memory Decode the instruction to see what it represents Appropriate action carried out. Next instruction fetched, decoded, and executed. Lather, rinse, repeat! Python Programming, 3/e
Programming Languages High-level language c = a + b This needs to be translated into machine language that the computer can execute. Compilers convert programs written in a high-level language into the machine language of some computer. Python Programming, 3/e
Programming Languages Interpreters simulate a computer that understands a high-level language. The source program is not translated into machine language all at once. An interpreter analyzes and executes the source code instruction by instruction. Python Programming, 3/e
Review what we saw in lab 1 # This is a comment 2 an integer literal 3.14159 a floating point literal "Syracuse" a string literal
The Python shell prompt In the Python shell >>> The Python shell prompt >>> We type commands here and press the enter key Some statements show results >>> 2+3 5 Some statements do something without producing output. >>> length = 5
Variables Can store values – ints, floats, strings,... Naming rules for identifiers: UPPER case and lower case are different can begin with _underscore or letter followed by a sequence of letters, digits, or underscores Python keywords can't be variable names (p32)
identifiers Legal names: sk8er given_name houseNumber version3 Illegal names: 4kLift month/day day of week "variable" Legal but bad names: O o l
expressions: fragments of code that produce or calculate new data values not and expression: literals age = 15 variables 3+5 "Falk " + "100"
Assignment statement <variable> = <expr> Compute the expression on the right side. Put the result in the variable on the left side. examples: nonexamples: fish = "tuna" "weight" = 20 norm = x*x + y*y 100-37 = change
Operations On numbers. (ints, floats) () ** (exponentiation) * / + - On ints (discuss later) // % On strings +
the Python Shell Interprets commands as we go. Commands go at shell prompts: >>> >>> 2**5 32
the Python Shell Anything in a line after # is a comment, ignored by the computer >>> #Here is a comment >>> 8+45 # Add two numbers 53 >>> 1 + 2 # + 3 3 Why not 6?
the Python Shell remembers values till they are replaced till the shell is “restarted” like a long calculator tape