Numbers

Numbers are one of the fundamental building block of any programming language. Number data types are used to store numeric values. Python also supports various data types related to numbers. Python supports following numeric types:
  • int − integers or ints; used to store positive or negative whole numbers. Integers in Python 3 are of unlimited size. 
  • float - floating point real values, also knows as floats; they represent real numbers (integer and fractional parts)
  • complex - complex numbers
Note: Python 2 has two integer data types - int and long. In Python 3, there is no separate 'long integer' data type.

Python interpreter can be used as a simple calculator to manipulate numbers. We can perform mathematical operation on numbers directly on Python command prompt. Operators +, -, * , / and parentheses ( ) can be used in expression. For example:

>>> 5 + 5
11
>>> 11 5*2
1
>>> (14 5*2) / 4
1.0
>>> 11 / 2  # division returns a floating point answer
5.5

Integer numbers like 2, 4, 6, 8, etc. have data type int; the numbers with a fractional part like 10.5, 0.05, etc. have data type float.

It is possible to represent an integer in Octal or Hexa-decimal form. Check the following example:

>>> num = 0xA5  # Hexa-decimal number, begins with Zero x (0x)
>>> num
165

>>> num = 0o51 # Octal number, begins with Zero o (0o)
>>> num
41

Division ) operation always returns a float answer. To do floor division and get an integer result (discarding any fractional result) you can use the // operator; to calculate the remainder you can use % (modulus operator):


>>> 15 / 3  # Division returns a float
5.0
>>> 
>>> 15 // 3  #floor division discards fractional part
5
>>> 16 % 3  #the % operator returns the remainder
1

** Operator (very similar to power function): In Python, ** can be used to calculate powers:


>>> 5 ** 2  # 5 square is 25
25
>>> 2 **# 2 to the power of 4
16

Important: Since ** has higher precedence than --5**2 will be interpreted as -(5**2) and thus result in -25. To avoid this and get 25, you can use (-5)**2.

Assignment Operator ( = )

= (equal sign) is used to assign a value to a variable. Result is not displayed on the interactive prompt after assignment of the value.

>>> number1 = 20
>>> number2 = 30
>>> number1 + number2
50

Use of Undefined Variable

Trying to use undefined variables will result in an error. In following python code, we are trying to use num variable which is not defined so far. This will result in error as shown below:

>>> num  # trying to use an undefined variable
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
NameError: name 'num' is not defined

Operation on mixed data type operands (int + float)

Operators with mixed type operands convert the integer operand to floating point:

>>> 5 * 2.5
12.5

Important:

In interpreter, the last printed expression is assigned to the variable _. This means that when you are using Python as a simple calculator, it is easier to continue calculations, for example:

>>> sum=0
>>> n1=5
>>> n2=10
>>> n1 + n2
15
>>> sum + _
15

This variable should be treated as read-only by the programmer. Don’t explicitly assign a value to it ( _ variable).

In addition to int and float, Python supports other types of numbers, such as Decimal and Fraction. Python also has built-in support for complex numbers, and uses the j or J suffix to indicate the imaginary part (e.g. 3 + 5j).

No comments:

Post a Comment