ISYS90088
Introduction to Application
Development
Week 2 – continue from week01; Software
Development Life Cycle
Variable, data types – integers and floating point
numbers, construct arithmetic expressions
Initialize and use variables
And others (if time permits)
Semester 2 , 2018
Dr Antonette Mendoza
ISYS90088 sem 2 2016 – some slides adapted from Fundamentals of Python by Kenneth A.
Lambert &local dept. resources
1 ISYS90088 sem 2 2018 – some slides adapted
from Fundamentals of Python by Kenneth A.
Lambert & local dept. resources
apple
Sticky Note
no hurdle questions
Objectives
After completing this Lecture, you will be able to:
• Describe the basic phases of software development:
analysis, design, coding, and testing
• Variables, data types – use integers and floating point
numbers in arithmetic operations
• Construct arithmetic expressions
• Initialize and use variables with appropriate names
• Use strings for the terminal input and output of text
2 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
Objectives (continued)
• Construct a simple Python program that performs inputs,
calculations, and outputs
• Commenting – use docstrings to document Python
programs
• Import functions from library modules – Math
3 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
The Software Development Process
• Software development: process of planning and organizing
a program
– Several approaches; one is the waterfall model
• Another is – incremental and iterative/agile methodology
– Analysis and design may produce a prototype of a system for
coding, and then back up to earlier phases to fill in more
details after some testing
4 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
5
The Software Development Process
(continued)
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
apple
Sticky Note
put all small cases together to get the final results
The Software Development Process
(continued)
• Programs rarely work as hoped the first time they are run
– Must perform extensive and careful testing
– The cost of developing software is not spread equally over
the phases
6 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
The Software Development Process
(continued)
7 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
apple
Highlight
Variables and the Assignment Statement
• A variable associates a name with a value
– Makes it easy to remember and use later in program
• Variable naming rules:
– Reserved words cannot be used as variable names
• Examples: if, for, while and import
– Name must begin with a letter or underscore _
• The rest of the name can contain zero or more occurrences of
the following things:
– Digits (0 to 9) .
– Alphabetic letters.
– Underscores.
– Names are case sensitive
• Example: WEIGHT is different from weight
– Tip: use “camel casing” (Example: interestRate)
8 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of Python by Kenneth A.
Lambert & local dept. resources
apple
Sticky Note
a place holder that holds the value
like x = 2
the value 2 is assigned to variable x
variable name shouldn’t be reserve words like for, if. while, import
apple
Highlight
apple
Sticky Note
Variables and the Assignment Statement
(continued)
• Nothing else is allowed in a variable name. Here is (incomplete)
list of the things you can’t use anywhere in a variable name:
– Whitespace characters (the space, tab, new line).
– Hyphens (-)
– Quotation marks (single or double)
– Symbol characters such as: the question mark, exclamation
mark, brackets, and so forth.
• The following words having special meaning in Python
(reserve words), and so they cannot be used for variable
Examples:
def, if, for, while, else, except, return, True, range, list, continue
9 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
Variables and the Assignment Statement
(continued)
• Variables receive initial values and can be reset to new
values with an assignment statement
Syntax:
– Subsequent uses of the variable name in expressions are
known as variable references
10 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
apple
Highlight
Variables : Example 1
#Which of the following are valid variable names?
a. length
b. _width
c. firstWord
d. 2MoreToGo
e. halt!
Solution: ????
11 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
apple
Highlight
apple
Highlight
apple
Highlight
Data Types
• A data type consists of a set of values and a set of
operations that can be performed on those values
• A literal is the way a value of a data type looks to a
programmer eg; 4 5 0.5 “hi” etc….
12 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
apple
Sticky Note
integer number
float (decimal) number
character
words-string
apple
Highlight
apple
Highlight
apple
Sticky Note
add; subtract; multiple and division etc
apple
Sticky Note
4 is a literal, which is data type integer
hello is a literal, which is data type string
Data Types (continued)
Numeric Data types integers, floating point numbers and
their cousins Character sets
13 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
Data Types – Integers
• Integers include 0, all of the positive whole numbers, all of
the negative whole numbers
• Integer literals in Python are written without commas
• A leading negative sign indicates a negative value in
python
• A computer’s memory places a limit on magnitude of the
largest positive and negative integers – Python’s int typical
range: –231 to 231 – 1 ie., (-2147483648 to 2147483647)
• A long integer is quite large. But still limited to your
computers memory capacity.
– Try evaluating: 2147483647 ** 100
14 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of Python by Kenneth A.
Lambert & local dept. resources
apple
Highlight
apple
Highlight
apple
Highlight
apple
Sticky Note
exponrntial in python
Data types – Floating-Point Numbers
• A real number consists of a whole number, a decimal point
and fractional part.
• Python uses floating-point numbers to represent real
numbers.
• Python’s float typical range: –10308 to 10308
• A floating point number can be written using either
ordinary decimal notation or scientific notation
• Scientific notation is usually useful when representing
very large numbers
15 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
apple
Highlight
apple
Highlight
apple
Sticky Note
0.378 = 3.78e-1
more detail on next slides
Floating-Point Numbers (continued)
16
Lets check this out !!!!
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
apple
Sticky Note
if we move decimal point to left, its positive, otherwise its negative
Revisit the input statement – example
ISYS90088 sem 2 2018
17
apple
Sticky Note
input statement: when the value is not a certain value
python coding
num1 = 2
num2 = 3
z = num1 +num2
print (z)
firstname= input (“enter your name”)
print (firstname)
num1 = input (“enter a value”)
num2 = input (“enter another value”)
z = num1 +num2
print (z), which doesnt work as the string cannot add together
correction:
num1 =int(input (“enter a value”))
num2 = int(input (“enter another value”))
z = num1 +num2
print (z) or
num1 =float(input (“enter a value”))
num2 = int(input (“enter another value”))
z = num1 +num2
print (z)
Data types – Character Sets
• Character literals in python look like strings and are of string
types
• They belong to character sets – ASCII set (128 codes)
(American Standard Code for Information Interchange)
• ASCII set encodes each keyboard characters
– The digits in the left column represent the leftmost digits of
the ASCII Code.
– The digit in the top row are the rightmost digits.
– ASCII code for ‘R’ = 82
18 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
Character Sets
19 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
Character Sets (continued)
• In Python, character literals look just like string literals and
are of the string type
– They belong to several different character sets, among them
the ASCII set
– ASCII character set maps to set of integers
• ord and chr convert characters to and from ASCII
20
Example: if you want to shift three places to the right of the letter ‘A’, simply write:
chr(ord(‘A’) + 3)
apple
Highlight
Data types: Quiz
#Write the values of the following floating point numbers in
Python’s scientific notation:
a. 355.76
b. 0.007832
c. 4.3212
21 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
apple
Sticky Note
3.5576e2
7.832e-3
43212e-4
Data types: Quiz
#Which data type would most appropriately be used to
represent the following data values?
a. The number of months in a year
b. The area of a circle
c. The current minimum wage
d. The approximate age of the universe (12,000,000,000 yrs)
e. Your name
22 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
apple
Sticky Note
a. int
b. float
c. float
d. int
e string
apple
Sticky Note
Program Comments and Docstrings
• What is a program comment?
A comment is a piece of program text that the interpreter
ignores but that provides useful documentation to
programmers.
• Why is it necessary? A good programming style
– Readability
– Re-use
Docstring: It is a multi-line string that briefly describes parts
of the program.
• End-of-line comment example:
23 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
apple
Highlight
apple
Sticky Note
like
#declare the data
#display the solution
apple
Sticky Note
” ” ”
Program Comments and Docstrings
• Docstring example:
• End-of-line comment: these comments begin with a #
symbol and extend to the end of a line.
– May explain the purpose of a variable or a strategy used in a
piece of code.
• End-of-line comment example:
>>> rate = 4.5 # this variable provides a constant value
24 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
Program Comments and Docstrings
Good programming style:
1. Begin a program with a statement of its purpose and other
information that helps the programmer or reader. This
includes: what the program does, the authors name,
version, date etc…
2. Accompany a variable definition with a comment that
explains the variables purpose
3. Precede major segments of code with comments
4. Use comments to explain certain complex segments in
your code
25 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
• Break ( if time permits continue)!
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
26
Expressions
• Expressions provide easy way to perform operations on
data values to produce other values
• When entered at Python shell prompt:
– an expression’s operands are evaluated
– its operator is then applied to these values to compute the
value of the expression
27 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
Arithmetic Expressions
• An arithmetic expression consists of operands and
operators combined in a manner that is already familiar to
you from learning algebra
28 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
apple
Sticky Note
商
20//3 is 6
7//2 is 3
apple
Sticky Note
30&4 is 2
Arithmetic Expressions (continued)
• Precedence rules:
– ** has the highest precedence and is evaluated first
– Unary negation is evaluated next
– *, /, and % are evaluated before + and –
– + and – are evaluated before =
– With two exceptions, operations of equal precedence are left
associative, so they are evaluated from left to right
• Exponentiation (**) and assignment (=) are right associative
(show examples on IDLE)
– You can use () to change the order of evaluation as
parenthesis takes precedence
29 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
Arithmetic Expressions (continued)
Syntax error: set of rules for constructing well formed expressions in a
language (error when an expression or sentence is not well formed).
Semantic error: detected when the action that an expression describes cannot be
carried out, even if the expression is syntactically correct.
Example: 45%0 is a semantic error
30 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
Arithmetic calculations – Questions:
#Let x = 8 and y = 2. Write the values of the following
expressions:
a. x + y * 3
b. (x + y) * 3
c. x ** y
d. x % y
e. x / 12.0
f. x // 6
31 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
Variables and the Assignment Statement –
Question !
#It is typical that you have a first name and a surname. Write a
python program that prints the first name followed by the surname,
making sure that there is a blank space between the first name and
the surname. Your program should store the first name in a variable
called firstName and the surname in a variable secondName. You
may use additional variables for the task.
32 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
Arithmetic Expressions (continued)
• When both operands of an expression are of the same
numeric type, the resulting value is also of that type
• When each operand is of a different type, the resulting
value is of the more general type
– Example: 3 // 4 is 0, whereas 3 / 4.0 is .75
33
Note: For multi-line expressions, use a \
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
Mixed-Mode Arithmetic and Type
Conversions
• Mixed-mode arithmetic involves integers and floating-point
numbers:
• Remember—Python has different operators for quotient
and exact division:
34 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
Using Functions and Modules
• Python includes many useful functions, which are
organized in libraries of code called modules
35 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
Calling Functions: Arguments and
Return Values
(to be taught in detail later in the course)
• A function is chunk of code that can be called by name to
perform a task
• Functions often require arguments or parameters
– Arguments may be optional or required
• When function completes its task, it may return a value
back to the part of the program that called it
36 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
The math Module
• To use a resource from a module, you write the name of a
module as a qualifier, followed by a dot (.) and the name of
the resource
– Example: math.pi
37 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
The math Module (continued)
• You can avoid the use of the qualifier with each
reference by importing the individual resources
• You may import all of a module’s resources to use
without the qualifier
– Example: from math import *
38 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
Mixed-Mode Arithmetic and Type
Conversions (continued)
39 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
Mixed-Mode Arithmetic and Type
Conversions (continued)
• Note that the int function converts a float to an int by
truncation, not by rounding
40 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
String Literals
• A string literal is a sequence of characters enclosed in
single or double quotation marks
• ” and “” represent the empty string
• Use ”’ and “”” for multi-line paragraphs
41 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
String Concatenation
• You can join two or more strings to form a new string
using the concatenation operator +
• The * operator allows you to build a string by repeating
another string a given number of times
42 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
Mixed-Mode Arithmetic and Type
Conversions (continued)
• Type conversion also occurs in the construction of
strings from numbers and other strings
• Solution: use str function
• Python is a strongly typed programming language
43 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
Escape Sequences
• The newline character \n is called an escape sequence
44 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
Finally, program format and structure
• Start with comment with author’s name, purpose of
program, and other relevant information
– In a docstring
• Then, include statements that:
– Import any modules needed by program
– Initialize important variables, suitably commented
– Prompt the user for input data and save the input data in
variables
– Process the inputs to produce the results
– Display the results
45 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
Summary
• Waterfall and agile models describes software
development processes in terms of several phases
• Literals are data values that can appear in program
• The string data type is used to represent text for input and
output
• Escape characters begin with backslash and represent
special characters such as delete key
• A docstring is string enclosed by triple quotation marks
and provides program documentation
46 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
Summary (continued)
• Comments are pieces of code not evaluated by the
interpreter but can be read by programmers to obtain
information about a program
• Variables are names that refer to values
• Some data types: int and float
• Arithmetic operators are used to form arithmetic
expressions
– Operators are ranked in precedence
• Mixed-mode operations involve operands of different
numeric data types
47 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources
Summary (continued)
• A function call consists of a function’s name and its
arguments or parameters
– May return a result value to the caller
• Python is a strongly typed language
• A module is a set of resources
– Can be imported
• A semantic error occurs when the computer cannot
perform the requested operation
• A logic error produces incorrect results
48 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python by Kenneth A. Lambert & local dept. resources