Tech Study

Python Numbers With Examples

Python Numbers

In python, the number data types are used to store numeric values.

Python supports three numeric data types:

  • int –  negative and non-negative numbers having no decimal point
  • float –  used to store decimal point value 
  • Complex – It is of the form a + bj in which a is the real part and the in bj j j is the imaginary one.

      Let’s understand one by one with the examples:

  • int: they are whole numbers, the negative and non-negative numbers having no decimal point. There is no limit to integers in Python.

Python Numbers With Examples

Example 1

x = 2  
print(x)
print (type(x))

 output: 2

 <class ‘int’>

type(): it is a function through which we specify the type of the object, whether it is int, float, or double.

Float: used to store floating point numbers or decimal point numbers, it helps in storing the result which has decimal point number like 4.34, 4.56788.

Example 2

  Divide a number by a number  produces a float

a= 4/5

b= 7/9

c= 9/10

d= 2.33/1.22




print(a)

print(b)

print(c)

print(d)




print(type(a))

print(type(b))

print(type(c))

print(type(d))

Output 0.8

0.7777777777777778

0.9

1.9098360655737705

<class ‘float’>

<class ‘float’>

<class ‘float’>

<class ‘float’>

1.909836065573770 has  many digits after decimal so, 

Note: accuracy of the float point number is up to 15 decimal places

Example 3:

Float is produced by multiplication by multiplying two float numbers,

a =  7.0 * 9 .0

print(a)

print(type(a))

output:

63.0

<class ‘float’>

  • Complex: The complex number consists of two parts the real and the imaginary one.

Example 4:

 3 + 4j

The 3 is the real component and 4j is the imaginary one(j is the imaginary part)

Example 5: 

a =  4 + 4j
print(type(a))

 Output: 

 <class ‘complex’>

Random Number in Python:

Python has a built-in module which is known as random which is used to generate random numbers.

For using built-in modules we have to import the modules through import.

Syntax:

import module name

we will use randrange() method which returns a randomly selected element from the given range.Let’s understand this by the example

Example 6:

import random

print(random.randrange(1,10))

so , it will return a random number between 1(included) to 9(not included).

Output:

7

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more