Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Fundamentals of Python Programming [Part 2]

The document outlines the software development process, detailing stages such as problem analysis, specification determination, design creation, implementation, testing/debugging, and maintenance. It provides examples of programs, including a temperature converter and average grade calculator, illustrating each stage with pseudocode and Python implementation. Additionally, it covers fundamental programming concepts, including variable assignment, input handling, and loop structures.

Uploaded by

ifeoluwaajibawo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Fundamentals of Python Programming [Part 2]

The document outlines the software development process, detailing stages such as problem analysis, specification determination, design creation, implementation, testing/debugging, and maintenance. It provides examples of programs, including a temperature converter and average grade calculator, illustrating each stage with pseudocode and Python implementation. Additionally, it covers fundamental programming concepts, including variable assignment, input handling, and loop structures.

Uploaded by

ifeoluwaajibawo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

The Software Development

Process
 The process of creating a program is often broken
down into stages according to the information that is
produced in each phase.

Fundamentals of Computer & Programming 2


The Software Development
Process
 Analyze the Problem
 Figure out exactly the problem to be solved. Try to
understand it as much as possible.

Fundamentals of Computer & Programming 3


The Software Development
Process
 Determine Specifications
 Describe exactly what your program will do.
 Don't worry about how the program will work, but
what it will do.
 Includes describing the inputs, outputs, and how
they relate to one another.

Fundamentals of Computer & Programming 4


The Software Development
Process
 Create a Design
 Formulate the overall structure of the program.
 This is where the how of the program gets worked
out.
 Develop your own algorithm that meets the
specifications.
 Algorithm
• a well-defined recipe for solving a problem
o Has a finite number of steps

o Completes in a finite amount of time

Fundamentals of Computer & Programming 5


The Software Development
Process
 Create a Design
 Often referred to as pseudocode
 Pseudocode is a dummy language.
• It has no defined instruction. But it's instructions must be
completely clear.
• It must be completely detailed.
• It is easily convertible to programming languages.

Fundamentals of Computer & Programming 6


The Software Development
Process
 Create a Design
 Flowchart
 A graphical model to represent an algorithm
 Steps are shown with boxes of different shapes
 The flow is specified by arrows

Fundamentals of Computer & Programming 7


The Software Development
Process
 Implement the Design
 Translate the design into a computer language.
 In this course we will use Python.

Fundamentals of Computer & Programming 8


The Software Development
Process
 Test/Debug the Program
 Try out your program to see if it worked.
 If there are any errors (bugs), they need to be
located and fixed. This process is called debugging.
 Your goal is to find errors, so try everything that
might "break" your program!

Fundamentals of Computer & Programming 9


The Software Development
Process
 Maintain the Program
 Continue developing the program in response to the
needs of your users.
 In the real world, most programs are never
completely finished – they evolve over time.

Fundamentals of Computer & Programming 10


Example Program:
Temperature Converter
 Analysis
 the temperature is given in Celsius, user wants it
expressed in degrees Fahrenheit.

 Specification
 Input: temperature in Celsius
 Output: temperature in Fahrenheit
 Relationship: Output = 9/5(input) + 32

Fundamentals of Computer & Programming 11


Example Program:
Temperature Converter
 Design

1.Begin
2.C ← input from user
3.F ← C x 9/5 + 32
4.Output F
5.End

Fundamentals of Computer & Programming 12


Example Program:
Temperature Converter
 Implementation
 Now we need to convert this to Python!

C = eval(input("What is the Celsius temperature? "))


F = (9/5) * C + 32
print("Temperature is ",F," degrees Fahrenheit.")

Fundamentals of Computer & Programming 13


Example Program:
Temperature Converter
 Once we write a program, we should test it!

What is the Celsius temperature? 0


The temperature is 32.0 degrees Fahrenheit.

What is the Celsius temperature? 100


The temperature is 212.0 degrees Fahrenheit.

Fundamentals of Computer & Programming 14


Example Program:
Average of Grades
 Analysis
 Given the grades of N students of a class, the user
wants the average grade.

 Specification
 Input: Grades of students (N grades)
 Output: The average grade
1
 Process: Average = 𝑁 σ𝑁𝑖=1 𝐺𝑖

Fundamentals of Computer & Programming 15


Example Program:
Average of Grades
 Design: Flowchart

Fundamentals of Computer & Programming 16


Example Program:
Average of Grades
 Design: pseudocode
1.Begin
2.N ← input from user
3.i← 0
4.Sum← 0
5.G ← input from user
6.Sum ← Sum + G
7.i← i+ 1
8.If i< N then go to step 5
9.Average ← Sum / N
10.Output Average
11.End

Fundamentals of Computer & Programming 17


Example Program:
Average of Grades
 Implementation

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)

Fundamentals of Computer & Programming 18


Example Program:
Average of Grades
 Test

number of students:5
next grade:11
next grade:12
next grade:13
next grade:14
next grade:15
13.0

Fundamentals of Computer & Programming 19


Some Simple Problem
Objectives
 Write a program that asks the user for their name
and greets them with their name.
 The user wants the summation of 1 to 10.
 Given a number N, the user wants the average and
summation of odd numbers less or equal to N.
 Given a number, the user wants to know if it is prime
or not.

Fundamentals of Computer & Programming 20


Some Simple Problem
Objectives

Fundamentals of Computer & Programming 21


Some Simple Problem
Objectives

Fundamentals of Computer & Programming 22


Elements of Programs
 <variable> = <value>
 Names
 Names are given to variables (C, F,Sum,i), etc.
 These names are called identifiers
 Every identifier must begin with a letter or
underscore ("_"), followed by any sequence of
letters, digits, or underscores.
 Identifiers are case sensitive.

Fundamentals of Computer & Programming 23


Elements of Programs
 These are all different, valid names
• X
• Celsius
• Spam
• spam
• spAm
• Spam_and_Eggs
• Spam_And_Eggs
• _spam245
• __10
 Not valid
• 44spAm

Fundamentals of Computer & Programming 24


Elements of Programs
 Some identifiers are part of Python itself. These
identifiers are known as reserved words (or
keywords).
 This means they are not available for you to use as
a name for a variable, etc. in your program.
 and, del, for, is, raise, assert,
elif, in, etc.
 For a complete list, see Table 2.1 (p. 32)

Fundamentals of Computer & Programming 25


Elements of Programs

Fundamentals of Computer & Programming 26


Elements of Programs
 Expressions
 The fragments of code that produce or calculate
new data values are called expressions.
 Literals are used to represent a specific value, e.g.
3.9, 1, 1.0, 32
 Simple identifiers can also be expressions.
 Also included are strings (textual data) and string
literals (like "Hello", "32").

Fundamentals of Computer & Programming 27


Elements of Programs
>>> x = 5
>>> x
5
>>> print(x)
5
>>> print(spam)
Traceback (most recent call last):
File "<pyshell#15>", line 1, in -toplevel-
print spam
NameError: name 'spam' is not defined
>>>

 NameError is the error when you try to use a variable without a


value assigned to it.

Fundamentals of Computer & Programming 28


Elements of Programs
 Simpler expressions can be combined using
operators.
 +, -, *, /, **
 Spaces are irrelevant within an expression.
 The normal mathematical precedence applies.
 ((x1 – x2) / 2*n) + (spam / k**3)

 >>> "Bat" + "man"


'Batman'

Fundamentals of Computer & Programming 29


Elements of Programs
 Output Statements
 print()
 print(<expr>, <expr>, …, <expr>)
 A print statement can print any number of
expressions.
 Successive print statements will display on separate
lines.
 A bare print will print a blank line.

Fundamentals of Computer & Programming 30


Elements of Programs

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

Fundamentals of Computer & Programming 31


Assignment Statements
 Simple Assignment

 <variable> = <expr>

 variable is an identifier, expr is an expression


 The expression on the RHS is evaluated to produce a
value which is then associated with the variable
named on the LHS.

Fundamentals of Computer & Programming 32


Assignment Statements
x = 5
celsius = 20
x = 3.9 * x * (1-x)
fahrenheit = 9/5 * celsius + 32
y = x ** fahrenheit
z1 = "first"
z2 = "last"
z = z1 + z2

Fundamentals of Computer & Programming 33


Assignment Statements
 Variables can be reassigned as many times as you
want!
>>> myVar = 0
>>> myVar
0
>>> myVar = 7
>>> myVar
7
>>> myVar = myVar + 1
>>> myVar
8
>>> myVar = "something"
>>> myVar
"something"

Fundamentals of Computer & Programming 34


Assignment Statements
 ''variable as a box" is not right.
 Variables are like a box we can put values in.
 When a variable changes, the old value is erased
and a new one is written in.

Fundamentals of Computer & Programming 35


Assignment Statements
 Technically, this model of assignment is simplistic for
Python.
 Python doesn't overwrite these memory locations
(boxes).
 Assigning a variable is more like putting a "sticky
note" on a value and saying, "this is x".

Fundamentals of Computer & Programming 36


Assigning Input
 The purpose of an input statement is to get input
from the user and store it into a variable.

 <variable> = input(<prompt>)

 First the prompt is printed


 The input part waits for the user to enter a value
and press <enter>
 The value is assigned to the variable as a string.

Fundamentals of Computer & Programming 37


Assigning Input
>>> name = input ("Enter your name: ")
Enter your name: John Yaya
>>> name
'John Yaya'
>>>

>>> ans = input ("Enter an expression: ")


Enter an expression: 3 + 4 * 5
>>> print (ans)
'3 + 4 * 5'
>>>

Fundamentals of Computer & Programming 38


Assigning Input
 <variable> = eval(input(<prompt>))

 The expression that was entered is evaluated to


turn it from a string of characters into a Python value
(a number).

>>> ans = eval (input ("Enter an expression: ") )


Enter an expression: 3 + 4 * 5
>>> print (ans)
23

Fundamentals of Computer & Programming 39


Simultaneous Assignment
 Several values can be calculated at the same time
 <var>, <var>, … = <expr>, <expr>, …
 Evaluate the expressions in the RHS and assign them
to the variables on the LHS

x, y = 2, 4
sum, diff = x+y, x-y

Fundamentals of Computer & Programming 40


Simultaneous Assignment
 How could you use this to swap the values for x and
y?
x = y
y = x

 Why doesn't this work?


# variables x=2, y=4
x = y
# now x=4, y=4
y = x
# final x=4, y=4 , wrong solution

Fundamentals of Computer & Programming 41


Simultaneous Assignment
 We could use a temporary variable…
temp = x
x = y
y = temp
 Why does this work?
# variables x y temp
# initial values 2 4 no value yet
temp = x
# 2 4 2
x = y
# 4 4 2
y = temp
# 4 2 2

Fundamentals of Computer & Programming 42


Simultaneous Assignment
 We can swap the values of two variables quite easily
in Python!
x, y = y, x

>>> 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

spam, eggs = eval(input("Enter num of slices of spam


followed by num of eggs:"))
print ("You ordered", eggs, "eggs and", spam, "slices of
spam. Yum!")

Enter num of slices of spam followed by num of eggs: 3, 2


You ordered 2 eggs and 3 slices of spam. Yum!

Fundamentals of Computer & Programming 44


Definite Loops
 A definite loop executes a definite number of times,
i.e., at the time Python starts the loop it knows
exactly how many iterations to do.
 The beginning and end of the body are indicated by
indentation.

for <var> in <sequence>:


<body>

Fundamentals of Computer & Programming 45


Definite Loops
for <var> in <sequence>:
<body>

 The variable after the for is called the loop index. It


takes on each successive value in sequence.
 Often, the sequence portion consists of a list of
values.
 A list is a sequence of expressions in square
brackets.

Fundamentals of Computer & Programming 46


Definite Loops
>>>for i in [2.5, 4, -1, 3]:
print(i)
2.5
4
-1
3
>>>for odd in [1, 3, 5, 7]:
odd = 2 * odd
print(odd*odd)
4
36
100
196
Fundamentals of Computer & Programming 47
Definite Loops
 what did range(10) do?

>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

 range is a built-in Python function that generates a


sequence of numbers, starting with 0.
 The body of the loop executes 10 times.

Fundamentals of Computer & Programming 48


Definite Loops
count = 0
for i in range(7):
print (i)
count = count + 1
print("count =", count)

0
1
2
3
4
5
6
count = 7
Fundamentals of Computer & Programming 49
Definite Loops
for <var> in <sequence>:
<body>

 for loops alter the flow of


program execution, so they
are referred to as control
structures.

Fundamentals of Computer & Programming 50


Question: True/False
 The best way to write a program is to immediately type in
some code and then debug it until it works.
 An algorithm can be written without using a programming
language.
 Keywords make good variable names.
 Python does not allow the input of multiple values with a
single statement.
 A counted loop is designed to iterate a specific number of
times.
 In a flowchart, diamonds are used to show statement
sequences, and rectangles are used for decision points.

Fundamentals of Computer & Programming 51


Question: Output?
print ("start")
for i in range (0):
print ("Hello")
print ("end")

IndentationError: expected an indented block

Fundamentals of Computer & Programming 52


Question: Output?
print ("start")
for i in range (0):
print ("Hello")
print ("end")

start
end

Fundamentals of Computer & Programming 53


Question: Output?
print ("start")
for i in range (3):
print ("Hello")
print ("end")

start
Hello
Hello
Hello
end

Fundamentals of Computer & Programming 54

You might also like