Data Types in Python
Data Types in Python
Topics to be discussed
a a b
Contd..
None type
numeric types
sequences
sets
Mappings
None type
4. a=int("12",16)
print(a)
print(hex(a),type(a))
output
18
0x12 <class 'int'>
Int contd..
5. a=int("10",8)
print(a)
print(oct(a),type(a))
Output
8
0o10 <class 'int'>
Integer constants
print(float())
print(float(10))
print(float("25"))
x=33
X=float(x)
print(float(x),type(x))
Output
0.0
10.0
25.0
33.0 <class 'int'>
Complex data type
c=2+3j
s=complex(-4,6.0)
print(s,type(s))
print(c,type(c))
print(c.real,c.imag)
Output
0j <class 'complex'>
(2+3j) <class 'complex'>
2.0 3.0
complex() as type converter
x=complex(2,3)
print(x,type(x))
y=complex("2")
print(y,type(y))
z=complex(2)
print(z,type(z))
a=complex(-4.5,0.6)
print(a,type(a))
b=complex("2+3j")
print(b,type(b))
Output
(2+3j) <class 'complex'>
(2+0j) <class 'complex'>
(2+0j) <class 'complex'>
(-4.5+0.6j) <class 'complex'>
(2+3j) <class 'complex'>
bool data type
bool data type allows us to store boolean
value True or False in boolean object. True
and False are python keyword. Objects of
type bool can be created using bool
constructor which also converts int, float and
string to bool type.
x=True
print(x,type(x))
y=bool()
print(y,type(y))
output
True <class 'bool'>
False <class 'bool'>
x=bool(1)
print(x,type(x))
y=bool("hello")
print(y,type(y))
z=bool(-3.5)
print(z,type(z))
x1=bool(0)
print(x1,type(x1))
y1=bool("")
print(y1,type(y1))
z1=bool(0.0)
print(z1,type(z1))
Output
True <class 'bool'>
True <class 'bool'>
True <class 'bool'>
False <class 'bool'>
False <class 'bool'>
False <class 'bool'>
Sequence Data type
string
list
tuple
set
range
byte
bytearray
dcitionary(mapping)
Contd
s=str(10)
print(s,type(s))
a=str(10.45)
print(a,type(a))
b=str(3+4j)
print(b,type(b))
c=str()
print(c,type(c))
Output
10 <class 'str'>
10.45 <class 'str'>
(3+4j) <class 'str'>
<class 'str'>
String constants in python
elif in True
else is try
THANK YOU