Abdullah Diab’s Blog

I’m Learning Python part 7

I’m Learning Python (part 7)

<p>
  <img title="Python Logo" src="http://www.python.org/images/python-logo.gif" alt="Python Logo" />
</p>

Apology:

First of all I wanna apologize for being late with this part, I’ve had busy days first during Shaam 2008 expo and second during some studies at the college and last busy time past launching Bawabaty project.

Numbers:

Numbers in Python are divided into 5 sections:

Integers:

Such as: 1234, -1234, 0

Long Integers (unlimited size):

Such as: 71632487932648921735413278645238947231659821365213847L

These numbers grow as long as your memory can hold, and it ends with a small or capital L but it is an optional literal, you can define:

/>>> TheLongVariable = 99999999999999999999999999

/>>> TheLongVariable

99999999999999999999999999L

Floating Point Numbers:

Such as: 1.234, 3.14e-10, 4.01e+12

You can define them using points, e or E.

Octal and hex literals:

Such as: 0172, 0x9fab, 0X3FBC

Octal is defined by starting your number with a leading zero, while hexadecimal is defined by starting you number by a leading 0x or 0X.

Complex Numbers:

Such as: 3+4j, 3.0-5.1j, 3J

Complex numbers are defined by a j or J at the end of the number, the number can consist of two parts, real and imaginary, so all the following are complex numbers:

1 + 3j, 3j, 5J, -10.1j

Python has lots of built-in numeric tools:

Of course you can use +, -, *, /, <, >, ==, !=, <> as any other programming language, but Python has more:

**: Power, 2 ** 3 is 8.

//: Is truncated division, 5 // 2 is 2

%: Remainder, 5 % 2 is 1

You can use also bitwise operations:

/>>, << shift right and left, 5 << 2 is 20, 24 >> 3 is 3.

|, & Or and And, 4 | 3 is 7, 7 & 2 is 2.

Just as any other programming language, Python deals with expressions using operator precedence: 3 + 4 * 2 is 11 because * is done before +.

Of course you can use parentheses to define the way you want to deal with the expression, (3+ 4) * 2 is 14.

When mixing multiple types in an expressions, all types are converted up, 40 + 3.14 will be calculated as 40.0 + 3.14. The types are ordered like this:

Integers < Long Integers < Floating Points < Complex Numbers.

The type of the result of a mixed expression is the type of the higher type, so 40 + 3.14 is a floating point, but you can force it into an integer using: int(40 + 3.14).

Variables:

During our trip in Python till now we used variables but we haven’t talked about them:

Variables are created once they are assigned a value.

Variables are replaced with their values in expressions.

You can’t use an unassigned variable, because simply you don’t have the variable yet.

Variables are references to objects.

repr & str:

Try this

/>>> a = 3

/>>> b = 4

/>>> b / (2.0 + a)

0.80000000000000004

/>>> print b / (2.0 + a)

0.8

We’ve seen something like this before, and we discussed it, the difference between the two methods is that each one uses a specified function, the first uses a function called repr which prints the value as it is in your memory, while the other uses a function called str which prints the value in a user friendly way.

More Functions:

Python provides functions for Octal and Hex conversion:

/>>> oct(64)

‘0100’

/>>> oct(8 )

‘010’

/>>> hex(255)

‘0xff’

And vice versa, Python provides functions for retrieving integers out of Hex and Oct:

/>>> int(‘0234’)

234

/>>> int(‘0234’, 8 )

256

/>>> int(‘0234′, 16)

564

And it also provides a way to format numbers in strings, which we will cover in another part, but a small example won’t hurt:

/>>> “%o %x” % (25, 21)

’31 15’

Other built-in functions and methods are in math module, which you can import and use:

/>>> import math

/>>> math.pi, math.e

(3.1415926535897931, 2.7182818284590451)

/>>> math.sin(math.pi / 2)

1.0

The result shown in parentheses is a tuple (which we covered last part), it is shown in a tuple because we put a column between math.pi and math.e

Bottom Line:

Thank you for waiting for this part, and until I meet you on another part try to read more about numbers from the online documentation of Python.

Cheers.