COMP 6411: Comparative
Programming Languages
Tutorial 3: Python
Programming
Topics
• Basic structure of a Python program
• Indentation
– Why use whitespace
• Flow control
– WHILE loop
– FOR loop
• Range statement
– IF and ELIF
• Functions
– Syntax
– Parameters
– Default arguments
Indentation
• As previously noted, Python uses whitespace to
delimit coding blocks
– There are no curly braces, other than for data
structures like dictionaries
• Here, whitespace can consist of either tabs or
space characters.
• NOTE: either works but they should NOT be
mixed in the same source file.
• In general, Python recommends the use of 4
spaces rather than tabs.
– Your IDE can usually be configured to do this
automatically.
Purpose
• There a couple of reasons that white space
indentation is useful.
• Recall the ZEN principles:
– Python is intended to be simple and clean
– So dumping all those curly braces makes the
code much less cluttered
• In addition, white space indentation
ensures that everyone’s Python code
essentially looks the same
– We don’t have 10 different styles of indentation.
Purpose…cont’d
• There is also a second reason.
if(total > 100) // no braces used
y = 4;
• Now we update this code to add an additional
statement
if(total > 100) // update
z = 2;
y = 4;
• Note that this will compile but “y = 4;” will
always execute, regardless of the value of total.
• If we use whitespace indentation, “y = 4;” only
gets executed if total > 100
While loops
• Python provides the conventional control flow
statements.
• Loops can be provided with either the WHILE or FOR
constructs
• An example of a WHILE loop is as follows:
while x < 1:
z = 2
y = 4
• Note the following:
– The conditional expression does not require any
delimiters
– The use of the “:” after the expression
– The contents of the WHILE loop are indented
– The WHILE loop ends when the indentation ends.
FOR loops
• Python uses a simple FOR loop style that resembles
Java’s more recent FOR-EACH loop (actually, Java
resembles Python)
• If anything, its syntax is even simpler and more
intuitive:
for x in animal:
print “Animal is a “, x # 2.x style print
• Note the use of the in keyword.
• The variable following in simply has to be a sequence
type
– Items are iterated over in order
• Like C and Java, loops can also use break and
continue
Ranges
• FOR loops are often used with the range
function
• This can be used to define a sequence of
values for the loop iteration
for x in range(5):
print “Count is “, x
• Ranges are inclusive/exclusive (i.e., they do
not include the last value in the range)
• Ranges can also have a start/end
– range(4, 8) # 4 to 7
• They can also have a “step”
– range (4, 20, 5) # 4 to 19, by 5’s
IF statements
• IF statements are similar to other languages
• They can also utilize the elif (else if) logic
and a final else.
– A “:” must be used on each line.
• Note that there is no switch/case statement in
Python
– You simply use a series of if/elif statements for
this
if x < 0:
print “x is < 0”
elif x < 10:
print “x is < 10”
else:
print “x is >= 10”
Testing and Conditionals
• Python includes the values True and False
that may be used in conditional evaluations.
• Rather than NULL, Python uses None to
indicate a non-existent value.
• The boolean operations, from highest to
lowest priority, are:
– not
– and
– or
– Both and and or only evaluate the second
operand if necessary
Conditionals…cont’d
• Conditional operations are similar to other
languages and include:
– <
– >=
– <
– <=
– ==
– !=
• We also have a couple of tests for Object identity
(discussed later)
– is
– is not
source: [Link]
Python functions
• Like all other general purpose languages, Python
provides support for function definitions.
• Not surprisingly, the syntax is simple and intuitive.
• An example is below:
def foo(number):
print “my number is ”, number
return number * 2
• Note:
– Functions are introduced with the def keyword
– Parameters require no type
– Return types are not explicitly specified
– We again use “:” and whitespace to define the function
block
– Functions return None by default
Functions…cont’d
• Python arguments use pass-by-value.
– Essentially the same as C and Java
– Keep in mind that references to objects (discussed later)
allow you to change object members inside the function,
just like Java.
• Also note that functions can be assigned to
variables.
– Let’s say that we have a function called boo().
– So we can do an assignment as follows
• boo2 = boo
– boo2 is now essentially an alias for the same function
– Don’t get carried away with this feature – it could be very
confusing
source: [Link]
Default argument values
• A really nice feature of Python is the ability to
configure default arguments
• This allows programmers to specify default
values for the arguments that are not provided
by the function’s user.
• Defaults are specified as “var=default”
def foo(num, text=“test”, thing=43.5):
print “my num is ”, num
print “my text is ”, text
print “my thing is ”, thing
Default args…cont’d
• Note:
– If no default is provided, then the argument must be
provided
– Once a default parameter is specified in the function
definition, any remaining parameters must also have
defaults
• def foo(n, x=2, m) # invalid syntax
• foo(3, 4) # no way to know if 4 is x or m
– If a subset of args are provided, they are applied to
the parameters in a left to right order
– You can change this order by explicitly providing the
parm name
• Say we have def foo(n, m=4, z=3):
• We could call foo(3, z= 7) # m would be 4
source: [Link]