Wednesday, 3 July 2019

Built-In Data Type


Built-in Data types (Predefined Data types/fundamental data types/Basic Data types)

1. int
2. float
3. complex
4. bool
5. None
6. sequences
7. sets
8. Mappings

a=2
int a=2 # error because data type should not be mentioned

Python is a Dynamic Type Language: 

Based on the value (data) type is assigned to the variable.

Number data type:

In Python there are three types of number data types are available:

They are:
1. int
2. float
3. complex

1. int
int:  

integer data type represents an integer. An integer number is a number without any decimal point or fraction part.
It is also called as Signed int (means positive number or negative numbers).
There is no 'long' integer in python.
For example:

100,5,-20, etc are treated as integer numbers.

Ex: 
a=-23

Here ‘a’ is called int data type variable since it is storing -23 which integer numeric value. There is no limit for the size of an int data type. It can store very large numbers.

type(): type()  is used to check the type of the variable.

Example:

a=10

id(): This function is used to find the address of the object.

id(a)àwhich displays the address of the object or address of the variable. i.e 1000 (here 1000 address of the variable (object), its my assumption)

type(): This function is used to check the type of the variable
Example:
b=10.2345
c=2+4j
type(b): which displays float type
type(c): which displays complex data

What is the range of integers?

An integer is not restricted by the number of bits and can expand to the limit of the available in the  memory
# range of integers in python

a = 30000000000000000000000000000000000000000000; 
print (a) 

"""output:
30000000000000000000000000000000000000000000

"""

2. Float:
The float data type represents floating point numbers (Real numbers). The number that contains a decimal point is called float.

Floats may also be in scientific notation, with E or e indicating the power of 10 (2.5e2 = 2.5 x 102 = 250).

Example:

0.5, -2.4578,145.09,0.00001,..etc are called float numbers

Example program on Float:

m=20
p=40
c=76
tot=m+p+c
avg=tot/3
print(avg)
45.333333333333336
print("%.2f"%avg)
45.33
print("%.2f" % round(avg,2))
45.33



3. Complex :
A complex number is a number that is written in the form of a+bj or a+bJ.
Here ‘a’ real part
          ‘b’ imaginary part
The suffix ‘j’ or ‘J’ after ‘b’ indicates the square root value of -1.
The parts ‘a’ and ‘b’ may contain integer or floats.

Example:
3+5j
10+2.6J  
0.6+0.2j 

Ex 2:
 a=2+3i #SyntaxError: invalid syntax, we have to give imaginary part with suffix 'j '

#This program explains complex data types
#Add two complex numbers
c1=3.5+5.2j
c2=2.0+1.3j
c3=c1+c2
print("sum=",c3)
print(type(c3))
print(id(c3))

"""output
F:\Programs\Datatypes>python compl.py
sum= (5.5+6.5j)

F:\Programs\Datatypes>python compl.py
sum= (5.5+6.5j)
<class 'complex'>

F:\Programs\Datatypes>python compl.py
sum= (5.5+6.5j)
<class 'complex'>
11453240

F:\Programs\Datatypes>"""

We can use complex data type generally in scientific application and electrical engineering applications.

Ex:

a=2+3j
a.real=2.0
a.imag=3.0

real, imag predefined attributes (variables/properties)

4. bool

Ex:
a=True
b=False
c=true # Error because it is not bool data type and we gave lower case letter 't'
d=false #Error because we gave lower case letter 'f'
type(a) # Find the type of a
output:
<class 'bool'>


We can use this data type to represent Boolean (True/False) values. It only allows True or False
Internally Python stores True as 1 and False as 0 (Zero) in the memory.

Example:

b=True
type(b)->bool

Ex2:
 a=10
 b=20
 c=a>b
 print(c)

"""output"""
False

5. None Data type:

In python, None means nothing or No value associated. The ‘None’ datatype represents an object that doesn’t contain any value. In java/c it is called ‘null’. It is used for as a default value.

Where do we use None data type?
If the value is not available, then to handle such type of situation None is used.

Example program on None
def ex():
   a=10

print(ex())
 None

Youtube channel click here to see this video
Next Topic:      Sequences data types
Previous Topic: Data types



You may like the following posts:


Related Videos:
https://youtu.be/ghJ8t7oKwoU

No comments:

Post a Comment

Files with Exception handling

#Ask the user to take two input integer values var a,b try:     a=int(input("enter any integer value:"))     b=int(input(...