0% found this document useful (0 votes)
45 views

Programming With Python 1: Mathematical Engineering Faculty of Engineering, Design and Computing

This document provides an overview of key concepts in Python programming including booleans, comparison operators, logical operators, conditional statements, and iterations. It explains that True and False are the only boolean values, but other values like 0 can be interpreted as False. Comparison operators and the in operator are used to create boolean expressions. Logical operators like and, or, and not are covered along with lazy evaluation. Conditional statements like if, elif, and else are demonstrated. Finally, iterations with while and for loops are presented, emphasizing proper indentation.

Uploaded by

Nick Manshanden
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Programming With Python 1: Mathematical Engineering Faculty of Engineering, Design and Computing

This document provides an overview of key concepts in Python programming including booleans, comparison operators, logical operators, conditional statements, and iterations. It explains that True and False are the only boolean values, but other values like 0 can be interpreted as False. Comparison operators and the in operator are used to create boolean expressions. Logical operators like and, or, and not are covered along with lazy evaluation. Conditional statements like if, elif, and else are demonstrated. Finally, iterations with while and for loops are presented, emphasizing proper indentation.

Uploaded by

Nick Manshanden
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

Faculty of ontwerpen

Techniek, Engineering,enDesign and Computing


informatica

Mathematical Engineering

Programming with Python 1


Booleans and conditions
Control structures

Jan Willem Crabbendam


Margje Penning
Enroll for the exams!!!

and read the mail you received


about the exams

2
No exam for Python, but

next week (…) the first


assessment
(chapters 1 to 8)

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>

randomint(n1,n2) two integer parameters and n1<= n2


return value pseudo random integer [n1,n2]

seed()

see randomseedtest.py

7
import pcinput.py

8
Boolean

9
Boolean values

True and False are the only boolean values

Anything that is not False is True.

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

Anything that is not False is True.


11
Boolean expression
an expression that is evaluated
as True or False

12
Comparison operators

print( "1.", 2 < 5 )


print( "2.", 2 <= 5 )
print( "3.", 3 > 3 )
print( "4.", 3 >= 3 )
print( "5.", 3 == 3.0 )
print( "6.", 3 == "3" )
print( "7.", "syntax" == "syntax" )
print( "8.", "syntax" == "semantics" )
print( "9.", "syntax" == " syntax" )
print( "10.", "Python" != "rubbish" )
print( "11.", "Python" > "Perl" )
print( "12.", "banana" < "orange" )
print( "13.", "banana" < "Orange" )

13
14
So why is
3 == 3.0 True and 3 == “3” False?

“banana” < “orange” True and “banana” < “Orange” 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

x = pcinput.getInteger("Please enter an integer: ")


if x < 0:
print( str(x) + " is indeed a negative")
print( "And some other stuff to be executed if x < 0" )
else:
print( str(x) + " is not < 0")
print( "This is always printed")

NSD
flowchart
20
some nesting

from pcinput import getInteger

x = getInteger("Please enter an integer: ")


if x < 0:
print( str(x) + " is indeed a negative")
print( "And some other stuff to be executed if x < 0" )
else:
if x < 5:
print( str(x) + " is indeed a positive number smaller 5")
print( "And some other stuff to be executed if x a positive smaller 5" )
else:
print( str(x) + " is not < 5")
print( "This is always printed")

NSD
flowchart
21
or elif

from pcinput import getInteger

x = getInteger("Please enter an integer: ")


if x < 0:
print( str(x) + " is indeed a negative")
print( "And some other stuff to be executed if x < 0" )
elif x < 5:
print( str(x) + " is indeed a positive number smaller 5")
print( "And some other stuff to be executed if x a positive smaller 5" )
else:
print( str(x) + " is not < 5")
print( "This is always printed")

NSD
flowchart
22
????

from pcinput import getInteger

age = getInteger(“How old are you? ")


if age < 18:
print( “You are a minor." )
elif age < 3:
print( “You are a toddler." )
elif age < 1:
print( “You are a baby." )
else:
print( "You are an adult.")
print( “And maybe confused now.")

23
DO NOT USE exit()
(exits the program)

6.3

24
Exercises
chapter 6

25
Iterations
while <boolean expression>
<statements>

for <variable> in <collection>


<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

print( "Total is", total )

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” )

function range called with 1 parameter (i.e. 10)


returns {0, 1, 2, 3 …. , 9}

range( n )
returns [0,n>

35
a collection of numbers

for x in range( 2, 10 ):
print( x )
print( “Done” )

function range called with 2 parameter (i.e. 2 and 10)


returns {2, 3 …. , 9}

range( n1, n2)


returns [n1,n2>

36
a collection of numbers

for x in range( 2, 10, 3 ):


print( x )
print( “Done” )

function range called with 3 parameter (i.e. 2, 10 and 3)


returns {2, 5, 8}

range( start, end, step)


returns [start, start + step, start + 2 * step, … , end>

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)

7.3 and 7.5

39
Exercises

Do the exercises of 7.2 … 7.2.5

Together:
NSD or flowchart for 7.5 and 7.6

Now code the


NSD or flowchart for 7.5 and 7.6

Do the exercises with chapter 7

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

conditional statements (if, elif, else)

iterations (while and for loop)

the importance of proper indentation


42

You might also like