# HG changeset patch # User Shantanu # Date 1271852260 -19800 # Node ID 8e02b76cf068a4329ab2e0ddbccbe064dce69098 # Parent 25248b12f6e4bc1ee6a632427ccbe1345a89c016 Number data types scripts. diff -r 25248b12f6e4 -r 8e02b76cf068 basic-python.txt --- a/basic-python.txt Wed Apr 21 17:34:01 2010 +0530 +++ b/basic-python.txt Wed Apr 21 17:47:40 2010 +0530 @@ -146,10 +146,4 @@ 17/2.0 8.5 17.0/2.0 -8.5 - - - - - - +8.5 diff -r 25248b12f6e4 -r 8e02b76cf068 numbers.org --- a/numbers.org Wed Apr 21 17:34:01 2010 +0530 +++ b/numbers.org Wed Apr 21 17:47:40 2010 +0530 @@ -8,38 +8,82 @@ *** Script Welcome friends. - In this tutorial we shall look at data types available in Python and - how to perform simple Input and Output operations. - for 'Numbers' we have: int, float, complex datatypes - for Text content we have strings. + In this tutorial we shall look at data types in Python and + mathematical operators available. + For 'Numbers' we have: int, float, complex datatypes For conditional statements, 'Booleans'. Lets get started by opening IPython interpreter. Lets start with 'numbers' - All integers irrespective of how big they are, are of 'int' - data type - Now we will create a variable, say + All integers irrespective of how big they are of 'int' data + type. Now we will create a variable, say x = 13 print x To check the data type of any variable Python provides 'type' function type(x) + lets create one more variable y = 999999999999 + (what about large value? and it is Long and not int, should we mention + that?) print y - Floating point numbers comes under 'float' + Floating point numbers come under 'float' p = 3.141592 type(p) Python by default provides support for complex numbers. c = 3+4j - c is a complex number. 'j' is used to specify the imaginary part. + creates a complex number c. Here 'j' is used to specify the imaginary + part. type(c) Python also provides basic functions for their manipulations like abs(c) will return the absolute value of c(sqrt(a^2 + b^2)) c.imag returns imaginary part and c.real gives the real part. + + All the basic operators work with Python data types, without any + surprises. When we try to add two numbers like x and y Python takes + cares of returning 'right' answer and we dont have to worry about + boundary conditions. + x + y + + % operator is for modulo operations + 1786 % 12 gives 10 + 45 % 2 returns 1 + Same as additions multiplication also works just right: + 3124 * 126789 + 396088836 + + x ** y returns x raised to power y. For example lets try: + big = 1234567891234567890 ** 3 + + Division in Python truncates, that is, when we divide a integer + variable with another integer result is also integer and decimal + value is truncated. So + 17 / 2 returns 8 and not 8.5 + + but + 17 / 2.0 will return the correct 8.5, similarly + 17.0 / 2 will also give correct answer. + + Other operators which comes handy are: + += + lets create one variable a with + a = 7546 + now + a += 1 will increment the value of 'a' by 1 + similarly + a -= 1 will decrement. + we can also use + a *= 2 + a + answer is multiplied by 2 + + a /= 5 + a is divided by 5 + Next we will look at Boolean datatype: Its a primitive datatype having one of two values: True or False. t = True @@ -50,7 +94,7 @@ f = not True - we can do binary operation like 'or' and 'not' with these variables + we can do binary operation like 'or', 'and', 'not' with these variables f or t f and t @@ -65,8 +109,38 @@ a and (b or c) False - we come to the end of this tutorial on introduction of Data types in - Python. In this tutorial we have learnt what are supported data types, - supported operations and performing simple IO operations in Python. + We also have support for relational and logical operators. Lets try some + examples: + We start with initializing three variables by: + p, z, n = 1, 0, -1 + To check equivalency of two variables use '==' + p == z + False + p >= n + True + + We can check for multiple logical operations in one statement itself. + n < z < p + True. + This statement checks if 'z' is smaller then 'p' and greater then 'n' + For inequality testing we use '!' + p + n != z will add 'p' and 'n' and check the equivalency with z + + We have already covered briefly in some of the previous sessions, + conversion of data among different types. + int(17 / 2.0) will convert result to integer type and we get + 8 as answer and not 8.5 + But if we try something like + float(17 / 2) we get 8.0 as 17/2 is already truncated to int + and converting that to float wont restore the lost decimal digits. + To rounding off a floating number to a given precision 'round' function + can be used. + round(7.5) returns + 8. + + This brings us to the end of tutorial on introduction of Data types + related to numbers in Python. In this tutorial we have learnt what are + supported data types, supported operations and operators. How to convert + one data type to other. *** Notes