When casting a numeric value to a boolean, a zero value is false, while all non-zero numbers are True
When casting a String or Unicode value to a boolean, an empty string is False, any other string is True.
When casting a float, rounding will not occur automatically to the decimal value. Instead, the round() function should be called.
When casting a String or Unicode value, the string literal needs to be a valid integer or long: decimal values contained in the string will result in a ValueError
Integers have at least 32 bits of precision, while Longs have unlimited precision.
123 321 400 ValueError
|When casting a string literal as a float, non-numeric characters in the string will result in an exception, except for a decimal point (".").|
str() and unicode()
|Most objects can be cast as a string representation of some sort, including sequences.|
80 [1, 2, 3]
Checking an Object's Type
Checking the data type of an object can easily be done with both the
|When passed a single parameter, this function returns the type of the |
type 'int'> <type 'str'>
Returns True if the
If checking for a string or unicode type, a
Generating a Range of Values
In some cases, it is useful to generate a range of integers for iteration. Python's
range() function will return a list of integers.
Returns a list of progressively greater integers.
[0, 1, 2, 3, 4] [1, 2, 3, 4] [1, 4, 7] [15, 12, 9, 6, 3]
Assume we need to read from five separate Tags with a nearly identical Tag path in a single script:
Instead of manually typing each path, we could use
range() in a for loop that would write the paths automatically.
You can round numbers inside Python with a few simple functions.
When passed a single parameter, this function returns a rounded integer value of a number. If the decimal is greater than or equal to .5, the it rounds up, less than .5 rounds down.
If the optional digits argument is used, then it rounds to that many decimal places.
Returns a truncated integer from the number given. The largest integer value less than or equal to number.
Note that the example needs to import that math library before being able to call floor().
Returns the ceiling integer from the number given. The smallest integer value greater than or equal to number.
Note that the example needs to import that math library before being able to call ceil().