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
1
Dr Antonette Mendoza
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 2 Python: First Programs
Recap-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 Python: First Programs
3
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.
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 4 Python: First Programs
Precedence rule for arithmetic operators (continued)
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of Python: First Programs
5
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 6 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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 7 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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 8 Python: First Programs
Example: type conversion
Check example on IDLE =>Using print; input and type conversion
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 9 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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 10 Python: First Programs
help, math modules in python – to help start simple programming
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 11 Python: First Programs
Example – math Module
• Tousearesourcefromamodule,youwritethenameofa module as a qualifier, followed by a dot (.) and the name of the resource
– Example usage with a qualifier – math.pi
• Youmayimportallofamodule’sresourcestousewithoutthe qualifier
– Example: from math import *
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of Python: First Programs
12
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’)))
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 13 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:usestrfunction
• Python is a strongly typed programming language
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 14 Python: First Programs
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 15 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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 16 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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 17 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.
Example – to show!
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 18 Python: First Programs
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 \t \\ \’ \”
new line character horizontal tab
character \
single quotation mark
double quotation mark
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of Python: First Programs
1-19
Escape Characters – some useful ones
Examples:
>>> print (‘mon\ttues\twed’) >>>print(‘mon\ntues\nwed”) >>>print(‘a\\b’) >>>print(‘a\”b’) >>>print(‘\’b’)
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of Python: First Programs
1-20
The Boolean Type, Comparisons, and Boolean Expressions
• Boolean data type consists of two values: true and false (typically through standard True/False)
21
• Example: 4 != 4 evaluates to False
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of Python: First Programs
Logical Operators and Compound Boolean Expressions (continued)
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of Python: First Programs
22
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
23
– not has higher precedence than and and or ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of Python: First Programs
Logical Operators and Compound Boolean Expressions (continued)
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of Python: First Programs
24
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
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of Python: First Programs
25
Fill in the blanks:
Quiz
– 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?????
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 26 Python: First Programs
• Break (if time permits)
ISYS90088 sem 2 2018 – some slides adapted from Fundamentals of 27 Python: First Programs
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
28
The Boolean Type, Comparisons, and Boolean Expressions
• Boolean data type consists of two values: true and false (typically through standard True/False)
• Example:4!=4evaluatestoFalse
ISYS90088 sem 2, 2018 – few slides adapted from Fundamentals of Python: First Programs
29
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 30 Python: First Programs
One-Way Selection Statements • Simplestformofselectionistheifstatement
>>>x = -2 >>>if x < 0:
x = abs(x) >>>print(x)
31
ISYS90088 sem 2, 2018 – few slides adapted from Fundamentals of Python: First Programs
if-else Statements
• Thetwopossibleactionseachconsistofasequenceofstatements
• Eachsequencemustbeindentedatleastonespacebeyondthe symbols if and else.
Syntax:
ISYS90088 sem 2, 2018 – few slides adapted from Fundamentals of Python: First Programs
32
if-else Statements (continued)
must be a Boolean expression
ISYS90088 sem 2, 2018 – few slides adapted from Fundamentals of Python: First Programs
33
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
ISYS90088 sem 2, 2018 – few slides adapted from Fundamentals of Python: First Programs
34