ICT582 Topic 02
ICT582 Topic 02
LECTURE 2 3
STRINGS
§ A string contains a sequence of letters, special characters,
spaces, digits enclosed in single quotes or double quotes
hi = 'hello there'
Py = "Python is fun"
LECTURE 2 5
INPUT/OUTPUT: print
§ Write a program that takes the unit code and total mark in
that unit as input and prints following customized message.
unit = input("Type in your unit code: ")
mark = input("Type in your mark: ")
print("Your unit code is " + unit + "and your mark is " + mark)
LECTURE 2 8
COMPARISON OPERATORS ON
int, float, string
A B A and B A or B
pset_time = 15
sleep_time = 8
print(sleep_time > pset_time)
drive = True
drink = False
both = drink and drive
print(both)
LECTURE 2 1
1
CONDITION AND BRANCHING
LECTURE 2 1
2
CONTROL FLOW - BRANCHING
if <condition>: if <condition>:
<statement> <statement>
<statement> <statement>
... ...
elif <condition>:
if <condition>: <statement>
<statement> <statement>
<statement> ...
... else:
else: <statement>
<statement> <statement>
<statement> ...
§ matters in Python
§ how you denote blocks of code
x = float(input("Enter a number for x: "))
y = float(input("Enter a number for y: "))
if x == y:
print("x and y are equal")
if y != 0:
print("therefore, x/y is", x/y)
elif x < y:
print("x is smaller")
else:
print("y is smaller")
1
print("thanks!") LECTURE 2
4
INDENTATION AND BLOCK
§ Indentation is important
- Python replies on the indentation to define the scope of a block
- Statements in the same block MUST have the same indentation
- Although the number of spaces in an indentation can vary –
Ø it is recommended that you use the same indentation, such as 4 spaces,
throughout your program – don’t try to be smart!
Ø It is also recommended that you use space character rather than tab
character to create an indentation.
§ Write a program that takes the unit code and total mark in
that unit as input and prints the letter grade according to
following letter grade conversion table:
HD 80-100
D 70-79
C 60-69
P 50-59
N <50
LECTURE 2 1
7
ACTIVITIES
§ Solution
unit = input("Type in your unit code: ")
mark = int(input("Type in your mark: "))
grade = 'N'
if mark >= 80:
grade = 'HD'
if mark >= 70 and mark <80:
grade = 'D'
print("Your unit mark is ", mark, " your unit grade is " + grade)
LECTURE 2 1
8
CONTROL FLOW: while LOOPS
while <condition>:
<statement>
<statement>
...
§ <condition> evaluates to a Boolean
§ if <condition> is True, do all the steps inside
the while code block
§ check <condition> again
§ repeat until <condition> is False
LECTURE 2 1
9
CONTROL FLOW: while LOOPS
PROGRAM:
n = input("You're in the Lost Forest. Go left or right? ")
while n == "right":
n = input("You're in the Lost Forest. Go left or right? ")
print("You got out of the Lost Forest!")
LECTURE 2 2
0
CONTROL FLOW: while LOOPS
LECTURE 2 2
1
CONTROL FLOW: for LOOPS
for <variable> in range(<some_num>):
<statement>
<statement>
...
§ Python function range returns a sequence of number
starting with 0 and incremented by 1 by default
- So range(5) returns the sequence 0,1,2,3,4
§ each time through the loop, <variable> takes a value of the
sequence
- first time, <variable> starts at the 1st value (0)
- next time, <variable> gets the next value 2
LECTURE 2
2
- etc.
range(start,stop,step)
mysum = 0
for i in range(7, 10):
mysum += i # same as mysum = mysum + 1
print(mysum)
mysum = 0
for i in range(5, 11, 2):
mysum += i
print(mysum) LECTURE 2 2
3
break STATEMENT
while <condition_1>:
<statement_b>
if <condition_2>:
break
<statement_c>
LECTURE 2 2
4
break STATEMENT
mysum = 0
for i in range(5, 12, 2):
if mysum > 10:
break
mysum += i
print(mysum)
LECTURE 2 2
5
for VS while LOOPS
LECTURE 2 2
6
ACKNOWLEDGEMENT
27