Data Types
Ø Data Type represents the type of data present
inside a variable.
Ø In Python we are not required to specify the
type explicitly.
Ø Based on value provided, the type will be assigned
automatically.
Ø Hence Python is Dynamically Typed Language.
Python contains the
following inbuilt data types
1. int 4.bool 7.bytearray 10.tuple 13.dict 2. float 5.str 8.range 11.set 14. None 3.complex 6.bytes 9.list 12.frozenset
Note: Python
contains several inbuilt functions
1. type():To check
the type of variable
2. id():To get
address of object
3. print():To print
the value.
In Python
everything is an object
int Data Type:
Ø We can use int data type to represent whole
numbers (integral values)
Example: >>>
a=10 >>>
a=40+60
>>> type(a) >>> a
<class 'int'> 100
Note:
Ø In Python2 we have long data type to
represent very large integral values.
Ø But in Python3 there is no long type
explicitly and we can represent long values also by using int type only.
We can
represent int values in the following ways
1. Decimal form
2. Binary form
3. Octal form
4. Hexa decimal
form
1.
Decimal form(base-10):
Ø It is the default number system in Python.The
allowed digits are: 0 to 9
Example : >>>
d=528 >>>
d=1234a
>>> d >>>
d=1234a
528 File
"<stdin>", line 1
d=1234a
^SyntaxError: invalid syntax
2. Binary form(Base-2);
Ø The allowed digits are: 0 & 1.Literal
value should be prefixed with Ob or OB
Example :>>>
b=0b1111 >>>
b=0B123
>>> b File
"<stdin>", line 1
b=0B123
15 ^SyntaxError: invalid
digit '2' in binary literal3
3. Octal
Form(Base-8);
Ø The allowed digits are: 0 to 7,Literal value
should be prefixed with 00 or 00.
Example: >>>
o=0o1234 >>> o=0O143
>>> o >>> o
668 99
>>> o=0O148
File "<stdin>", line 1 o=0O148
^SyntaxError: invalid
digit '8' in octal literal
4. Hexa
Decimal Form(Base-16);
Ø The allowed digits are: 0 to 9, a-f (both
lower and upper cases are allowed) Literal value should be prefixed with Ox or
OX
Example:>>>
h=0x123f >>>
h=0X123e
>>> h >>>
h
4671 4670
>>>
h=0X14h
File "<stdin>", line 1 h=0X14h
^SyntaxError: invalid syntax
Note: Being a
programmer we can specify literal values in decimal, binary, octal and hexa
decimal forms. But PVM will always provide values only in decimal form.
2.float
Data Type:
Ø We can use float data type to represent
floating point values (decimal values)
Example :.>>>
f=1.234 >>> f = 1.2e3 >>> f = 3.4E2 >>> type(f)
>>> f >>> f >>> f <class
'float'>
1.234 1200.0 340.0
Ø We can also represent floating point values
by using exponential form (scientific notation
Ø Instead of 'e' we can use 'E'
Ø The main advantage of exponential form is we
can represent big values in less memory.
Note:
Ø We can represent int values in decimal,
binary, octal and hexa decimal forms. But we can represent float values only by
using decimal form.
>>>
f=0B11.01
File "<stdin>", line 1 f=0B11.01
^Syntax Error: invalid syntax
x = 35e3
y = 12E4
z = -87.7e100
print(type(x)) #<class
'float'>
print(x) #35000.0
print(type(y)) #<class 'float'>
print(y) #120000.0
print(type(z)) #<class 'float'>
print(z) #-8.77e+101
3. complex
Data Type:
Ø A complex number is of the form
a +
bj
Ø Real Part Imaginary Part
Ø a and b contain integers or floating point
values
Examples :3+5j 10+5.5j
0.5+0.1j
Ø In the real part if we use int value then we
can specify that either by decimal,octal,binary or hexa decimal form.
Ø But imaginary part should be specified only
by using decimal form.
>>>
a=0B11+5j
>>> a
(3+5j)
>>> a=3+0B11j
Syntax Error: invalid syntax
Ø Even we can perform operations on complex
type values.
1) >>>
a=10+1.5j
2) >>> b=20+2.5j
3) >>> c=a+b
4) >>>
print(c)
5) (30+4))
6) >>>
type(c)
7) <class
'complex'>
Note: Complex data
type has some inbuilt attributes to retrieve the real part and imaginary part
c=10.5+3.6j
c.real==>10.5 c.imag==>3.6
Ø We can use complex type generally in
scientific Applications and electrical engineering Applications.
4. bool
Data Type:
Ø We can use this data type to represent
boolean values. The only allowed values for this data type are:True and False
Ø Internally Python represents True as 1 and
False as 0
b=True
type(b)
=>bool
a=10
b=20
c=a<b
print(c)==>True
True+True==>2
True-False==>1
5. str
Data Type:
Ø str represents String data type.
Ø A String is a sequence of characters enclosed
within single quotes or double quotes.
s1='siddu' s1="siddu"
Ø By using single quotes or double quotes we
cannot represent multi line string literals.
s1="siddu
soft"
Ø For this requirement we should go for triple
single quotes("") or triple double quotes(""")
s1=’’’siddu soft’’’ s1="""siddu
soft"""
Ø We can also use triple quotes to use single
quote or double quote in our String.
""This is "
character""
'This is" Character'
Ø We can embed one string in another string ""This
"Python class very helpful" for java students""
ü In Python the following data types are
considered as Fundamental Data types
1. int 2. float
3. complex 4. bool 5
.str
ü In Python, we can represent char values also
by using str type and explicitly char type is not available.
Example:
>>> c='a'
>>> type(c)
<class 'str'>
ü long Data Type is available in Python2 but
not in Python3. In Python3 long values also we can represent by using int type
only.
ü In Python we can present char Value also by using str Type and explicitly char Type is not available.
No comments:
Post a Comment
Thank you Very Much.For Given Comment