Professional Documents
Culture Documents
IFB104 Topic02 CallingFunctions
IFB104 Topic02 CallingFunctions
IFB104 Topic02 CallingFunctions
https://qut.to/286bp
CRICOS No. 00213J
Get help and learn with peers
Drop by STEM peer support with STIMulate
The STIMulate Team are trained volunteer students. Many maths, science, IT and
Engineering topics are supported through free and confidential drop-in sessions.
Schedule
• Roster: 10am-3pm
• Days: Monday to Friday
• Weeks: 2-13
• Location: The Gardens Point Learning Hub located on level 2 of the Gardens Point
Library (V block)
stimulate.qut.edu.au
CRICOS No. 00213J
The challenge of building IT systems
7
CRICOS No. 00213J
The challenge of building IT systems
10
CRICOS No. 00213J
Key programming concept: Three different
ways of writing expressions
• Confusingly, a programming language such
as Python offers three distinct ways of
performing operations on data values:
• operators
• functions
• methods
• There are sound historical and technical
reasons for this, but they are not always
apparent to the beginner programmer!
11
CRICOS No. 00213J
Built-in operators
13
CRICOS No. 00213J
Methods
Method First
(applicable “argument”
to strings in (object)
this case)
14
CRICOS No. 00213J
Key programming concept: Two reasons
for calling a function (or method)
• Most functions/methods accept some
arguments and return a new value, which
can be used in a larger expression, assigned
to a variable, or printed to the screen
• However, some functions produce a side-
effect on variables or the computing
environment and return nothing
• An attempt to access the value returned
by a pure side-effecting function gets the
special value None
• And some functions do both (e.g., Turtle’s
stamp method both draws an image and
returns an identifier) 15
CRICOS No. 00213J
Optional and named arguments
16
CRICOS No. 00213J
More character string operations
19
CRICOS No. 00213J
Some more commonly-used list operations
L[n] returns the nth item in list L, counting from zero
L[m:n] returns the subsequence of list L from position m,
inclusive, to position n, exclusive
len(L) returns the length of list L
max(L) returns the largest value in numeric list L
sum(L) returns the sum of numbers in a numeric list L
L.append(i) adds item i onto the end of list L (note that this
operation changes L ‘in place’ rather than returning a
value)
L.sort() sorts the items in list L (again this changes variable L’s
value in place)
20
CRICOS No. 00213J
Key programming concept: Mutable versus
immutable variables
• String variables are immutable (unchangeable)
• All string operations return new strings
• To update a string variable we must assign
a new value to it
• List variables are mutable (changeable)
• Some list operations return new lists and
leave the given list unchanged
• Other list operations change the list ‘in
place’ when the operation is applied
• It’s hard to remember which of these effects
a particular string function has, so keep the
Python Standard Library manual handy!
CRICOS No. 00213J
22
CRICOS No. 00213J
The random module
24
CRICOS No. 00213J
Doing the same thing many times
26
CRICOS No. 00213J
Part D — Fun with Graphics
27
CRICOS No. 00213J
Turtle graphics
• Another module we will use a lot is called • There are many pre-defined functions in the
turtle and allows us to draw things! turtle module
• Pictures are drawn by moving a pen- • See the Python Standard Library manual
carrying ‘turtle’ (i.e., cursor) around the under Turtle Graphics for full details
screen
• The turtle can move forward a certain
number of pixels (‘picture elements’)
• The turtle can walk in a circle
• The turtle can turn left or right by a
certain angle
• The turtle can raise and lower its pen
• The turtle can be sent to a specific x-y
coordinate on the drawing canvas
28
CRICOS No. 00213J
dot(d) draw a dot of diameter d pixels at the current position (even if the pen is up)
begin_fill() … fill the shape drawn between these two statements with the current colour
end_fill()
color(c) set the default colour to c (c can be a string)
31
CRICOS No. 00213J