D ATA T YPE. NUMBERS A number is an immutable type – means that changing or updating its value...

Post on 12-Jan-2016

214 views 0 download

Tags:

Transcript of D ATA T YPE. NUMBERS A number is an immutable type – means that changing or updating its value...

DATA TYPE

NUMBERS

A number is an immutable type – means that changing or updating its value result in a newly allocated object.

There are several types of numeric types – Integer Long integer Boolean Double –precision floating point real numbers Decimal floating point numbers Complex numbers

INTEGERS

There are several types of integers Boolean type – 2 possible values, True and False Actually an integer and behave like integer values

0 and 1 if used in numeric context. Standard Integers

Python standard integers are the universal numeric type.

Most machines (32-bit) running Python will provide a range of -231 to 231 -1 ( -2,147,483,648 to 2,147,483,647)

Example of integer : 0101, 84, -234,Ox80 etc Integers are normally represented in base 10

decimal format. It can also be represented in base 8 (octal, “0”

prefix)or base 16 (hexadecimal, “0x”/”OX” prefixes)

LONG INTEGERS

Longs are a superset of integers It is useful when your application requires

integers that exceed the range of plain /standard integers.

Use of longs is denoted by the letter ‘L’, uppercase (L) or lowercase (l), appended to the integer’s numeric values.

Values can be expressed in decimal , octal , or hexadecimal.

Examples : 16384L,-Ox4E8L ,-5432101234L,2997924581l

DOUBLE PRECISION FLOATING POINT NUMBERS

Floating point values are denoted by a decimal point (.) in the appropriate place and an optional “e” suffix representing scientific notation.

Can use either lowercase (e) or uppercase (E).

Positive (+)/negative (-) signs between the “e” and the exponent indicate the sign of the exponent.

No sign indicate positive exponent. Examples : 0.0, -5.55557119, -1.609E-

19,3.67 etc

COMPLEX NUMBERS

Combining a real number with an imaginary number

Example: 64.375 +1j, 0.23-8.55j,9.80665-8.31441J

OPERATORS-NUMERIC TYPE (ARITHMETIC )

Division Classic division

Using integer operands, classic division truncates the fraction, returning an integer (floor division).

Using a pair of floating point operands, it returns the actual floating-point quotient (true division).

Example :>>> 1/2 #perform integer result (floor)0>>> 1.0/2.0 #returns actual quotient0.5

CONTINUE

True division the division always returns the actual quotient

regardless of the type of the operands For now, to take advantage of true division, need

to give the from_future_import division directive. Once that happens, the division operator (/)

performs only true division. >>> from_future_import division >>>>>>1/20.5>>>1.0/2.00.5

CONTINUE

Floor division (//)- always truncates the fraction and rounds it

to the next smallest whole number toward the left on the number line, regardless of the operands’ numeric types.

Examples:>>> 1 // 20>>>1.0 // 2.0 0.0>>>-1 // 2-1

MODULUS

Integer modulus is straightforward integer division remainder .

Exponentiation Has a peculiar/strange precedence rule in its

relationship with the unary operators. It binds more tightly than unary operators to

its left, less tightly than unary operators to its right.

example:>>> 3 ** 29

CONTINUE

>>> -3 ** 2 # perform 3 to the power of two before applies the unary negation

-9>>> (-3) ** 29

EXAMPLES OF NUMERIC OPERATORS

>>> -442 -77-519>>> 4**364>>> 4.2 ** 3.298.7183139527>>> 8/32>>> 8.0/3.02.66666666667>>> 8 % 32

CONTINUE

>>> (60. -32.) * (5./9.)15.5555555556>>> 14 * 0x0456>>> 0170/430>>> 45L * 22L990L

NUMERIC TYPE FUNCTIONS

Function operation

bool (obj) Returns the boolean value of obj.

int (obj,base =10) Returns integer representing of string or number obj.

long (obj, base =10)

Returns long representation of string or number obj.

float (obj) Returns floating point representation of string or number obj

complex (str) Returns complex number representation of str, or builds one given real

EXAMPLES

>>> int (4.2555)4>>> long (42)42L>>> float(4)4.0>>>complex(4)(4+0j)

OPERATIONAL Python has five operational built-in function

for numeric types: abs() – returns the absolute value of the given

argument. If the argument is a complex number, then math.sqrt(num.real2 +num.imag2) is returned.

coerce ( ) – returns a tuple containing the converted pair of numbers.

divmod ( ) –combines division and modulus operations into a single function. The values returned are the same as those given for

the classic division and modulus operator for integer type.

As for float, the quotient returned math.floor(num1/num2).

As for complex numbers, the quotient is math.floor((num1/num2).real)

CONTINUE

pow ( ) – pow () and ** perform exponentiation, one is operator (**), and the other is build-in function.

round ( ) – has a syntax of round(flt,ndig=0) rounds a floating point number to the nearest integral

number and returns that result still in a float. When the optional ndig is given, round () will round the

argument to the specific number of decimal places.

EXAMPLE

abs ()>>> abs(-1)1>>> abs (10.)10.0>>> abs (0.23-

0.78)0.55

• coerce ()>>> coerce(1,2)(1,2)>>>coerce (1,134L)(1L,134L)

CONTINUE WITH EXAMPLES

>>>divmod(10,3)(3,1)>>> divmod (3,10)(0,3)

>>> pow(2,5)32

>>> round (3)3.0>>> round(3.49999,1)3.5

>>> bool(1)True>>> bool(True)True>>> bool(‘1’)True