Numbers in Python

Numbers in Python


Number data type stores Numerical Values. This data type is immutable i.e. value of its object cannot be changed (we will talk about this aspect later). These are of three different types:

a) Integer & Long

b) Float/floating point

c) Complex

Range of an integer in Python can be from -2147483648 to 2147483647, and long integer has unlimited range subject to available memory.

1.1 Integers are the whole numbers consisting of + or – sign with decimal digits like 100000, -99, 0, 17. While writing a large integer value, don‟t use commas to separate digits. Also integers should not have leading zeros.

When we are working with integers, we need not to worry about the size of integer as a very big integer value is automatically handled by Python. When we want a value to be treated as very long integer value append L to the value. Such values are treated as long integers by python.

>>> a = 10
>>> b = 5192L #example of supplying a very long value to a variable
>>> c= 4298114
>>> type(c) # type ( ) is used to check data type of value
<type 'int'>
>>> c = c * 5669
>>> type(c)
<type 'long'>

Integers contain Boolean Type which is a unique data type, consisting of two constants, True & False. A Boolean True value is Non-Zero, Non-Null and Non-empty.

Example

>>> flag = True
>>> type(flag)
<type 'bool'>

1.2 Floating Point: Numbers with fractions or decimal point are called floating point numbers.

A floating point number will consist of sign (+,-) sequence of decimals digits and a dot such as 0.0, -21.9, 0.98333328, 15.2963. These numbers can also be used to represent a number in engineering/ scientific notation.

-2.0X 105 will be represented as -2.0e5

2.0X10-5 will be 2.0E-5

Example

y= 12.36

A value when stored as floating point in Python will have 53 bits of precision.


1.3 Complex: Complex number in python is made up of two floating point values, one each for real and imaginary part. For accessing different parts of variable (object) x; we will use x.real and x.image. Imaginary part of the number is represented by „j‟ instead of „i‟, so 1+0j denotes zero imaginary part.

Example

>>> x = 1+0j
>>> print x.real,x.imag
1.0 0.0

Example

>>> y = 9-5j
>>> print y.real, y.imag
9.0 -5.0


Qus. 1 : <p>What will be the datatype of the var in the below code snippet?</p><pre><span style="font-size: 14px;">var = 10<br></span><span style="font-size: 14px;">print(type(var))<br></span><span style="font-size: 14px;">var = "Hello"<br></span><span style="font-size: 14px;">print(type(var))</span></pre>

  1. Str and int
  2. int and int
  3. str and str
  4. int and str

Programs

python program to input a welcome message and print it

View Solution


python program to print number of seconds in year

View Solution


Python program to calculate GST Goods and Service Tax

View Solution


python program to find the ASCII Value of entered character

View Solution


python program that display a joke but display the punch line only when the user presses enter key

View Solution


python program to generate the following output 5 10 9

View Solution


python program to print 5@10@9

View Solution


Latest Current Affairs 2024 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online MBA 2 years Online MCA Online BCA Best Website and Software Company in Allahabad