Fundamentals of Python Programming [Part 2]
Fundamentals of Python Programming [Part 2]
Process
The process of creating a program is often broken
down into stages according to the information that is
produced in each phase.
Specification
Input: temperature in Celsius
Output: temperature in Fahrenheit
Relationship: Output = 9/5(input) + 32
1.Begin
2.C ← input from user
3.F ← C x 9/5 + 32
4.Output F
5.End
Specification
Input: Grades of students (N grades)
Output: The average grade
1
Process: Average = 𝑁 σ𝑁𝑖=1 𝐺𝑖
N = eval(input("number of students:"))
i=0
Sum = 0
for i in range(N):
G = eval(input("next grade:"))
Sum = Sum + G
Average = Sum / N
print(Average)
number of students:5
next grade:11
next grade:12
next grade:13
next grade:14
next grade:15
13.0
Code:
var = -2
nextVar = var * var / 4 Output:
print(3+4) 7
print(3, 4, 3+4) 3 4 7
print()
print("The answer is", 3+4) The answer is 7
print("var is", var,".") var is -2 .
print(nextVar) 1.0
<variable> = <expr>
<variable> = input(<prompt>)
x, y = 2, 4
sum, diff = x+y, x-y
>>> x = 3
>>> y = 4
>>> print(x, y)
3 4
>>> x, y = y, x
>>> print(x, y)
4 3
Fundamentals of Computer & Programming 43
Simultaneous Assignment
We can use this same idea to input multiple variables
from a single input statement!
Use commas to separate the inputs
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
0
1
2
3
4
5
6
count = 7
Fundamentals of Computer & Programming 49
Definite Loops
for <var> in <sequence>:
<body>
start
end
start
Hello
Hello
Hello
end