程序代写代做代考 python ISYS90088

ISYS90088
Introduction to Application
Development

Week 3 –
Continued from week 2- arithmetic precedence,

examples for mixed type arithmetic, fundamentals of
strings – concatenation and length, Boolean and logical

operations

If time permits – introduction to if statement

Semester 2 , 2018
Dr Antonette Mendoza

ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of Python by Kenneth A.
Lambert &local dept. resources

1

Objectives

•  Continued from week 2 – arithmetic precedence,
examples for mixed type arithmetic

•  fundamentals of strings – concatenation, and
length,

•  Boolean and logical operations
•  If time permits – introduction to if statement

ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python: First Programs

2

Recap-Arithmetic Expressions

•  An arithmetic expression consists of operands and
operators combined in a manner that is already familiar to
you from learning algebra

3 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python: First Programs

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 equal to (=)
–  With two exceptions, operations of equal precedence are left

associative, so they are evaluated from left to right
•  Exponentiation (**) and assignment (=) are right associative

–  You can use parenthesis ( ) to change the order of evaluation
as parenthesis takes precedence.

4 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python: First Programs

ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of Python: First Programs 5

Precedence rule for arithmetic operators
(continued)

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
6 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of

Python: First Programs

This should
be -9

Arithmetic calculations – Quiz

#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
g.  3 + 4 ** 2//5

7 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python: First Programs

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:

Tip:
–  Use exact division
–  Use a type conversion function with variables

8 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python: First Programs

Example: type conversion

Check example on IDLE =>Using print; input and type
conversion

9 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python: First Programs

Using some useful library functions in python

•  Python includes many useful functions, which are
organized in libraries of code called modules
–  What is a function? 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.

Note: Functions will be taught in detail later in the
course

10 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of Python: First Programs

help, math modules in python – to help start
simple programming

11 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python: First Programs

Example – 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 usage with a qualifier – math.pi

•  You may import all of a module’s resources to use without the

qualifier

–  Example: from math import *

12 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of Python: First Programs

Mixed-Mode Arithmetic and Type
Conversions – Examples

>>> int(6.75)

>>>round(6.51)

>>> round(6.75)

>>> float(4)

>>>int(“33”)

>>>str(‘a’ + chr(ord(‘d’)))

13 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python: First Programs

Mixed-Mode Arithmetic and Type
Conversions (continued)

•  Type conversion also occurs in the construction of
strings from numbers and other strings

•  Solution: use str function

•  Python is a strongly typed programming language

14 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python: First Programs

apple
高亮

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

15 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python: First Programs

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

16 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python: First Programs

Quiz

# write the output of the following python statements:
a.  “hell_no”
b.  “hell_no” * 10
c.  “hell_no” + “ “ * 10
d.  (“hell_no” + “ “) * 10

17 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python: First Programs

String length

Using a library function to count the length of a string:
•  String’s length – Number of characters it contains

(0+).
•  len is a library function that allows us to do some

manipulation with strings.

ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python: First Programs

18

Example – to show!

Escape Characters

•  It is a special character that is preceded with a backslash(\)
appearing inside a string literal.

•  When a string literal that contains the escape character is
printed, the escape characters are treated as special
commands that are embedded in the string.
\n new line character
\t horizontal tab

\\ character \
\’ single quotation mark

\” double quotation mark

1-19 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of

Python: First Programs

Escape Characters – some useful ones

Examples:
>>> print (‘mon\ttues\twed’)

>>>print(‘mon\ntues\nwed”)
>>>print(‘a\\b’)

>>>print(‘a\”b’)
>>>print(‘\’b’)

1-20 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python: First Programs

ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of Python: First Programs 21

The Boolean Type, Comparisons, and
Boolean Expressions

•  Boolean data type consists of two values: true and
false (typically through standard True/False)

•  Example: 4 != 4 evaluates to False

ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of Python: First Programs
22

Logical Operators and Compound Boolean
Expressions (continued)

ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of Python: First Programs
23

Logical Operators and Compound Boolean
Expressions (continued)

•  Next example verifies some of the claims made in
the previous truth tables:

•  The logical operators are evaluated after
comparisons but before the assignment operator
–  not has higher precedence than and and or

ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of Python: First Programs 24

Logical Operators and Compound Boolean
Expressions (continued)

ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of Python: First Programs 25

Logical operation evaluation: Example

•  In (A and B), if A is false, then so is the expression,
and there is no need to evaluate B

•  In (A or B), if A is true, then so is the expression, and
there is no need to evaluate B

Quiz
Fill in the blanks:

–  A compound boolean expression created with a the ———–
operator is true only if both of its sub expressions are true:

Is it the or, and, not?????

The ——– operator takes a boolean expression as its operand
and reverses its logical value.

Is it the or, not or and operators?????

26 ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python: First Programs

•  Break (if time permits)

ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of
Python: First Programs

27

ISYS90088 sem 2, 2018 – few slides adapted from Fundamentals of Python: First Programs 28

Selection: if and if-else Statements
•  Selection statements allow a computer to make choices –

based on a condition

•  The if statement is used to create a decision structure, which

allows a program to have more than one path of execution.

•  The if statement causes one or more statements to execute
only when a Boolean expression is true.

•  It is a control structure – a logical design that controls the
order in which a set of statements execute.

ISYS90088 sem 2, 2018 – few slides adapted from Fundamentals of Python: First Programs 29

The Boolean Type, Comparisons, and
Boolean Expressions

•  Boolean data type consists of two values: true and
false (typically through standard True/False)

•  Example: 4 != 4 evaluates to False

ISYS90088 sem 2, 2018 – few slides adapted from Fundamentals of
Python: First Programs

30

The if – else statement

•  The simplest is a one-way selection statement (if)

•  Also called a two-way selection statement (if-else)

•  The condition in the if-else statement must be a Boolean
expression – that is, an expression that evaluates to either true
or false

ISYS90088 sem 2, 2018 – few slides adapted from Fundamentals of Python: First Programs
31

One-Way Selection Statements

•  Simplest form of selection is the if statement

>>>x = -2
>>>if x < 0: x = abs(x) >>>print(x)

ISYS90088 sem 2, 2018 – few slides adapted from Fundamentals of Python: First Programs
32

if-else Statements

•  The two possible actions each consist of a sequence of statements
•  Each sequence must be indented at least one space beyond the

symbols if and else.

Syntax:

ISYS90088 sem 2, 2018 – few slides adapted from Fundamentals of Python: First Programs 33

if-else Statements (continued)
must be a Boolean expression

ISYS90088 sem 2, 2018 – few slides adapted from Fundamentals of Python: First Programs 34

Exercise – if-else Statements (continued)

Write a program to accepts two positive integers and print
out the maximum and the minimum of the two input
numbers – using max and min functions from the math
library