Download presentation
Presentation is loading. Please wait.
1
Class 2
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
3
Hardware Basics Python Programming, 3/e
4
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
5
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
6
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
7
Review what we saw in lab 1
# This is a comment 2 an integer literal a floating point literal "Syracuse" a string literal
8
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 >>> Some statements do something without producing output. >>> length = 5
9
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)
10
identifiers Legal names: sk8er given_name houseNumber version3 Illegal names: 4kLift month/day day of week "variable" Legal but bad names: O o l
11
expressions: fragments of code that produce or calculate new data values
not and expression: literals age = 15 variables 3+5 "Falk " + "100"
12
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 = change
13
Operations On numbers. (ints, floats) () ** (exponentiation) * / + - On ints (discuss later) // % On strings +
14
the Python Shell Interprets commands as we go. Commands go at shell prompts: >>> >>> 2**5 32
15
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 >>> # + 3 3 Why not 6?
16
the Python Shell remembers values till they are replaced
till the shell is “restarted” like a long calculator tape
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.