Datatypes in Python


Data types define the type of data a variable can hold, for example an integer variable can hold integer data, a character type variable can hold character data etc.

DataType

Classification of datatype

  • List.
  • tuple.
  • Dictionary
  • String
  • Sets

Some more Datatype

  • int
  • float
  • char
  • complex
  • boolean

int

It contains integer value.

Example
x=2
print(type(x))

Here the output of above code is "<class 'int'>"

Note

Here "type()" is function return the datatype of the variable.

float

It contains integer Decimal value..

Example
x=2.0
print(type(x))

Here the output of above code is "<class 'float'>"

char

It contains integer Decimal value..

Example
x="e"
print(type(x))

Here the output of above code is <class 'char'>

booleans

Booleans represets the value in the term of "True" or "False".
it returns only any expression is given like if we compare two or more operends then the value return in the form of booleans

Example
print(5<2)
print(2==3)

Here the output of above code is
True
False

Many times it is used with "if-else".

Example
if 5>2:
    print("Small Code")
else:
    print("5 is less then 2")
Output
Small Code
bool() function

bool() function are used to evaluate any datatype into in term of "True" or "False".

Example
x=2
y="Small Code"
print(bool(x))
print(bool(y))
Output
True
True

Note

  • Any string except "" return True.
  • Any number except 0 return True..
  • Any datatype except (),{},[] returns True.

Complex Number

As we all known comlex number is in the form of "a+bj" here "a" is a real part and "bj" is a imaginary part.

Example
x=2+5j
print(type(x))
Output
<class 'complex'>
complex() function

complex() function are used convert any datatype to complex form.

Example
x=2
y=4.0
print(complex(x))
print(complex(y))
Output
(2+0j)
(4+0j)