datatypes in python

Datatypes introduction:
Datatypes represent the type of data that we are using is known as a datatype. It helps to understand what kind of operations can be performed on a value and it also represents the type of data stored inside a variable.
since we know that python is a dynamic programming language that means hare we no need to declare the type explicitly i.e., it is automatically considered based on your provided data by the python virtual machine(PVM).
where in C,C++,java we have to define the type so these type of languages are called as statically typed language.
the fundamental data types are:
  • int
  • float
  • complex
  • bool
  • str
1. int datatype:
this type of datatype represents the data which has numeric value.
that is the numbers without decimal point is known as integers.
if we want to represent integral numbers int datatype is used.
example:
a = 100
here we did not need to declare the type explicitly i.e., it is automatically considered based on your provided data by the python virtual machine(PVM).
type() function is a built-in function to know the type of data.

example:
a = 100
print(type(a))
output:
<class 'int'>

if we want to know where the object is stored i.e., to know the address of the object we have a built-in function id( ).
a = 100
print(type(a))
print(id(a))
output:
<class 'int'>
2440165086672

note:
in other languages like java they have four types to represent integral values such as
byte
short
long
int

but in python we have only one type even how long the size of integer it is int type only.
example:
a = 100
b = 199999999999999999999999999
print(type(a))
print(type(b))
output:
<class 'int'>
<class 'int'>

to represent int values we have 4 possible ways they are..
1. Binary 
2. Decimal
3.Octal
4.Hexaadecimal

1.Binary form:(Base-2)
It allows only 0's and 1's
example:
a = 10101
print(a)
output:
10101
it is not treated as binary number because the default number is always considered as decimal only
but to specify it as a binary number then we have to specify the number prefixed with 0b or 0B.
that is if a number is prefixed with 0b or oB this number is considers as binary number.
example:
a = 0b10101
print(a)
output:
21
now you can get a doubt why the output is 21 even though if we specify it as a binary number means
that is the default number system is decimal 
so it converts binary number a = 0b10101  to decimal and then prints on screen with the help of print().

2.Decimal form:(base-10)
this is  the default number system.
it allows digits upto 0 to 9.
Ex: 123

3.Octal form : (base 8)
 it allows digits upto 0 to 7 only.
Remember the literal value should starts with 0o or 0O (zero capital O or  Small o).
Ex: this a=0o123 octal into decimal will be converted. and then prints on screen with the help of print().
a=0o123
print(a)
output:
83

4.Hexadecimal: (base-16)
It allows digits from 0 to 9 and there after A to f either capital or small A to F.
where,
A = 10
B = 11
C = 12
D = 13
E = 14
F = 15
The literal should be prefixed with 0X or 0x ie, Zero & Small x or Capital x
example:
a=0x10
print(a)
output: 
16
note:
If you observe, even though if we entering input in decimal or binary or octal or hexadecimal but the output was always generates only in decimal by the python virtual machine.
but if you want the output in other ways like binary or octal or hexadecimal then we can achieve it by using base conversions.
i.e., to convert from one base to another base for this python provides three built-in functions
1.bin( )
2.oct(  )
3.hex( )

example:
bin(15)
output:
0b1111

print(oct(177))
output:
0o261

print(hex(12345))
output:
0x3039

2. Float datatype:
It is a real number with floating point representation and it is specified by a decimal point.
that is the number with decimal point is considered as floating point values.
example:
floatvalue = 1.2
print(type(floatvalue))
output:
<class 'float'>

we can also represent floating point numbers in exponential form i.e, by using e or E
floatvalue = 1e3
print(type(floatvalue))
print(floatvalue)
output:
1000.0

note:
Floating points is applicable only in decimal values but not in binary,octal or hexadecimal.
If you try in binary,octal or hexadecimal. it will throws an error.

3. Complex datatype:
This is the special datatype in python because other languages like java,C,C++ it doesn't contains this type of datatype so it is a special datatype.
this type of datatype is useful for developing scientific applications
the syntax for this datatype is a+bj
where a is a real part and b is a imaginary part and j is  j = √-1 and j² = -1
Example:
a = 1+2j
print(type(a))
output:
<class 'complex'>

To know the real and imaginary parts we have 
a = 1+2j
print(type(a))
print(a.real)
print(a.imag)
output:
<class 'complex'>
1.0
2.0

note:
We can take int values or float values in real and imaginary parts 
a = 1+2j
and
a  = 10+23.2j

we can take binary,octal or hexadecimal in real part but in imaginary we should take only decimal values 
Example:
a = 0b111+20j

remember that we can also perform arithmetic operations between two complex numbers

4. Bool datatype:
This type of datatype used to represent logical values such as true and false
this datatype consist of only two values True and Flase.
It should be started with capital i.e, True and Flase otherwise it throws an error
example:
a = true
print(a)
output:
    a = true
NameError: name 'true' is not defined

where is we define in capital then
a = True
print(a)
output:
True

To check the type
a = True
print(a)
print(type(a))
output:
True
<class 'bool'>

a small program to get understand about bool datatype
program:
a = 10
b = 20
c = a>b
print(c)
output:
False

Internally the value of True is 1 and false is 0.
print(True+True)   #2
print(True+False) #1
print(True*True) #1
print(True-True) #0
print(True/True) #1.0
print(False+False) #0
print(False-True) #-1
print(False+True) #1
print(False-False) #0
print(False*False) #0

5. str datatype:
string datatype is the most common datatype in any language.
A string is a collection or sequence of one or more characters enclosed  in single quote, double-quote or triple quote.
where other languages like java single char with single quote is char datatype but not in python i.e, in python there is no char datatype.a char is a string of length one. and it is represented by str.
Example:
s= "zaheer"
print(type(s))
print(s)
output:
<class 'str'>
zaheer

we can use single or double quotes or triple quotes
Example:
s= 'zaheer'
print(type(s))
print(s)
output:
<class 'str'>
zaheer

note:
single and double quotes are useful for single line string 
but to define multiline string we should have to use triple quotes
Example:
name = """zaheer
    this is a multiline
    string example"""
print(name)

output:
zaheer
    this is a multiline
    string example

and triple quotes are very useful to use single quote and double quotes as a normal characters in string.
Example:
name = """ 'python' is "easy" """
print(name)
output:
 'python' is "easy" 

etc..are the fundamental datatypes in python.

note:
i hope you like the content. like and comment your opinion and dont forget to subscribe..

No comments:

Post a Comment