SlideShare a Scribd company logo
1 of 46
1
Fundamentals of Python: First Programs
Second Edition
Chapter 2
Software Development, Data Types, and Expressions
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use
as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use.
Objectives (1 of 2)
2.1 Describe the basic phases of software development:
analysis, design, coding, and testing
2.2 Use strings for the terminal input and output of text
2.3 Use integers and floating point numbers in arithmetic
operations
2.4 Construct arithmetic expressions
2.5 Initialize and use variables with appropriate names
© 2018 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain
product or service or otherwise on a password-protected website for classroom use.
Objectives (2 of 2)
2.6 Import functions from library modules
2.7 Call functions with arguments and use returned values
appropriately
2.8 Construct a simple Python program that performs
inputs, calculations, and outputs
2.9 Use docstrings to document Python programs
© 2018 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain
product or service or otherwise on a password-protected website for classroom use.
4
The Software Development Process (1 of 4)
• Software development: process of planning and organizing a program
• Several approaches; one is the waterfall model
• Waterfall model phases:
• Customer request
• Analysis
• Design
• Implementation
• Integration
• Maintenance
• 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
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
5
The Software Development Process (2 of 4)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
6
The Software Development Process (3 of 4)
• 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
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
7
The Software Development Process (4 of 4)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
8
Strings, Assignment, and Comments
• Text processing is by far the most common application of computing
• E-mail, text messaging, Web pages, and word processing all rely on and
manipulate data consisting of strings of characters
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
9
Data Types (1 of 2)
• 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
• int and float are numeric data types
• They represent numbers
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
10
Data Types (2 of 2)
Type of Data Python Type Name Example Literals
Integers int −1, 0, 1, 2
Real numbers float −0.55, .3333, 3.14, 6.0
Character strings str “Hi”, “”, ‘A’, “66”
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
11
String Literals (1 of 2)
• In Python, a string literal is a sequence of characters enclosed in single or
double quotation marks
• ‘‘ and ”” represent the empty string
• Double-quoted strings are handy for composing strings that contain single
quotation marks or apostrophes:
>>> “I’m using a single quote in this string!”
“I’m using a single quote in this string!”
>>> print(“I’m using a single quote in this string!”)
I’m using a single quote in this string!
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
12
String Literals (2 of 2)
• Use ‘‘‘ and ”””for multi-line paragraphs
>>> print(“””This very long sentence extends
all the way to the next line.”””)
This very long sentence extends all the way to the next line
• When you evaluate a string in Python without the print function
• You can see the literal for the newline character, n embedded in the result:
>>> “””This very long sentence extends
all the way to the next line.”””
‘This very long sentence extendsnall the way to the next line.’
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
13
Escape Sequences
• The newline character n is called an escape sequence
Escape Sequence Meaning
b Backspace
n Newline
t Horizontal tab
 The  character
’ Single quotation mark
” Double quotation mark
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
14
String Concatenation
• You can join two or more strings to form a new string using the concatenation
operator +
>>> “Hi “ + “there,” + “Ken!”
‘Hi there, Ken!
The * operator allows you to build a string by repeating another string a given
number of times
>>> “ “ * 10 + “Python”
‘Python’
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
15
Variables and the Assignment
Statement (1 of 3)
• 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: begin each word in the variable name with an uppercase, except the first one
(Example: interestRate)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
16
Variables and the Assignment
Statement (2 of 3)
• Programmers use all uppercase letters for symbolic constants (contain values
that the program never changes)
• 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
• Example:
>>> firstName = “Ken”
>>> secondName = “Lambert”
>>> fullName = firstName + “ ” + secondName
>>> fullName
‘Ken Lambert’
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
17
Variables and the Assignment
Statement (3 of 3)
• Variables serves two purposes:
• Help the programmer keep track of data that change over time
• Allow the programmer to refer to a complex piece of information with a simple name
(called abstraction)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
18
Program Comments and Docstrings (1 of 3)
• Program comments
• A piece of program text that the computer ignores but that provides useful
documentation to programmers
• Docstring
• A multi-line string of the form
• Example:
“““
Program: circle.py
Author: Ken Lambert
Last date modified: 10/10/17
The purpose of this program is to compute the area of a circle. The input is an integer or
floating-point number representing the radius of the circle. The output is a floating-point
number labeled as the area of the circle.
”””
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
19
Program Comments and Docstrings (2 of 3)
• End-of-line comments
• Begin with the # symbol and extend to the end of a line
• Might explain the purpose of a variable or the strategy used by a piece of code
• Example:
>>> RATE = 0.85 # Conversion rate for Canadian to US dollars
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
20
Program Comments and Docstrings (3 of 3)
• Tips related to comments and docstrings:
• Begin a program with a statement of purpose and other information helpful to
programmers
• Accompany a variable definition with a comment that explains the variable’s purpose
• Precede major segments of code with brief comments that explain their purpose
• Include comments to explain the workings of complex or tricky sections of code
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
21
Numeric Data Types and Character Sets
• The first applications of computers were to crunch numbers
• The use of numbers in many applications is still very important
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
22
Integers
• In real life, the range of integers is infinite
• A computer’s memory places a limit on the magnitude of the largest positive
and negative integers
• Python’s int typical range:
 
31 31
21 to2 1
• Integer literals are written without commas
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
23
Floating-Point Numbers (1 of 2)
• Real numbers have infinite precision
• The digits in the fractional part can continue forever
• Python uses floating-point numbers to represent real numbers
• Python’s float typical range:
 308 308
10 to10 and
• Typical precision: 16 digits
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
24
Floating-Point Numbers (2 of 2)
• A floating-point number can be written using either ordinary decimal notation
or scientific notation
Decimal Notation Scientific Notation Meaning
3.78 3.78e0
3.78 times 10 to the 0
37.8 3.78e1
3.78 times 10 to the 1
3780.0 3.78e3
3.78 times 10 Cubed
0.378 3.78e−1
3.78 times 10 to the minus 1
0.00378 3.78e−3
3.78 times 10 to the minus 3
0
3.78×10
1
3.78×10
3
3.78×10
1
3.78×10
3
3.78×10
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
25
Character Sets
• 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 and the
Unicode set
• ASCII character set maps to a set of integers
• ord and chr functions convert characters to and from ASCII
• Example:
>>> ord(‘a’)
97
>>> ord(‘A’)
65
>>> chr(65)
‘A’
>>> chr(66)
‘B’
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
26
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
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
27
Arithmetic Expressions (1 of 3)
• An arithmetic expression consists of operands and operators combined in a
manner that is already familiar to you from learning algebra
Operator Meaning Syntax
− Negation −a
** Exponentiation a ** b
* Multiplication a * b
/ Division a / b
// Quotient a // b
% Remainder or modulus a % b
+ Addition a + b
− Subtraction a − b
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
28
Arithmetic Expressions (2 of 3)
• 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
- ** and = are right associative
• You can use () to change the order of evaluation
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
29
Arithmetic Expressions (3 of 3)
• 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 
• Example:
>>> 3 + 4 * 
2 ** 5
131
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
30
Mixed-Mode Arithmetic and Type
Conversions (1 of 4)
• Mixed-mode arithmetic involves integers and floating-point numbers:
• >>> 3.14 * 3 ** 2
• 28.26
• You must use a type conversion function when working with input of numbers
• It is a function with the same name as the data type to which it converts
• Input function returns a string as its value
• You must use the int or float function to convert the string to a number before
performing arithmetic
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
31
Mixed-Mode Arithmetic and Type
Conversions (2 of 4)
Conversion Function Example Use Value Returned
int(<a number or a string>) int(3.77)
int(“33”)
3
33
float(<a number or a string>) float(22) 22.0
str(<any value>) str(99) ‘99’
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
32
Mixed-Mode Arithmetic and Type
Conversions (3 of 4)
• Note that the int function converts a float to an int by truncation, not by
rounding
>>> int(6.75)
6
>>> round(6.75)
7
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
33
Mixed-Mode Arithmetic and Type
Conversions (4 of 4)
• Type conversion also occurs in the construction of strings from numbers and
other strings
>>> profit = 1000.55
>>> print(‘$’ + profit)
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
TypeError: cannot concatenate ‘str’ and ‘float’ objects
• Solution: use str function
>>> print(‘$’ + str(profit))
$1000.55
• Python is a strongly typed programming language
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
34
Using Functions and Modules
• Python includes many useful functions, which are organized in libraries of code
called modules
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
35
Calling Functions: Arguments and Return
Values
• 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
• To learn how to use a function’s arguments, use the help function:
>>> help(round)
Help on built-in function round in module builtin:
round(…)
round(number[, ndigits]) -> floating point number
Round a number to a given precision in decimal digits (default 0 digits).
This returns an int when called with one argument, otherwise the same type as
number, ndigits may be negative.
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
36
The Math Module (1 of 2)
• Functions and other resources are coded in components called modules
• Functions like abs and round from the _builtin_ module are always available to
use
• The math module includes functions that perform basic mathematical
operations
• 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
>>>math.pi
3.1415926535897931
Math.sqrt(2)
1.4142135623730951
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
37
The Math Module (2 of 2)
• You can avoid the use of the qualifier with each reference by importing the
individual resources
>>> from math import pi, sqrt
>>>print(pi, sqrt(2))
3.14159265359 1.41421356237
• You may import all of a module’s resources to use without the qualifier
• Example: from math import *
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
38
The Main Module
• In the case study, earlier in this chapter, we showed how to write
documentation for a Python script
• To differentiate this script from the other modules in a program, we call it the
main module
• Like any module, the main module can be imported
>>> import taxform
Enter the gross income: 120000
Enter the number of dependents: 2
The income tax is $20800.0
• After importing a main module, view its documentation by running the help
function
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
39
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
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
40
Running a Script from a Terminal Command
Prompt (1 of 4)
• A way to run a Python script:
• Open a terminal command prompt window
• Click in the “Type here to search” box, type Command Prompt, and click Command
Prompt in the list
• Navigate or change directories until the prompt shows directory that contains the
Python script
• Run the script by entering:
- python3 scriptname.py
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
41
Running a Script from a Terminal Command
Prompt (2 of 4)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
42
Running a Script from a Terminal Command
Prompt (3 of 4)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
43
Running a Script from a Terminal Command
Prompt (4 of 4)
• Python installations enable you to launch Python scripts by double-clicking the
files from the OS’s file browser
• May require .py file type to be set
• Fly-by-window problem: Window will close automatically
- Solution: Add an input statement at end of script that pauses until the user presses the enter
or return key
Input(“Please press enter or return to quit the program. ”)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
44
Chapter Summary (1 of 3)
• 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
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
45
Chapter Summary (2 of 3)
• 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
• Type conversion functions can be used to convert a value of one type to a value
of another type after input
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
46
Chapter Summary (3 of 3)
• 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
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.

More Related Content

Similar to Software Development, Data Types, and Expressions

9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx
9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx
9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docxsleeperharwell
 
9781337102087 ppt ch10
9781337102087 ppt ch109781337102087 ppt ch10
9781337102087 ppt ch10Terry Yoast
 
9781337102087 ppt ch04
9781337102087 ppt ch049781337102087 ppt ch04
9781337102087 ppt ch04Terry Yoast
 
Intro to Web Design 6e Chapter 5
Intro to Web Design 6e Chapter 5Intro to Web Design 6e Chapter 5
Intro to Web Design 6e Chapter 5Steve Guinan
 
9781337102087 ppt ch08
9781337102087 ppt ch089781337102087 ppt ch08
9781337102087 ppt ch08Terry Yoast
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16Terry Yoast
 
Chapter 6Network Security Devices, Design, and Technology
Chapter 6Network Security Devices, Design, and TechnologyChapter 6Network Security Devices, Design, and Technology
Chapter 6Network Security Devices, Design, and TechnologyDr. Ahmed Al Zaidy
 
Intro to Web Design 6e Chapter 7
Intro to Web Design 6e Chapter 7Intro to Web Design 6e Chapter 7
Intro to Web Design 6e Chapter 7Steve Guinan
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18Terry Yoast
 
Chapter 4 Advanced Cryptography and P K I
Chapter 4 Advanced Cryptography and P K IChapter 4 Advanced Cryptography and P K I
Chapter 4 Advanced Cryptography and P K IDr. Ahmed Al Zaidy
 
Intro to Web Design 6e Chapter 1
Intro to Web Design 6e Chapter 1Intro to Web Design 6e Chapter 1
Intro to Web Design 6e Chapter 1Steve Guinan
 
1WebDesign6EChapter1TheEnvironmentandtheTools.docx
1WebDesign6EChapter1TheEnvironmentandtheTools.docx1WebDesign6EChapter1TheEnvironmentandtheTools.docx
1WebDesign6EChapter1TheEnvironmentandtheTools.docxlorainedeserre
 
Access 2016 module 4 ppt presentation
Access 2016 module 4 ppt presentationAccess 2016 module 4 ppt presentation
Access 2016 module 4 ppt presentationdgdotson
 
Chapter 7 Administering a Secure Network
Chapter 7 Administering a Secure Network Chapter 7 Administering a Secure Network
Chapter 7 Administering a Secure Network Dr. Ahmed Al Zaidy
 
Chapter 13 Vulnerability Assessment and Data Security
Chapter 13 Vulnerability Assessment and Data SecurityChapter 13 Vulnerability Assessment and Data Security
Chapter 13 Vulnerability Assessment and Data SecurityDr. Ahmed Al Zaidy
 
Intro to Web Design 6e Chapter 6
Intro to Web Design 6e Chapter 6Intro to Web Design 6e Chapter 6
Intro to Web Design 6e Chapter 6Steve Guinan
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15Terry Yoast
 

Similar to Software Development, Data Types, and Expressions (20)

9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx
9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx
9781337117562_ppt_ch01.pptxChapter 1An Overview of Compute.docx
 
9781337102087 ppt ch10
9781337102087 ppt ch109781337102087 ppt ch10
9781337102087 ppt ch10
 
Chapter 3 Basic Cryptography
Chapter 3 Basic CryptographyChapter 3 Basic Cryptography
Chapter 3 Basic Cryptography
 
9781337102087 ppt ch04
9781337102087 ppt ch049781337102087 ppt ch04
9781337102087 ppt ch04
 
Intro to Web Design 6e Chapter 5
Intro to Web Design 6e Chapter 5Intro to Web Design 6e Chapter 5
Intro to Web Design 6e Chapter 5
 
9781337102087 ppt ch08
9781337102087 ppt ch089781337102087 ppt ch08
9781337102087 ppt ch08
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16
 
Chapter 6Network Security Devices, Design, and Technology
Chapter 6Network Security Devices, Design, and TechnologyChapter 6Network Security Devices, Design, and Technology
Chapter 6Network Security Devices, Design, and Technology
 
Intro to Web Design 6e Chapter 7
Intro to Web Design 6e Chapter 7Intro to Web Design 6e Chapter 7
Intro to Web Design 6e Chapter 7
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18
 
Chapter 4 Advanced Cryptography and P K I
Chapter 4 Advanced Cryptography and P K IChapter 4 Advanced Cryptography and P K I
Chapter 4 Advanced Cryptography and P K I
 
Sql9e ppt ch08
Sql9e ppt ch08Sql9e ppt ch08
Sql9e ppt ch08
 
Intro to Web Design 6e Chapter 1
Intro to Web Design 6e Chapter 1Intro to Web Design 6e Chapter 1
Intro to Web Design 6e Chapter 1
 
1WebDesign6EChapter1TheEnvironmentandtheTools.docx
1WebDesign6EChapter1TheEnvironmentandtheTools.docx1WebDesign6EChapter1TheEnvironmentandtheTools.docx
1WebDesign6EChapter1TheEnvironmentandtheTools.docx
 
Access 2016 module 4 ppt presentation
Access 2016 module 4 ppt presentationAccess 2016 module 4 ppt presentation
Access 2016 module 4 ppt presentation
 
Chapter 7 Administering a Secure Network
Chapter 7 Administering a Secure Network Chapter 7 Administering a Secure Network
Chapter 7 Administering a Secure Network
 
Whitman_Ch06.pptx
Whitman_Ch06.pptxWhitman_Ch06.pptx
Whitman_Ch06.pptx
 
Chapter 13 Vulnerability Assessment and Data Security
Chapter 13 Vulnerability Assessment and Data SecurityChapter 13 Vulnerability Assessment and Data Security
Chapter 13 Vulnerability Assessment and Data Security
 
Intro to Web Design 6e Chapter 6
Intro to Web Design 6e Chapter 6Intro to Web Design 6e Chapter 6
Intro to Web Design 6e Chapter 6
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15
 

Recently uploaded

Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 

Recently uploaded (20)

Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 

Software Development, Data Types, and Expressions

  • 1. 1 Fundamentals of Python: First Programs Second Edition Chapter 2 Software Development, Data Types, and Expressions © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 2. Objectives (1 of 2) 2.1 Describe the basic phases of software development: analysis, design, coding, and testing 2.2 Use strings for the terminal input and output of text 2.3 Use integers and floating point numbers in arithmetic operations 2.4 Construct arithmetic expressions 2.5 Initialize and use variables with appropriate names © 2018 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 3. Objectives (2 of 2) 2.6 Import functions from library modules 2.7 Call functions with arguments and use returned values appropriately 2.8 Construct a simple Python program that performs inputs, calculations, and outputs 2.9 Use docstrings to document Python programs © 2018 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 4. 4 The Software Development Process (1 of 4) • Software development: process of planning and organizing a program • Several approaches; one is the waterfall model • Waterfall model phases: • Customer request • Analysis • Design • Implementation • Integration • Maintenance • 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 © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 5. 5 The Software Development Process (2 of 4) © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 6. 6 The Software Development Process (3 of 4) • 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 © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 7. 7 The Software Development Process (4 of 4) © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 8. 8 Strings, Assignment, and Comments • Text processing is by far the most common application of computing • E-mail, text messaging, Web pages, and word processing all rely on and manipulate data consisting of strings of characters © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 9. 9 Data Types (1 of 2) • 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 • int and float are numeric data types • They represent numbers © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 10. 10 Data Types (2 of 2) Type of Data Python Type Name Example Literals Integers int −1, 0, 1, 2 Real numbers float −0.55, .3333, 3.14, 6.0 Character strings str “Hi”, “”, ‘A’, “66” © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 11. 11 String Literals (1 of 2) • In Python, a string literal is a sequence of characters enclosed in single or double quotation marks • ‘‘ and ”” represent the empty string • Double-quoted strings are handy for composing strings that contain single quotation marks or apostrophes: >>> “I’m using a single quote in this string!” “I’m using a single quote in this string!” >>> print(“I’m using a single quote in this string!”) I’m using a single quote in this string! © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 12. 12 String Literals (2 of 2) • Use ‘‘‘ and ”””for multi-line paragraphs >>> print(“””This very long sentence extends all the way to the next line.”””) This very long sentence extends all the way to the next line • When you evaluate a string in Python without the print function • You can see the literal for the newline character, n embedded in the result: >>> “””This very long sentence extends all the way to the next line.””” ‘This very long sentence extendsnall the way to the next line.’ © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 13. 13 Escape Sequences • The newline character n is called an escape sequence Escape Sequence Meaning b Backspace n Newline t Horizontal tab The character ’ Single quotation mark ” Double quotation mark © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 14. 14 String Concatenation • You can join two or more strings to form a new string using the concatenation operator + >>> “Hi “ + “there,” + “Ken!” ‘Hi there, Ken! The * operator allows you to build a string by repeating another string a given number of times >>> “ “ * 10 + “Python” ‘Python’ © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 15. 15 Variables and the Assignment Statement (1 of 3) • 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: begin each word in the variable name with an uppercase, except the first one (Example: interestRate) © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 16. 16 Variables and the Assignment Statement (2 of 3) • Programmers use all uppercase letters for symbolic constants (contain values that the program never changes) • 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 • Example: >>> firstName = “Ken” >>> secondName = “Lambert” >>> fullName = firstName + “ ” + secondName >>> fullName ‘Ken Lambert’ © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 17. 17 Variables and the Assignment Statement (3 of 3) • Variables serves two purposes: • Help the programmer keep track of data that change over time • Allow the programmer to refer to a complex piece of information with a simple name (called abstraction) © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 18. 18 Program Comments and Docstrings (1 of 3) • Program comments • A piece of program text that the computer ignores but that provides useful documentation to programmers • Docstring • A multi-line string of the form • Example: “““ Program: circle.py Author: Ken Lambert Last date modified: 10/10/17 The purpose of this program is to compute the area of a circle. The input is an integer or floating-point number representing the radius of the circle. The output is a floating-point number labeled as the area of the circle. ””” © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 19. 19 Program Comments and Docstrings (2 of 3) • End-of-line comments • Begin with the # symbol and extend to the end of a line • Might explain the purpose of a variable or the strategy used by a piece of code • Example: >>> RATE = 0.85 # Conversion rate for Canadian to US dollars © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 20. 20 Program Comments and Docstrings (3 of 3) • Tips related to comments and docstrings: • Begin a program with a statement of purpose and other information helpful to programmers • Accompany a variable definition with a comment that explains the variable’s purpose • Precede major segments of code with brief comments that explain their purpose • Include comments to explain the workings of complex or tricky sections of code © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 21. 21 Numeric Data Types and Character Sets • The first applications of computers were to crunch numbers • The use of numbers in many applications is still very important © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 22. 22 Integers • In real life, the range of integers is infinite • A computer’s memory places a limit on the magnitude of the largest positive and negative integers • Python’s int typical range:   31 31 21 to2 1 • Integer literals are written without commas © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 23. 23 Floating-Point Numbers (1 of 2) • Real numbers have infinite precision • The digits in the fractional part can continue forever • Python uses floating-point numbers to represent real numbers • Python’s float typical range:  308 308 10 to10 and • Typical precision: 16 digits © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 24. 24 Floating-Point Numbers (2 of 2) • A floating-point number can be written using either ordinary decimal notation or scientific notation Decimal Notation Scientific Notation Meaning 3.78 3.78e0 3.78 times 10 to the 0 37.8 3.78e1 3.78 times 10 to the 1 3780.0 3.78e3 3.78 times 10 Cubed 0.378 3.78e−1 3.78 times 10 to the minus 1 0.00378 3.78e−3 3.78 times 10 to the minus 3 0 3.78×10 1 3.78×10 3 3.78×10 1 3.78×10 3 3.78×10 © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 25. 25 Character Sets • 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 and the Unicode set • ASCII character set maps to a set of integers • ord and chr functions convert characters to and from ASCII • Example: >>> ord(‘a’) 97 >>> ord(‘A’) 65 >>> chr(65) ‘A’ >>> chr(66) ‘B’ © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 26. 26 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 © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 27. 27 Arithmetic Expressions (1 of 3) • An arithmetic expression consists of operands and operators combined in a manner that is already familiar to you from learning algebra Operator Meaning Syntax − Negation −a ** Exponentiation a ** b * Multiplication a * b / Division a / b // Quotient a // b % Remainder or modulus a % b + Addition a + b − Subtraction a − b © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 28. 28 Arithmetic Expressions (2 of 3) • 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 - ** and = are right associative • You can use () to change the order of evaluation © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 29. 29 Arithmetic Expressions (3 of 3) • 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 • Example: >>> 3 + 4 * 2 ** 5 131 © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 30. 30 Mixed-Mode Arithmetic and Type Conversions (1 of 4) • Mixed-mode arithmetic involves integers and floating-point numbers: • >>> 3.14 * 3 ** 2 • 28.26 • You must use a type conversion function when working with input of numbers • It is a function with the same name as the data type to which it converts • Input function returns a string as its value • You must use the int or float function to convert the string to a number before performing arithmetic © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 31. 31 Mixed-Mode Arithmetic and Type Conversions (2 of 4) Conversion Function Example Use Value Returned int(<a number or a string>) int(3.77) int(“33”) 3 33 float(<a number or a string>) float(22) 22.0 str(<any value>) str(99) ‘99’ © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 32. 32 Mixed-Mode Arithmetic and Type Conversions (3 of 4) • Note that the int function converts a float to an int by truncation, not by rounding >>> int(6.75) 6 >>> round(6.75) 7 © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 33. 33 Mixed-Mode Arithmetic and Type Conversions (4 of 4) • Type conversion also occurs in the construction of strings from numbers and other strings >>> profit = 1000.55 >>> print(‘$’ + profit) Traceback (most recent call last): File “<stdin>”, line 1, in <module> TypeError: cannot concatenate ‘str’ and ‘float’ objects • Solution: use str function >>> print(‘$’ + str(profit)) $1000.55 • Python is a strongly typed programming language © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 34. 34 Using Functions and Modules • Python includes many useful functions, which are organized in libraries of code called modules © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 35. 35 Calling Functions: Arguments and Return Values • 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 • To learn how to use a function’s arguments, use the help function: >>> help(round) Help on built-in function round in module builtin: round(…) round(number[, ndigits]) -> floating point number Round a number to a given precision in decimal digits (default 0 digits). This returns an int when called with one argument, otherwise the same type as number, ndigits may be negative. © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 36. 36 The Math Module (1 of 2) • Functions and other resources are coded in components called modules • Functions like abs and round from the _builtin_ module are always available to use • The math module includes functions that perform basic mathematical operations • 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 >>>math.pi 3.1415926535897931 Math.sqrt(2) 1.4142135623730951 © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 37. 37 The Math Module (2 of 2) • You can avoid the use of the qualifier with each reference by importing the individual resources >>> from math import pi, sqrt >>>print(pi, sqrt(2)) 3.14159265359 1.41421356237 • You may import all of a module’s resources to use without the qualifier • Example: from math import * © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 38. 38 The Main Module • In the case study, earlier in this chapter, we showed how to write documentation for a Python script • To differentiate this script from the other modules in a program, we call it the main module • Like any module, the main module can be imported >>> import taxform Enter the gross income: 120000 Enter the number of dependents: 2 The income tax is $20800.0 • After importing a main module, view its documentation by running the help function © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 39. 39 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 © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 40. 40 Running a Script from a Terminal Command Prompt (1 of 4) • A way to run a Python script: • Open a terminal command prompt window • Click in the “Type here to search” box, type Command Prompt, and click Command Prompt in the list • Navigate or change directories until the prompt shows directory that contains the Python script • Run the script by entering: - python3 scriptname.py © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 41. 41 Running a Script from a Terminal Command Prompt (2 of 4) © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 42. 42 Running a Script from a Terminal Command Prompt (3 of 4) © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 43. 43 Running a Script from a Terminal Command Prompt (4 of 4) • Python installations enable you to launch Python scripts by double-clicking the files from the OS’s file browser • May require .py file type to be set • Fly-by-window problem: Window will close automatically - Solution: Add an input statement at end of script that pauses until the user presses the enter or return key Input(“Please press enter or return to quit the program. ”) © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 44. 44 Chapter Summary (1 of 3) • 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 © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 45. 45 Chapter Summary (2 of 3) • 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 • Type conversion functions can be used to convert a value of one type to a value of another type after input © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
  • 46. 46 Chapter Summary (3 of 3) • 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 © 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.