>>> a * b
50
>>> a / b
0
>>> b = 10.0
>>> a / b
0.5
>>> a // b
0.0
The first division returns 0
because both a
and b
are integers (whole numbers), so Python calculates the division as an integer, giving 0
. Because b
is converted to 10.0
, Python considers it to be a floating-point number and so the division is now calculated as a floating-point value, giving 0.5
. Even with b
being floating-point, using //
—floor division— rounds it down.
Using **
, you can easily see how Python works with integers:
>>> 2 ** 30
1073741824
>>>2 ** 31
2147483648L
The first statement raises 2 to the power of 30 (that is, 2?2?2?2?2? ...), and the second raises 2 to the power of 31. Notice how the second number has a capital L on the end of it — this is Python telling you that it is a long integer. The difference between long integers and normal integers is slight but important: Normal integers can be calculated with simple instructions on the CPU, whereas long integers — because they can be as big as you need them to be — need to be calculated in software and therefore are slower.
When specifying big numbers, you need not put the L at the end — Python figures it out for you. Furthermore, if a number starts off as a normal number and then exceeds its boundaries, Python automatically converts it to a long integer. The same is not true the other way around: If you have a long integer and then divide it by another number so that it could be stored as a normal integer, it remains a long integer:
>>> num = 999999999999999999999999999999999L
>>> num = num / 1000000000000000000000000000000
>>> num
999L
You can convert between number types by using typecasting, like this:
>>> num = 10
>>> int(num)
10
>>> float(num)
10.0
>>> long(num)
10L
>>> floatnum = 10.0
>>> int(floatnum)
10
>>> float(floatnum)
10.0
>>> long(floatnum)
10L
You need not worry whether you are using integers or long integers; Python handles it all for you, so you can concentrate on getting the code right.
More on Strings
Python stores a string as an immutable sequence of characters — a jargon-filled way of saying 'it is a collection of characters that, after they are set, cannot be changed without creating a new string.' Sequences are important in Python. There are three primary types, of which strings are one, and they share some properties. Mutability makes much more sense when you learn about lists in the next section.
As you saw in the previous example, you can assign a value to strings in Python with just an equal sign, like this:
>>> mystring = 'hello';
>>> myotherstring = 'goodbye';
>>> mystring
'hello'
>>> myotherstring;
'goodbye'
>>> test = 'Are you really Bill O'Reilly?'
>>> test
'Are you really Bill O'Reilly?'
The first example encapsulates the string in single quotation marks and the second and third in double quotation marks. However, printing the first and second strings shows them both in single quotation marks because Python does not distinguish between the two. The third example is the exception — it uses double quotation marks because the string itself contains a single quotation mark. Here, Python prints the string with double quotation marks because it knows the string contains the single quotation mark.
Because the characters in a string are stored in sequence, you can index into them by specifying the character in which you are interested. Like most other languages, these indexes are zero-based, which means you need to ask for character 0 to get the first letter in a string. For example:
>>> string = 'This is a test string'
>>> string
'This is a test string'
>>> string[0]
'T'
>>> string [0], string[3], string [20]
('T', 's', 'g')
The last line shows how, with commas, you can ask for several indexes at the same time. You could print the entire first word by using this:
>>> string[0], string[1], string[2], string[3]
('T', 'h', 'i', 's')
However, for that purpose you can use a different concept: slicing. A
>>> string[0:4]
'This'
The syntax there means 'take everything from position 0 (including 0) and end at position 4 (excluding it).' So [0:4]
copies the items at indexes 0, 1, 2, and 3. You can omit either side of the indexes, and it copies either from the start or to the end:
>>> string [:4]
'This'
>>> string [5:]
'is a test string'