2-IntroToProgramming
2-IntroToProgramming
Overview of Programming
What Is a Program?
• Examples:
abs(-5)
max(33, 66)
Library Functions
• A library is a collection of resources, including functions,
that can be imported for use in a program
• Example:
import math
math.sqrt(2)
Variables and Assignment
• Variables are used to name data and other resources
pi = 3.14
34 * 34 * pi
• Example:
import math
3 * math.pi
• Or:
3 * pi
Script Mode
• Longer programs can be edited and saved to a file and then run as
scripts (synonymous with programs)
IDLE editor
Python source code
Python compiler
Byte code
Outputs to user
Terminal-Based Programs
• A terminal allows a user to
– run a program
– view output as text on a screen or in a window
– enter input as text from the keyboard
docstring
import statements
input statements
computation statements
output statements
print(math.pi)
print(math.sqrt(2))
print(pi)
print(pi)
print(sqrt(2))
print(10 + x * y ** 2)
int(3.72) # Returns 3
abs(-5) # Returns 5
c = math.sqrt(a ** 2 + b ** 2)
print('The hypotenuse is', c)
>>> dir(math)
['__doc__', '__file__', '__name__', 'acos', 'asin',
'atan', 'atan2', 'ceil', 'cos', 'cosh', 'degrees', 'e',
'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot',
'ldexp', 'log', 'log10', 'modf', 'pi', 'pow',
'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh']
>>>
>>> dir(math)
['__doc__', '__file__', '__name__', 'acos', 'asin',
'atan', 'atan2', 'ceil', 'cos', 'cosh', 'degrees', 'e',
'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot',
'ldexp', 'log', 'log10', 'modf', 'pi', 'pow',
'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh']
>>> help(math.sqrt)
sqrt(x)
Software development
The Waterfall Model: Trickle-down
Analysis
Design
Implementation
(coding)
Testing
Maintenance
The Waterfall Model:
Back up to earlier phase
Analysis
Design
Implementation
(coding)
Testing
Maintenance
Analysis
Program Pseudocode
Design
requirements algorithms
Program
Testing
code
Program
Maintenance
code