Many of the topics we discussed today are covered in these sections of Introducing Python:
Chapter 5. Text Strings
Create with Quotes
Escape with \
Get a Character with []
Get a Substring with a Slice
Get Length with len()
Chapter 6. Loop with while and for
Repeat with while
Skip Ahead with continue
Check break Use with else
Iterate with for
Skip with continue
Check break Use with else
Chapter 7. Tuples and Lists
Lists
Create with []
Create or Convert with list()
Get an Item by [ offset ]
Get Items with a Slice
Add an Item to the End with append()
Change an Item by [ offset ]
Assign with =
Copy with copy(), list(), or a Slice
Here are some additional notes.
The sequence of Fibonacci numbers Fi is defined as follows: F1 = 1, F2 = 1, and Fi = Fi-1 + Fi-2 for all i ≥ 3. So the sequence is
1, 1, 2, 3, 5, 8, 13, 21, …
Let's write a program that reads an integer N and writes the first N numbers in the Fibonacci sequence.
To accomplish this, we use two integer variables a
and b
to store the next two numbers that we will
output:
n = int(input('Enter n: ')) a = 1 b = 1 for i in range(n): print(a) c = a + b a = b b = c
On each loop iteration, we first output a
. We then
compute c
, which is the Fibonacci number that comes
after a
and b
. Then, since we have already
output a
, we shift b
into a
and then c
into b
.
The key to solving a problem like this is figuring
out what state we must remember between loop iterations – in
this case, the variables a
and b
.
In Python, we can alternatively write the code above using multiple assignment. Instead of
c = a + b a = b b = c
we can write
a, b = b, a + b
This first computes the values b and (a + b), then assigns these new values to a and b respectively.
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:
The ASCII character set includes only 128 characters; its code points range from 0 to 127. For example, in ASCII the character 'A' has the number 65, and 'B' has the number 66. ASCII includes all the characters you see on a standard English-language keyboard: the uppercase and lowercase letters A-Z/a-z of the Latin alphabet, the numbers 0-9 and various punctuation marks such as $, % and &. ASCII does not include accented characters such as č or ř.
The newer Unicode character set extends ASCII to include all characters in all languages of the world, including accented characters and also ideographic characters in Asian languages such as 日. Code points in Unicode range from 0 to 1,114,111.
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.
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.