You are on page 1of 3

1.

Datatype in Python

1.1 numeric types

In [3]: 1 x = 10
2 type(x)

Out[3]: int

In [4]: 1 y = 32.4
2 type(y)

Out[4]: float

In [5]: 1 z = 4+9j
2 type(z)

Out[5]: complex

1.2 Sequence type

1.2.1 Strings

In [8]: 1 x = "apple" # double qoutes


2 type(x)

Out[8]: str

In [9]: 1 x = 'apple' # single qoutes


2 type(x)

Out[9]: str
In [13]: 1 x = """apple""" # triple quotes : docstring
2 type(x)

Out[13]: str

1.2.2 List

In [18]: 1 # list
2 x = ["apple", "orange","grapes"] # square bracets
3 type(x)

Out[18]: list

1.2.3 Tuple

In [17]: 1 x = ("apple", "orange","grapes") # plain brackets


2 type(x)

Out[17]: tuple

1.2.4 Set

In [21]: 1 x = {1,2,3,4,5} # curly brackets


2 type(x)

Out[21]: set

1.2.5 Dictionary

In [23]: 1 # a key value pair


2
3 x = {101: "sunil", 102: "rajat", 103 : "richa"}
4
5 type(x)

Out[23]: dict

1.2.6 Boolean

In [30]: 1 type(True)

Out[30]: bool

1.2.7 NoneType

In [32]: 1 type(None)

Out[32]: NoneType
Casting

In [34]: 1 x = str(3) # x will be '3'


2 y = int(3) # y will be 3
3 z = float(3) # z will be 3.0
4
5 print(type(x))
6 print(type(y))
7 print(type(z))
8

<class 'str'>
<class 'int'>
<class 'float'>

In [39]: 1 x = int(1) # x will be 1


2 y = int(2.8) # y will be 2
3 z = int("3") # z will be 3

In [40]: 1 x = float(1) # x will be 1.0


2 y = float(2.8) # y will be 2.8
3 z = float("3") # z will be 3.0
4 w = float("4.2") # w will be 4.2

In [41]: 1 x = str("s1") # x will be 's1'


2 y = str(2) # y will be '2'
3 z = str(3.0) # z will be '3.0'

In [42]: 1 print(x);print(type(x))
2 print(y);print(type(x))
3 print(z);print(type(x))

s1
<class 'str'>
2
<class 'str'>
3.0
<class 'str'>

You might also like