Programming With Python 1: Mathematical Engineering Faculty of Engineering, Design and Computing
Programming With Python 1: Mathematical Engineering Faculty of Engineering, Design and Computing
Mathematical Engineering
2
No exam for Python, but
3
What you learned so far
4
Where to find information on this course.
Where to find information on Python
Getting started with Python
Data types String, Integer and Float
Escape character \
Comments after #
Casting
Operators for numbers and strings
Expressions
Functions and function calls
print()
int()
str()
float()
5
Variables:
value
name
data type
Functions:
naming
return value
parameter/argument
passing by reference vs passing by value
definition and function call
Methods:
called on objects
Modules:
and how to import them
(math, random, pcinput)
6
the module random
random() no parameters
return value pseudo-random float [0.0,1.0>
seed()
see randomseedtest.py
7
import pcinput.py
8
Boolean
9
Boolean values
10
Data types
integer (integer numbers)
float (numbers with decimal point)
string (text)
bool (boolean)
Soft typing
interpreted as False:
False
None (i.e. no value)
0
0.0
“”
Empty mapping (later in the course)
Any function or method call returning
one of the above
12
Comparison operators
13
14
So why is
3 == 3.0 True and 3 == “3” False?
15
in operator
16
Logical operators
t = True
f = False
print( t and t ) Give the truth tables for:
print( t and f ) • and
print( f and t ) • or
print( f and f ) • not
print( t or t )
print( t or f )
Give the truth tables for:
print( f or t )
• (a and b) or c
print( f or f )
• a and (b or c)
print( not t )
print( not f )
17
Lazy evaluation
x=1
y=0
print( (x == 0) or (y == 0) or (x/y == 1) )
print( (x == 0) or (x/y == 1) or (y == 0) )
18
Conditional statements
if <boolean expression1>:
<statements1>
elif <boolean expression2>:
<statements2>
else:
<statements3>
<statements4>
19
#from pcinput import getInteger
import pcinput
NSD
flowchart
20
some nesting
NSD
flowchart
21
or elif
NSD
flowchart
22
????
23
DO NOT USE exit()
(exits the program)
6.3
24
Exercises
chapter 6
25
Iterations
while <boolean expression>
<statements>
26
while <boolean expression>
<statements>
27
num = 1
while num <= 5 :
print( num )
num += 1
print( "Done" )
variables?
data types?
boolean expression?
NSD or flowchart?
What does it do?
28
from pcinput import getInteger
total = 0
count = 0
while count < 5:
total += getInteger( "Please give a number: " )
count += 1
variables?
data types?
boolean expression?
NSD or flowchart?
What does it do?
29
????
number = 1
total = 0
while (number * number)%1000 != 0:
total += number
print( "Total is", total )
variables?
data types?
boolean expression?
NSD or flowchart?
What does it do?
30
????
total = 1.0
while total != 10:
print( total )
total += 0.1
variables?
data types?
boolean expression?
NSD or flowchart?
What does it do?
31
Exercises
Do the exercises of 7.1
32
for <variable> in <collection>
<statements>
33
a string is a collection of letters
fruit = “banana“
for letter in fruit:
print( letter )
print( “Done” )
34
a collection of numbers
for x in range( 10 ):
print( x )
print( “Done” )
range( n )
returns [0,n>
35
a collection of numbers
for x in range( 2, 10 ):
print( x )
print( “Done” )
36
a collection of numbers
37
Loops can be nested as
conditions can be nested as
conditions and loops can be nested
…..
38
WITH LOOPS DO NOT USE
else (to be executed when condition is no longer met)
break (jumps out of the loop)
continue (jumps back to the condition and
continues there)
39
Exercises
Together:
NSD or flowchart for 7.5 and 7.6
40
What you learned today
41
boolean
only values are True and False
some other values are interpreted as False
anything that is not (interpreted as) False is True
boolean expression
comparison operators
in operator
logical operators
and, or, not
lazy evaluation