Lecture 2
Lecture 2
CPU (central processing unit) - the “brain” of the machine, where all the
basic operations are carried out, such as adding two numbers or do logical
operations
Main Memory – stores programs & data. CPU can ONLY directly access info
stored in the main memory, called RAM (Random Access Memory). Main
memory is fast, but volatile.
Secondary Memory – provides more permanent storage
o Hard disk (magnetic)
o Optical discs
o Flash drives
Input Devices – keyboard, mouse, etc
Output Device – monitor, printer, etc
Python is an interpreted language
• Start the Python interpreter in an interactive mode
• comments:
o any text from # through the end of a line
o intended for humans, ignored by the Python
• defining a function called main
• x is variable, used to give a name to a value so that we can refer to later
• The statement starting with for is an example of a loop
o A loop is a device that tells Python to do the same thing over and over
again
o The lines indented underneath the loop heading form the body of the loop
• x = 3.9 * x * (1-x) is an assignment statement: the value on the
right-hand side is computed, and is then stored back (assigned) into the
variable on the left-and side of =.
Topic 2: Writing Simple Programs
Software development process
• Formulate Requirements Figure out exactly what the problem to be solved is
• Determine Specifications Describe exactly what your program will do. What
will it accomplish? What the inputs and outputs of the program?
• Create a Design Formulate the overall structure of the program. How will the
program achieve the desired goals?
• Implement the Design Translate the design into a computer language and put
it into the computer.
• Test/Debug the Program Try out your program and see if it works as
expected. If there are any errors (often called bugs), then you should go back
and fix them. The process of locating and fixing errors is called debugging a
program.
• Maintain the Program Continue developing the program in response to the
needs of your users. Most programs are never really finished; they keep
evolving over years of use.
An example: temperature converter
• Input the temperature in degrees Celsius (call it celsius)
• Calculate fahrenheit = 9/5 * celsius + 32
• Output fahrenheit
Elements of Programs: Names
• Names (also called identifiers): we give names to
– modules (e.g., convert, chaos)
– functions within modules (e.g., main)
– variables (e.g., celsius, fahrenheit)
• Python rules on identifiers
– must begin with a letter or underscore (‘_’), which may be followed by any
sequence of letters, digits, or underscores.
– cannot contain any spaces
– the names that are part of Python, called reserved words, cannot be used
as ordinary identifiers
Elements of Programs: Expressions
• Programs manipulate data. The fragments of code that produce or calculate
new data values are called expressions.
• Using a variable that has not been assigned a value will result in a NameError.
• More complex expressions can be constructed by combining simpler
expressions with operators (e.g., +, -, *, /, **)
• Spaces are irrelevant within an expression. Usually it’s a good idea to place
some spaces in expressions to make them easier to read
• Use parentheses to modify the order of evaluation.
Output statements
The syntax of print:
•These are templates for using print, using notations called meta-languages
•A print statement consists of the keyword print followed by zero or more
expressions, which are separated by commas.
•The angle bracket notation (<>) is used to indicate “slots” that are filled in by other
fragments of Python code. The name inside the brackets indicate what is missing;
expr stands for an expression.
•The ellipses (“...”) indicate an indefinite series (of expressions, in this case). You
don’t actually type the dots.
•The fourth version shows that a print statement may be optionally ended with a
comma.
Output statements
The semantics of print:
•Displays information in textual form, with expression evaluated left to right
•The resulting values are displayed on a single line in a left to right fashion
•A single blank space character is placed between the displayed values
Assignment statements: simple
assignment
• The template for the basic assignment statement:
<variable> = <expr>
where variable is an identifier and expr is an expression
• A variable can be assigned many times. It always retain the value of the most
recent assignment.
Assignment statements: assigning input
• The template for the assigning input:
<variable> = input(<prompt>)
where prompt is an expression that serves to prompt the user for input; this is
almost always a string literal (i.e., some text inside of quotation marks).
Assignment statements: Simultaneous
Assignment
• The template for simultaneous assignment:
<var>, <var>, …, <var> = <expr>, <expr>, …, <expr>
• Python evaluate all the expressions on the right-hand side and then assign
these values to the corresponding variables named on the left-hand side.
Definite Loops
• Programmers use loops to execute a sequence of statements several times in succession.
The simplest kind of loop is called a definite loop. This is a loop that will execute a definite
number of times
– The body of the loop can be any sequence of Python statements. The start and end of
the body is indicated by its indentation under the loop heading
Topic 3: Computing with Numbers
Numeric Data Types
Example output:
Numeric Data Types
• Whole numbers are represented using the integer data type (int for
short).Values of type int can be positive or negative whole numbers.
• Numbers that can have fractional parts are represented as floating point (or
float) values.
• The data type of an object determines what values it can have and what
operations can be performed on it.
• The float type only stores approximations. There is a limit to the precision,
or accuracy, of the stored values. By contrast, the int type is exact.
Numeric Data Types
fact = 1
Examples:
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(5,10)
[5, 6, 7, 8, 9]
>>> range(5,10,3)
[5, 8]
Accumulating Results: Factorial
• n!=n(n-1)(n-2)…(1). Write a program that will compute the factorial of a
number entered by the user.
The limits of int
Handling Large Numbers: Long Ints
• Python provides a better solution for large, exact values in the form of a third
numeric type long int.
• A long int is not a fixed size, but expands to accommodate whatever value it
holds.
• To get a long int, you put an “L” suffix on a numeric literal.
Accumulating Results: Factorial
• n!=n(n-1)(n-2)…(1). Write a program that will compute the factorial of a
number entered by the user.
Type Conversions