代写 math python shell graph ISYS90088

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 2018 – some slides adapted 1
from Fundamentals of Python by Kenneth A.
ISYS90088 sem 2 2016 – some slides adapted from Fundamentals of Python by Kenneth A. Lambert & local dept. resources
Lambert &local dept. resources

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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 2 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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 3 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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 4 Python by Kenneth A. Lambert & local dept. resources

The Software Development Process (continued)
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of Python by Kenneth A. Lambert & local dept. resources
5

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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 6 Python by Kenneth A. Lambert & local dept. resources

The Software Development Process (continued)
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of Python by Kenneth A. Lambert & local dept. resources
7

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 _
• Therestofthenamecancontainzeroormoreoccurrencesof the following things:
– Digits (0 to 9) .
– Alphabetic letters.
– Underscores.
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of Python by Kenneth A. Lambert & local dept. resources
8

Variables and the Assignment Statement
(continued)
• Nothingelseisallowedinavariablename.Hereis(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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 9 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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 10 Python by Kenneth A. Lambert & local dept. resources

Variables : Example 1
#Which of the following are valid variable names?
a. length
b. _width
c. firstWord
d. 2MoreToGo e. halt!
Solution: ????
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 11 Python by Kenneth A. Lambert & local dept. resources

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….
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 12 Python by Kenneth A. Lambert & local dept. resources

Data Types (continued)
Numeric Data types integers, floating point numbers and their cousins Character sets
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 13 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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of Python by Kenneth A. Lambert & local dept. resources
14

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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 15 Python by Kenneth A. Lambert & local dept. resources

Floating-Point Numbers (continued)
Lets check this out !!!!
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 16 Python by Kenneth A. Lambert & local dept. resources

Revisit the input statement – example
ISYS90088 sem 2 201817

Data types – Character Sets
• Characterliteralsinpythonlooklikestringsandareofstring types
• Theybelongtocharactersets–ASCIIset(128codes) (American Standard Code for Information Interchange)
• ASCIIsetencodeseachkeyboardcharacters
– 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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 18 Python by Kenneth A. Lambert & local dept. resources

Character Sets
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 19 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
Example: if you want to shift three places to the right of the letter ‘A’, simply write:
chr(ord(‘A’) + 3)
20

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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 21 Python by Kenneth A. Lambert & local dept. resources

Data types: Quiz
#Which data type would most appropriately be used to represent the following data values?
a. b. c. d. e.
Thenumberofmonthsinayear Theareaofacircle
The current minimum wage Theapproximateageoftheuniverse(12,000,000,000yrs)
Your name
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 22 Python by Kenneth A. Lambert & local dept. resources

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.
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 23 Python by Kenneth A. Lambert & local dept. resources

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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 24 Python by Kenneth A. Lambert & local dept. resources

Program Comments and Docstrings
Good programming style:
1. Beginaprogramwithastatementofitspurposeandother information that helps the programmer or reader. This includes: what the program does, the authors name, version, date etc…
2. Accompanyavariabledefinitionwithacommentthat explains the variables purpose
3. Precedemajorsegmentsofcodewithcomments
4. Usecommentstoexplaincertaincomplexsegmentsin
your code
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 25 Python by Kenneth A. Lambert & local dept. resources

• Break ( if time permits continue)!
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 26 Python by Kenneth A. Lambert & local dept. resources

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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 27 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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 28 Python by Kenneth A. Lambert & local dept. resources

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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 29 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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 30 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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 31 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.
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 32 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
Note: For multi-line expressions, use a \
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 33 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:
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 34 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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 35 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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 36 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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 37 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 *
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 38 Python by Kenneth A. Lambert & local dept. resources

Mixed-Mode Arithmetic and Type Conversions (continued)
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 39 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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 40 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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 41 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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 42 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:usestrfunction
• Python is a strongly typed programming language
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 43 Python by Kenneth A. Lambert & local dept. resources

Escape Sequences
• The newline character \n is called an escape sequence
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 44 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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 45 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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 46 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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 47 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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 48 Python by Kenneth A. Lambert & local dept. resources