Fundamentals of Python:: Chapter 2: Software Development, Data Types, and Expressions
Fundamentals of Python:: Chapter 2: Software Development, Data Types, and Expressions
2
Objectives (continued)
• Import functions from library modules
• Call functions with arguments and use returned
values appropriately
• Construct a simple Python program that performs
inputs, calculations, and outputs
• Use docstrings to document Python programs
3
The Software Development Process
• Software development: process of planning and
organizing a program
– Several approaches; one is the waterfall model
• Modern software development is usually
incremental and iterative
– 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
The Software Development Process
(continued)
5
The Software Development Process
(continued)
6
The Software Development Process
(continued)
7
The Software Development Process
(continued)
8
Strings, Assignment, and Comments
9
Data Types
10
Data Types (continued)
11
Escape Sequences
12
String Concatenation
13
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, def, and import
– Name must begin with a letter or _
– Name can contain any number of letters, digits, or _
– Names are case sensitive
• Example: WEIGHT is different from weight
– Tip: use “camel casing” (Example: interestRate)
14
Variables and the Assignment
Statement (continued)
• Programmers use all uppercase letters for
symbolic constants
– Examples: TAX_RATE and STANDARD_DEDUCTION
• Variables receive initial values and can be reset to
new values with an assignment statement
<variable name> = <expression>
– Subsequent uses of the variable name in
expressions are known as variable references
15
Numeric Data Types and Character
Sets
16
Expressions
• A literal evaluates to itself
• A variable reference evaluates to the variable’s
current value
• 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
17
Arithmetic Expressions
• An arithmetic expression consists of operands
and operators combined in a manner that is
already familiar to you from learning algebra
Integer Division
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
• For multi-line expressions, use a \
Tip:
– Use exact division
– Use a type conversion function with variables
24
Calling Functions: Arguments and
Return Values
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
25
Summary
• Waterfall model describes software development
process 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
26
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
27
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
28
Questions???
29
Reference:
Clausen, Dave (2015).Software Development, Data Types, and Expressions
Singapore:
Pearson Education Limited.
30