ASCII Character set:
In Python, characters are represented in ASCII, the American Standard Code for Information Exchange. The table above lists the complete set of ASCII codes. This tells you how various characters will compare and how they will sort.
Try this out:
If ‘,’ < ‘.’:
print “a comma is less than a period”
“(“ < “[“
The ord function in Python will convert a character into it’s internal numeric representation.
The chr function in Python will convert a number into it’s ASCII character.
Try it:
ord (‘m’)
ord (‘M’)
ord (‘,’)
ord(‘.’)
Now try it the other way around:
chr (55)
chr(99)
chr(109)
Exercise: write your first name as a list of numbers that represent ASCII characters.
Example: the name Sam would be: 83, 97, 109
Notice that ASCII characters each take up 7 bits. You’ll notice that the numbers range from 0
to 127.
Binary,
Octal and Hexadecimal numbers
We know that our number system is a base 10 number system. That means that we have 10 symbols (0, 1, 2, 3, 4, 5, 6, ,7, 8, 9). This means that when we write a number like: 127 that we have 7 in the ones position, 2 in the tens position, and 1 in the hundreds position. If we say that another way, we have a 7 in the 100 position, 2 in the 101 position, and 1 in the 102 position. Each position, as we move to the left in a base 10 number represents a value that is 10 times greater than the position immediately to its left.
Base 2 numbers are also called binary numbers. We have two symbols: 0 and 1. For the binary number 11011 we know that, as we move from right to left, that the 1 is in the ones position, the next 1 is in the twos position, the 0 is in the fours position, the next 1 is in the eights position, and the final 1 (on the left) is in the sixteens position. So, 11011 is equal to 16 + 8 + 2 + 1 = 27 in base 10.
Base 8 numbers are also called octal numbers. We have 8 symbols: 0, 1, 2, 3, 4, 5, 6, 7. The positions are the ones, eights, sixty-fours, and so on.
Let’s take a look at how many numbers we can represent using three binary digits:
Exercise: first
count (i.e., write down the numbers from 000 to 111, in sequence) in binary.
Now, convert those groups to octal.
Try a more complex example. Take a binary number like this:
110110
In Python, this is written down like this: 0b110110
Consider dividing it into groups where each group consists of three binary digits (bits).
So we can consider the number 110110 as: 110 110
Convert each of these groups into octal: we get 0o66 (This is Python’s way of writing down an octal number: a leading 0 followed by the letter “o” for octal and then the number.)
Exercise: convince yourself that 110110 in base 2 (binary) is equal to 66 in base 8 (octal)
We can always do this. We can always take a binary number and group it into groups of three bits; starting from the decimal point and moving to the left. Then convert each group into octal; we have an octal number. People often find it useful to “read” octal rather than looking at binary numbers.
Hexadecimal numbers:
Write down all of the binary numbers that you can represent using 4 bits.
Convert those binary numbers to hexadecimal. Wow! Each hexadecimal digit is equal to a four-digit binary number. We can take a binary number, say 110110, and convert it to hexdecimal, just by grouping it into groups of 4, starting with the decimal point and moving to the left. So, 110110 becomes 11 0110. We can convert each of these groups into hexadecimal and get this number: 0x36. This is how Python represents a hexadecimal number: a leading 0 followed by an x followed by the number.
What is 0x36 equal to in base 10?
Exercise: convert the following numbers from binary to octal, to hexadecimal, and to base 10.
1111
10101010
110111000101
The string
library in Python:
Python has a string library, just like it has a math library. (It has many, may libraries.)
You can use the string library by including this command in your program:
import string
or you can say:
import string as s
if you want to use “s” as the short name for string.
Go the Python 2.6.1 documentation and look at the string library. Choose some of the functions available and try them out. Note: skip over the formatting operations; we’ll talk about that later. Notice the list of “deprecated” string functions. Deprecated means that the work (for now) but later on, they are planning on taking them out of the language.
See the definition taken from dictionary.com:
dep·re·cate
(děp'rĭ-kāt')
tr.v. de·pre·cat·ed
, de·pre·cat·ing , de·pre·cates
These deprecated string functions are available using a different syntax. (We may talk about it later. For now, just know that a number of string functions have been implemented for you in Python. They are available in the string library. And, if you become a serious Python programmer, you’ll need to learn the other syntax for invoking string functions.)
Try out, in particular the following string functions; assume s is a stringand w is a word:
string.lower(s)
string.capitalize(w)
string.upper(s)
string.swapcase(s)
string.capwords(s)
You can also examine some of the variables that the string library initializes. Try these:
string.lowercase
string.digits
string.hexdigits
string.punctuation
Strings,
lists, and tuples in Python
A character string in Python is actually a sequence of individual characters. So, the character “Hi, there!” is actually a sequence of 9 characters. We can access the individual characters using the slicing and indexing that we learned last week:
s = “Hi, there!”
s[0] is just “H”
s[-1] is just “!”
s[3:4] is just “ ”
# just a space
s[4:] is just “there!”
Remember: the indexing of a character string always starts with zero!
Lists are sequences of any Python values. The elements in a list can be indexed, just like a string.
l = [1, 2, 3, 4, 5]
l[0] is just 1
l[1] is 2
l[0:5] is [1, 2, 3, 4, 5]
You can change (mute) things in a list like this:
l[3] = 7
But you CANNOT change things in a character string.
Tuples are similar; they are sequences consisting of any number of Python values. But tuples cannot be changed (muted). We say that tuples and strings are immutable – that means they can’t be changed.
Try this:
x = input(‘enter three things’)
then evaluate x
what do you get?
Note: you can do multiple assignment from a tuple (a sequence surrounded by parentheses) or from a list (a sequence surrounded by square brackets).
Extra
stuff:
Another function in Python that you should know about is the eval function. The eval function can take a string as input and evaluates it. Thus, the parameter that you provide for the eval function should be any valid Python expression, represented as a string.
Try this:
eval(“3 + 5”)
eval(“4”)
eval(“123”)
x = “73”
eval(“x”)