Programming 1, 2020-1
Lecture 3: Notes

Some of today's topics are covered in these sections of Think Python:

And in these chapters of Introducing Python:

Here are some additional notes.

Unicode

Computers generally store text using a coded character set, which assigns a unique number called a code point to each character. Two character sets are used in virtually all software systems today:

Python 3 is fully compatible with Unicode. You can write

s = 'Řehoř'

or

s = ''

and these strings will work just like strings of ASCII characters.

ord() and chr()

Python includes two functions that can map between characters and their integer code points.

Given a Unicode character c, ord(c) returns its code point. For example, ord('A') is 65, and ord('B') is 66. ord('ř') is 345 (a value outside the ASCII range).

chr() works inversely: it maps a code point to a character. For example, ord(65) is 'A', and ord(345) is 'ř'.

These functions are sometimes useful when we wish to manipulate characters. For example, here is a program that reads a lowercase letter, and prints the next letter in the alphabet:

c = input('enter letter: ')
i = ord(c) - ord('a')
if 0 <= i < 26:
  i = (i + 1) % 26
  print('next letter is', chr(ord('a') + i))
else:
  print('not a lowercase letter')

The program uses ord() to convert a character (such as 'd') to a number (such as 3) representing its position in the lowercase alphabet. It then adds 1 (mod 26), and uses chr() to map the result back into a lowercase letter.

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.