Programming 1
Lecture 4: Notes

Many of the topics we discussed today are covered in these sections of Introducing Python:


Here are some additional notes.

structural and reference equality

Suppose we create a couple of lists as follows:

>>> x = [3, 5, 7, 9]
>>> y = x
>>> z = [3, 5, 7, 9]

Now the variables x and y refer to the same list. As we have seen earlier, if we change x[0], then the change will be visible in y, but not z.

Python provides two different operators for testing equality. The first is the == operator:

>>> x == y
True
>>> x == z
True

This operator tests for structure equality. In other words, given two lists, it compares them element by element to see if they are equal. (It will even descend into sublists to compare elements there as well.)

The second equality operator is the is operator:

>>> x is y
True
>>> x is z
False

This operator tests for reference equality. In other words, it returns true only if its arguments actually refer to the same object.

You may want to use each of these operators in various situations. Note that is returns instantly, whereas == traverses lists in their entirety and so may be slower if lists are large.

functions and methods

Python includes both functions and methods.

A function takes one or more arguments and optionally returns a value. Some of Python's built-in functions that we've already seen in this course are len(), chr(), ord(), input() and print(), for example. To call a function, we simply write its name followed by the arguments:

n = input('Enter a number: ')

A method is like a function, but is invoked on a particular object. For example:

s = 'yoyo'
b = s.startswith('yo')  # method call

In the second line above, we are invoking (or calling) the startswith() method on the object s. (Recall that in Python a value and an object are the same thing.) This method call takes one additional argument, namely the string 'yo'. It returns a value (True in this case).

Both functions and methods are common in programming languages today. However, they are not both present in all languages. Many procedural languages (including C and classic Pascal) and functional languages (including Haskell) have only functions, not methods.

On the other hand, some object-oriented languages (including Java and C#) have only methods, not functions.

Python is a bit of a hybrid since it has both functions and methods. This arguably makes the language more flexible and convenient at the cost of some complexity.

In this course we will soon learn how to write our own functions, and before long (in a few lectures) will learn how to write our own methods as well.

library quick reference

We have now learned about quite a few functions and methods in Python's standard library. I've made a quick reference page that lists these functions and methods. It's linked from the course home page. I suggest that you read over this reference page now, and continue to refer to it as you write Python programs.

local and global variables

Consider this Python program:

x = 7

def abc(a):
  i = a + x
  return i

def ha():
  i = 4
  print(abc(2))
  print(x + i)

The variable x declared at the top is a global variable. Its value is visible everywhere: both inside the function abc(), and also in the top-level code at the end of the program.

The variables i declared inside abc() and ha() are local variables. They are different variables: when the line "i = a + x" executes inside abc(), that does not change the value of i in ha().

Now consider this variation of the program above:

x = 7
i = 4

def abc(a):
  i = a + x
  return i

print(abc(2))
print(x + i)

In this version, the variables x and i declared at the top are both global. abc() declares its own local i. This is not the same as the global i. In particular, when abc() runs "i = a + x", this does not change the value of the global. The local i is said to shadow the global i inside the function body.

Local variables are a fundamental feature of every modern programming language. Because a local variable's scope (the area of the program where it is visible) is small, it is easy to understand how the variable will behave. I recommend making variables local whenever possible.

In the program above, what if we want the function abc() to use the global i, rather than making a new local variable? We can achieve this by declaring i as global:

x = 7
i = 4

def abc(a):
  global i
  i = a + x
  return i

print(abc(2))
print(x + i)

Now the line "i = a + x" will update the global i.

Notice that in all of the programs above, abc() is able to read the global x without declaring it as global. But if a function wants to write to a global variable, it must declare the variable as global.

To be more precise, here is how Python determines whether each variable in a function is local or global: