You are on page 1of 2

CHAPTER 2: WRITING SIMPLE PROGRAMS  This is accomplished using an assignment

statement combined with a built in function


2.1 Process of creating programs: called ‘input’. Eg) name = input(“Enter your
1. Analyze the Problem name: “) etc
2. Determine Specifications – what the outputs & inputs  When user input is text use input(“…”) but
and how they relate when the user input is a number we use
3. Create a Design – how? Algorithm <variable>= eval(input(<prompt>)).
4. Implement the design – translate to computer language  Function ‘eval’ is wrapped around the function
using Python ‘input’
5. Maintain the Program – continue development of
2.5.3 Simultaneous Assignment
program
 Alternate form of the assignment that allows
2.2 Example
us to calculate several values all at the same
 Algorithm follows a standard pattern: Input, Process, time.
Output  <var>, <var>, ……, <var> = <expr>, <expr>, …..,
 Writing algorithm in pseudocode (precise English that <expr> tells Python to evaluate all the
describes what the program does) expressions on the RHS and assign these
values to the respective variables on the LHS.
Input temperature in degrees Celsius (call it Celsius) Eg sum, diff = x+y, x-y
Calculate Fahrenheit as (9/5)Celsius +32
Output Fahrenheit 2.6 Definite Loops

 Simplest kind of loop is definite loop – loop that


2.3 Elements of Programs executes a definite number of times e.g chaos function
loop is always executed 10 times.
2.3.1 Names  The loop in the chaos function is called a counted loop –
using the Python ‘for’ statement.
 Variables are used to given names to values e.g. Celsius,
 Loops alter the “flow of control” in a program –
Fahrenheit
introducing loops causes puython to go back and do
 All these names are call identifiers. Every identifier must
some statement again.
begin with a letter or underscore, no spaces allowed. Eg
 Statements like the for loop are called ‘control
x, Celsius, spam2, __123
structures’ because they control the execution of other
 Reserved words/keywords – identifiers that are already
parts of the program.
included in Python and cannot be used. Eg import, for,
 In flow charts, boxes represent different parts of a
class etc
program and arrows show the sequence when running,
2.3.2 Expressions diamonds represent decision.

 Fragments of code that produce or calculate new data 2.7 Example Program: Future Value – completed in IDLE
values are called expressions. The simplest kind is a
2.9 EXCERCISES
literal.
 Evaluation – process of turning an expression into an Review Questions: True or False
underlying data type.
1. False
2.4 Output Statements 2. True
3. False
 Information can be displayed on screen using Python’s 4. True
built in function ‘print’. 5. True
 Like other languages, Python has precise set of rules for 6. True
the syntax (form) and semantics(meaning) of each 7. True
statement. 8. False
 Print(“The answer is “, end=” “)???? 9. True
10. False
2.5 Assignment Statements
Multiple Choice
2.5.1 Simple Assignment
1. C – Fee Setting
 Form is <variable> = <expr> eg. X=5,
2. A
Fahrenheit = 9/5*Celsius+32
3. D – Specification
 Process of automatic memory management is
4. C
called garbage collection.
5. B
2.5.2 Assignment Input 6. D – Assignment statements: Fragments of code that
produce or calculate new data values.
 Purpose of input statement is to get 7. B
information from user of program and store it 8. D
into variable 9. B : variable-as-box – most accurate model of assignment
in python???
10. D

Discussion

1. The 6 steps in software development


i.) Problem analysis – studying the problem to be
solved
ii.) Program specification – deciding exactly what
the program will do
iii.) Design – writing an algorithm in pseudocode
iv.) Implementation – translating into
programming language
v.) Testing/debugging – fixing errors
vi.) Maintenance – keep program up to
date/evolve

2. #File: chaos.py
#A simple program illustrating chaotic behaviour.

def main():
print("This program illustrates a chaotic function.")
x = eval(input("Enter a number between 0
and 1 : "))
for i in range(10):
x = 3.9*x*(1-x)
print(x)
main()
3. Definite loop – executes a known number of times,
python for statement is a definite loop that iterates
through a sequence of values, a python ‘list’ if often
used in a for loop to provide a sequence of values for
the loop. Counted Loop – loop designed specifically for
the purpose of repeating some portion of the program a
specific number of times, created by using the built in
range function to produce a suitably sized list of
number. For loop – python built in function.
4. Show the output from the following fragments.
5. It is important to see how the program will work (IPO)
prior to coding.
6. Python ‘print’ function supports other keyword
parameters besides ‘end’ like ‘sep’ – the separator used
between multiple values when printing. ?????

You might also like