Wednesday, December 2, 2009

Python Tutorial (Operators)

Mathematical operators
+ - * / // % **

// floor division
** exponential operators

Comparison operators, which return a Boolean value indicating the truthfulness of the expression:
< <= > >= == != <>

Python currently supports two "not equal" comparison operators, != and <>.

and or not

We can use these operations to chain together arbitrary expressions and logically combine the Boolean results:
>>> 2 < 4 and 2 == 4
False
>>> 2 > 4 or 2 < 4
True
>>> not 6.2 <= 6
True
>>> 3 < 4 < 5
True
They are simply identifier names with an alphabetic first character "alphabetic" meaning upper-or lowercase letters, including the underscore ( _ ). Any additional characters may be alphanumeric or underscore. Python is case-sensitive, meaning that the identifier "cAsE" is different from "CaSe." Python is dynamically typed, meaning that no pre-declaration of a variable or its type is necessary. The type (and value) are initialized on assignment. Assignments are performed using the equal sign. >>> counter = 0

>>> m = 1000.0
>>> name = 'sameer'
>>> counter = counter + 1
>>> km = 1.609 * m
>>> print '%f miles is the same as %f km' % (m, km)
1000.000000 miles is the same as 1609.000000 km


Python also supports augmented assignment, statements that both refer to and assign values to variables. You can take the following expression ...
n = n * 10

...and use this shortcut instead:
n *= 10

Python does not support increment and decrement operators like the ones in C: n++ or --n. Because + and -- are also unary operators, Python will interpret --n as -(-n) == n, and the same is true for ++n.
Python supports five basic numerical types, three of which are integer types.
· int (signed integers)
o long (long integers)
o bool (Boolean values)
· float (floating point real numbers)
· complex (complex numbers)


Here are some examples:
int 0101 84 -237 0x80 017 -680 -0X92
long 29979062458L -84140l 0xDECADEDEADBEEFBADFEEDDEAL
bool True False
float 3.14159 4.2E-10 -90. 6.022e23 -1.609E-19
complex 6.23+1.5j -1.23-875J 0+1j 9.80665-8.31441J -.0224+0j

There is also a sixth numeric type, decimal, for decimal floating numbers, but it is not a built-in type. You must import the decimal module to use these types of numbers. They were added to Python (version 2.4) because of a need for more accuracy. For example, the number 1.1 cannot be accurately representing with binary floating point numbers (floats) because it has a repeating fraction in binary. Because of this, numbers like 1.1 look like this as a float:
>>> 1.1
1.1000000000000001
>>> print decimal.Decimal('1.1')
1.1

Delimiters
The following tokens serve as delimiters in the grammar:

( ) [ ] { } @
, : . ` = ;
+= -= *= /= //= %=
&= |= ^= >>= <<= **=

The period can also occur in floating-point and imaginary literals. A sequence of three periods has a special meaning as an ellipsis in slices. The second half of the list, the augmented assignment operators, serve lexically as delimiters, but also perform an operation.

The following printing ASCII characters have special meaning as part of other tokens or are otherwise significant to the lexical analyzer:

' " # \

The following printing ASCII characters are not used in Python. Their occurrence outside string literals and comments is an unconditional error:

$ ?

No comments:

Post a Comment