# Data types in Python

The program we create need to store the data, Python defines various data types to store different types of values. Data types are classified into below sub-types, these are

•     Numbers

•     Sequence

•     Map

•    Set

These types can be further classified as shown below

Numbers                    Sequence                   Map                           Set

Integer                       String                  Dictionary             Set

Float                           List                                                     Frozen set

Complex                     tuple

Bool

## The other systems you need to prepend to integer value as shown in the below table.

 Prefix Meaning Base 0b (zero + lowercase letter 'b') 2 0B (zero + uppercase letter 'B') 0o (zero + lowercase letter 'o') 8 0O (zero + uppercase letter 'O') 0x (zero + lowercase letter 'x') 16 0X (zero + uppercase letter 'X')

## print(0b10)

# decimal equivalent of binary 10

#Output: 2

print(0B10)

# decimal equivalent of binary 10

#Output: 2

print(0o16)

# decimal equivalent of octal 16

#Output: 14

print(0O16)

# decimal equivalent of octal 16

#Output: 14

print(0x14)

# decimal equivalent of hexadecimal 14

#Output: 20

print(0X14)

# decimal equivalent of hexadecimal 14

#Output: 20

Output

2

2

14

14

20

20

The integer in python  is specified using class int , same can be verified using type() function for example

#Output

print(type(2.))

#Output

print(type(.2))

#Output

print(.2e6

#Output 200000.0

#Output 0.5

## Output

200000.0

0.5

Scientific notation expresses real numbers that are too large or too small to be conveniently written in decimal form. Here is the table that  demonstrates how to convert  decimal notation into scientific nation

 Decimal notation Scientific notation 2 2×100    or  2 e0 300 3×102   or   3 e2 −53000 −5.3×104  or  -5.3 e4 0.2 2×10−1  or  2 e-1 987 9.87×102   or  9.87 e2 0.00000000551 5.51×10−9  or  5.51 e-9

## Complex Numbers

A Complex number is specified as <real number>`+`<real number>j

Example : Complex number

com_num =  2 + 3j

print(com_num)

#Output : (2+3j)

com_num =  2.0 - 3.3j

print(com_num)

#Output : (2-3.3j)

Output

(2+3j)

(2-3.3j)

Example : Complex number using complex function

com_num = complex(2,3)

print(com_num)

#Output : (2+3j)

com_num = complex(-2,3)

print(com_num)

#Output : (-2+3j)

com_num = complex(2)

print(com_num)

#Output : (2+0j)

Output

(2+3j)

(2-3.3j)

(2+0j)

## Bool (Boolean Data type)

In Python, Boolean data are defined by the `True` and `False` keywords.

Example: Bool data type

x = True

y = False

type(x)

<class 'bool'>

​​​​​​​X = True

Y = False

The operators used for the below operators:

•          or

•          and

•          not

•          == (equivalent)

•          != (not equivalent)

The below table demonstrates the use of  operators with boolean values

 X Y not X X == Y X = ! Y X or Y X and Y T F F F T T F F T T F T T F T T F T F T T F F T T F F F

## string1 ="Hello world"

string2 ='Python'

print(string1)

#Output: Hello world

print(string2)

#Output: Python

type(string1)

<class,'str'>

string3 ='123'

string4 ='Bill Gates'

print(string3)

#Output: 123

print(string4)

#Output: Bill Gates

Output

Hello world

Python

123

Bill Gates

## Lists

A list is a data type is a collection of elements of different data types.  A list contains an element in a square brackets `[ ]` where its elements are commas separated.

`​​​​​​​A few points to ponder`

•  List is a mutable object that means we can add or remove its elements.

•  Lists are dynamic which means the list grows if an element is added and shrinks if an element is deleted.

•  Lists are ordered.

•  Elements in a list can be accessed by slicing and index.

•  The index of the list starts with 0

•  Lists can be nested to arbitrary depth.

`Example: list`

mylist = [ 'a', 2, 3.14159 ]

print(mylist)

#Output ['a', 2, 3.14159]

type(mylist)

# Output

Output

['a', 2, 3.14159]

Tuples

Tuples are similar to a list, however, tuples are immutable. A tuple contains an element in a square brackets `[ ]` where its elements are commas `separated``.`

A few points to ponder

•  Elements of a tuple can  accesses only, they cannot be modified.

Example: tuple

_tuple = (1,'Python',[1,2,3])

print(_tuple)

#Output (1, 'Python', [1, 2, 3])

type(_tuple)

#Output:

Output

(1, 'Python', [1, 2, 3])

## Dictionary

`A ``Dictionary ``is a collection of key-value pairs, each`` key-value pairs are commas separated and key-value pair is sep``arated`` by a colon. key-value pairs are enclosed in curly braces { }.`

A few points to ponder

`•``  ``A dictionary consists of a key and its associated value. `

`•``  ``A key is used to lookup and return its associated value. `

`•   A key must be immutable whereas value can be any type. `

`•`` ``The integers, strings, or tuples can be key, but not lists, because lists are mutable.`

`•``  Dictionary is `` mutable.`

`•`` ``An item(key-value pair) can be added, removed, and changed any time.`

`• A key is always unique. You`` can't have two items with the same key.`

Example: Dictionary

_dict = {1:'a', 2:'b',3:'c'}]

print(_dict)

#Output {1: 'a', 2: 'b', 3: 'c'}

marks = {'akash':90,'Dinesh':87,'Manish':95,'yash':99}

print(marks)

#Output{'akash': 90, 'Dinesh': 87, 'Manish': 95, 'yash': 99}

contacts= {'ashish':9876543210, 'Sanjay':[9999911111,9898989898]}

print(contacts)

#Output {'ashish': 9876543210, 'Sanjay': [9999911111, 9898989898]}

Output

{1: 'a', 2: 'b', 3: 'c'}

{'akash': 90, 'Dinesh': 87, 'Manish': 95, 'yash': 99}

{'ashish': 9876543210, 'Sanjay': [9999911111, 9898989898]}

## Python Sets

`A set is an unordered collection of values. A set is created by placing elements inside curly braces {}, separated by comma.`

A few points to ponder

`•``   ``Every element in  a set is unique. `

`•``   ``Set does not have contains duplicate values`

`•``   ``Sets are mutable`

`•``   ``Sets can perform mathematical set operations like union, intersection, symmetric ``  ``difference, etc.`

`•``   ``But a set cannot have mutable elements like lists, sets or dictionaries as its elements.`

Example: Set

_set = {"Hello",1.0, (5, 6, 7)}

​​​​​​​print(my_set)

# Output {"Hello",1.0, (5, 6, 7)}

Output

{"Hello",1.0, (5, 6, 7)}