C++ Chapter 1

Download Report

Transcript C++ Chapter 1

And now for something
completely different . . .
20-Jul-15
AHD c 2010
1
Part 4
More on
Data Processing
and Introducing
Selection
20-Jul-15
AHD c 2010
2
Python
Data Processing and Selection
4.1
4.2
4.3
4.4
4.5
4.6
20-Jul-15
Python Program Structure
Memory Concepts
Arithmetic Expression Operators
Relational and Logical Operators
Selection
De Morgan’s Laws
AHD c 2010
3
Python
Data Processing and Selection
4.1
4.2
4.3
4.4
4.5
4.6
20-Jul-15
Python Program Structure
Memory Concepts
Arithmetic Expression Operators
Relational and Logical Operators
Selection
De Morgan’s Laws
AHD c 2010
4
Python Program Structure
1. Programs are composed of modules
2. Modules contain statements
3. Statements contain expressions
4. Expressions create and process objects
20-Jul-15
AHD c 2010
5
Python
Data Processing and Selection
4.1
4.2
4.3
4.4
4.5
4.6
20-Jul-15
Python Program Structure
Memory Concepts
Arithmetic Expression Operators
Relational and Logical Operators
Selection
De Morgan’s Laws
AHD c 2010
6
Memory Concepts in Python
A variable is a name that refers to a value.
The assignment statement creates new
variables and gives them values:
number1 = input("Enter first number:\n")
20-Jul-15
AHD c 2010
7
Variables refer to Objects
Variable names such as number1 actually refer
to Python objects.
Every object has a data type (e.g. int),
a size (e.g. 4 bytes), a value (e.g. 10) and a
location in the computer's memory. . .
20-Jul-15
AHD c 2010
8
Creation of an object
number1 = input("Enter first number:\n")
"10"
20-Jul-15
Python first creates an
object to hold the userentered string and places
the object into a memory
location
AHD c 2010
9
Creation of an object
number1 = input("Enter first number:\n")
number1
The assignment symbol
(=) then associates the
name number1 with the
newly created object.
"10"
20-Jul-15
AHD c 2010
10
A program cannot change an object's type or location
In Python, a program
cannot change an
object's type or location.
20-Jul-15
AHD c 2010
11
A program cannot change an object's type or location
In Python, a program
cannot change an object's
type or location.
Only with a small selection of object
types can the value of the object be
changed. . .
20-Jul-15
AHD c 2010
12
Python program statements
cannot change the value of a
number or string object.
These objects are said to be
immutable.
20-Jul-15
AHD c 2010
13
Creating a string object
number1 = input("Enter first number:\n")
10 is entered
print (number1, type(number1))
The output is:
10 <class 'str'>
number1
"10"
20-Jul-15
04-01.py
AHD c 2010
14
Converting from one data type to another
number1 = input("Enter first number:\n")
print (number1, type(number1))
The int function creates
a new object to store the
integer value of the
string entered in the first
line.
number1 = int(number1)
number1
"10"
20-Jul-15
10
AHD c 2010
15
Converting from one data type to another
number1 = input("Enter first number:\n")
print (number1, type(number1))
number1 = int(number1)
number1
"10"
20-Jul-15
The new object has a
different address which
is then associated with
the variable name
number1 .
10
AHD c 2010
16
Converting from one data type to another
number1 = input("Enter first number:\n")
print (number1, type(number1))
number1 = int(number1)
print (number1, type(number1))
number1
"10"
20-Jul-15
10
Once an object
no longer has a
reference to it, its
memory is
released.
04-02.py
AHD c 2010
17
Converting from one data type to another
number1 = input("Enter first number:\n")
print (number1, type(number1))
number1 = int(number1)
print (number1, type(number1))
number1
Once an object
no longer has a
reference to it, its
memory is
released.
10
20-Jul-15
AHD c 2010
18
Displaying an objects memory location
number1 = input("Enter first number:\n")
print (number1, type(number1), id(number1))
number1 = int(number1)
print (number1, type(number1) ), id(number1))
A representation of the memory location of an
object can be obtained by using the id function.
04-03.py
20-Jul-15
AHD c 2010
19
Python
Data Processing and Selection
4.1
4.2
4.3
4.4
4.5
4.6
20-Jul-15
Python Program Structure
Memory Concepts
Arithmetic Expression Operators
Relational and Logical Operators
Selection
De Morgan’s Laws
AHD c 2010
20
Numeric Expressions (int)
2 + 4
6 - 4
6 * 3
6 / 3
6 % 3
6 // 3
-5
3**2
20-Jul-15
04-04.py
AHD c 2010
21
Numeric Expressions (float)
2.0 + 4.0
6.0 - 4.0
6.0 * 3.0
6.0 / 3.0
6.0 % 3.0
6.0 // 3.0
-5.0
3.0**2.0
20-Jul-15
AHD c 2010
04-05.py
22
Mixed Numeric Expressions
2 + 4.0
6 - 4.0
6 * 3.0
6 / 3.0
6 % 3.0
6 // 3.0
-5.0
3**2.0
20-Jul-15
04-06.py
AHD c 2010
23
Python
Data Processing and Selection
4.1
4.2
4.3
4.4
4.5
4.6
20-Jul-15
Python Program Structure
Memory Concepts
Arithmetic Expression Operators
Relational and Logical Operators
Selection
De Morgan’s Laws
AHD c 2010
24
Relational operators relate two operands
7
4
4
4
4
4
20-Jul-15
> 10
< 16
== 4
<= 4
>= 4
!= 4
These are Boolean
expressions. The
result of these
expressions is either
true (1) or false (0).
AHD c 2010
25
Boolean expressions result in values true or false
7
4
4
4
4
4
20-Jul-15
> 10
< 16
== 4
<= 4
>= 4
!= 4
04-07.py
AHD c 2010
26
A Boolean Example
number = 10
isPositive = (number > 0)
a boolean expression
04-08.py
20-Jul-15
AHD c 2010
27
A Boolean Example
number = 10
isPositive = (number > 0)
# the value true (1) is
# assigned to isPositive
20-Jul-15
AHD c 2010
28
Combining Boolean expressions
You can combine Boolean expressions. For
example, if you need to know if a person's
age is greater than 21, AND they have a
salary greater than 50 thousand dollars…..
20-Jul-15
AHD c 2010
29
Combining Boolean Expressions with a
Logical Operator (and)
age = 25
salary = 55000
print ((age > 21) and (salary > 50000))
04-09.py
20-Jul-15
AHD c 2010
30
Logical operator: and
(age > 21) and (salary > 50000)
The and is known as a logical operator.
20-Jul-15
AHD c 2010
31
Logical (Boolean) Operators
and
or
not
20-Jul-15
AHD c 2010
32
Truth Tables of Boolean Operators
value of A value of B
false
false
true
true
20-Jul-15
false
true
false
true
AHD c 2010
resulting value of
A and B
false
false
false
true
33
Truth Tables of Boolean Operators
value of A value of B
false
false
true
true
20-Jul-15
false
true
false
true
AHD c 2010
resulting value of
A or B
false
true
true
true
34
Truth Tables of Boolean Operators
20-Jul-15
value of A
resulting value of
not A
false
true
true
false
AHD c 2010
35
When writing boolean expressions
or arithmetic expressions, it is
usually best to indicate the order of
operations by using parentheses
(brackets).
20-Jul-15
AHD c 2010
36
If parentheses are not used in an
expression, the computer will
perform the operations in an order
determined by the precedence
rules. . .
20-Jul-15
AHD c 2010
37
Precedence Rules
lowest
logical or
logical and
logical not
relational operators: <, >, <=, etc
+, *, /, %, //
-x, + x
( ) Note: expressions in parentheses (brackets) are highest
evaluated first.
20-Jul-15
AHD c 2010
38
Python
Data Processing and Selection
4.1
4.2
4.3
4.4
4.5
4.6
20-Jul-15
Python Program Structure
Memory Concepts
Arithmetic Expression Operators
Relational and Logical Operators
Selection
De Morgan’s Laws
AHD c 2010
39
The if statement
x = 'spam'
if x == 'spam':
print ('Hi spam')
else:
print ('not spam')
20-Jul-15
AHD c 2010
The if
statement makes
use of a
Boolean
expression to
decide which
statement(s) to
execute.
40
The if statement
x = 'spam'
if x == 'spam':
print ('Hi spam')
else:
print ('not spam')
20-Jul-15
AHD c 2010
The Boolean
expression in
this example is:
x == 'spam'
The expression
has a value of
true or false (1
or 0).
41
The if statement
x = 'spam'
if x == 'spam':
print ('Hi spam')
else:
print ('not spam')
20-Jul-15
AHD c 2010
The Boolean
expression is
also known as
the condition of
the if statement.
42
The if statement
x = 'spam'
if x == 'spam':
print ('Hi spam')
else:
print ('not spam')
20-Jul-15
AHD c 2010
If the condition
is true, the first
print statement is
executed and the
second one is
skipped.
43
The if statement
x = 'spam'
if x == 'spam':
print ('Hi spam')
else:
print ('not spam')
20-Jul-15
AHD c 2010
If the condition is
false, the first
print statement is
skipped and the
second one is
executed.
44
The if statement
x = 'spam'
if x == 'spam':
print ('Hi spam')
else:
print ('not spam')
20-Jul-15
AHD c 2010
It's possible to
have multiple
statements in the
true or false
sections of an if
statement...
45
The if statement
x = 'spam'
if x == 'spam':
print ('Hi spam')
print ('Hi Anne')
else:
print ('not spam')
print ('bye Anne')
20-Jul-15
AHD c 2010
If the condition
is true, the first
two print
statements are
executed and the
second set are
skipped.
46
The if statement - syntax
x = 'spam'
if x == 'spam':
print ('Hi spam')
print ('Hi Anne')
else:
print ('not spam')
print ('bye Anne')
20-Jul-15
AHD c 2010
The if
statement starts
with the
keyword if
followed by a
Boolean
expression,
followed by a
colon (:).
47
The if statement - syntax
x = 'spam'
if x == 'spam':
print ('Hi spam')
print ('Hi Anne')
else:
print ('not spam')
print ('bye Anne')
20-Jul-15
AHD c 2010
Beneath the if
line, the
statements to be
run if the
condition is true
are entered after
pressing the Tab
key or typing a
few space
characters. 48
The if statement - syntax
x = 'spam'
if x == 'spam':
print ('Hi spam')
print ('Hi Anne')
else:
print ('not spam')
print ('bye Anne')
20-Jul-15
AHD c 2010
The statements
to be run must be
indented to the
same level. It's
recommended to
press the Tab
key before
typing the
statements.
49
The if statement - syntax
x = 'spam'
if x == 'spam':
print ('Hi spam')
print ('Hi Anne')
else:
print ('not spam')
print ('bye Anne')
20-Jul-15
AHD c 2010
The else part of an
if statement is
optional, but if
included, must be
followed by a colon
(:), and then the
indented statement(s)
to be executed if the
condition is false.
50
The if statement
x = 'spam'
if x == 'spam':
print ('Hi spam')
print ('Hi Anne')
else:
print ('not spam')
print ('bye Anne')
20-Jul-15
AHD c 2010
04-10.py
04-11.py
04-12.py
51
The nested if / elif / else statement
score = input("Enter score: ")
score = int(score)
if score >= 80:
04-13.py
grade = 'A'
else:
04-14.py
if score >= 70
grade = 'B'
else:
grade = 'C'
print ("\n\nGrade is: " + grade)
20-Jul-15
AHD c 2010
Nested if/else
statements test
for multiple
cases by placing
if/else
selection
structures inside
other if/else
selection
structures…
52
The nested if statement
score = input("Enter score: ")
score = int(score)
if score >= 80:
grade = 'A'
elif score >= 70:
04-15.py
grade = 'B'
elif score >= 55:
grade = 'C'
elif score >= 50:
grade = 'Pass'
else:
grade = 'Fail'
print ("\n\nGrade is: " + grade)
20-Jul-15
AHD c 2010
Nested if/else
statements can
be written
using an
alternate
if/elif/else
construct.
Program
04-14.py is
exactly
equivalent to
04-15.py.
53
Python
Data Processing and Selection
4.1
4.2
4.3
4.4
4.5
4.6
20-Jul-15
Python Program Structure
Memory Concepts
Arithmetic Expression Operators
Relational and Logical Operators
Selection
De Morgan’s Laws
AHD c 2010
54
De Morgan’s Laws
1. A not
2. A not
is equivalent to an or with two negated inputs.
or is equivalent to an and with two negated inputs.
and
http://www.annedawson.net/DeMorgansLaws.htm
http://en.wikipedia.org/wiki/De_Morgan%27s_law
04-16.py
20-Jul-15
AHD c 2010
55
This presentation uses the following program files:
http://www.annedawson.net/Python3Programs.txt
04-01.py
04-02.py
04-03.py
04-04.py
04-05.py
04-06.py
04-07.py
04-08.py
04-09.py
04-10.py
04-11.py
04-12.py
04-13.py
04-14.py
04-15.py
04-16.py
20-Jul-15
AHD c 2010
56
End of
Python3_Processing_Selection.ppt
20-Jul-15
AHD c 2010
57
Last updated: Friday 15th May 2009, 09:13 PT, AHD
20-Jul-15
AHD c 2010
58