Python Data Types

Python is dynamically typed language i.e we don’t need to provide the variable type while declaring it, it is implicitly bounded by the interpreter.  Python provides five types of Standard Data Types. They are:

  • Numbers
  • Boolean
  • Dictionary
  • Set
  • Sequence Type

Python Data Types

Numbers

Numbers represents the numeric values. It includes integer type numbers, complex numbers and float numbers.

  • Integer (int): It includes positive as well as negative numbers ( 2, 555 , -80). It can be of any length.
  • Complex number: It contains ordered pair numbers i.e. in form x + iy, where x is a real number and y is an imaginary number. Example, 2 + 3i, 6i etc.
  • Float: It stores floating-point numbers and the length is up to 15 decimal. Example, 2.568 etc.

NOTE: To know the type of data, type() function is used. To check whether an object belongs to particular class isinstance() function is used.

Example: a = 2

print(“ Type of a”, type(a))

print(“ Is a is integer :”, isinstance(2, int)

OUTPUT: Type of a <class, ‘int’>

Is a is integer : True

Boolean

Boolean check the statement whether it is true or false. It provides two built-in values, true and false and belongs to class bool. True is represented by non-zero or ‘T’ and false is represented by zero or ‘F’.

Example, print(“Type:”, type(True))

OUTPUT: Type: <class, ‘bool’>

Dictionary

A dictionary stores the unordered set of objects in key-value pair. The key is primitive data type and value holds the Python object. Similar to the hash table where the key holds the value specified to it.

Example, dict = { 1:’Pyhton’ , 2:’Java’, 3:’C++’ }          “Creating dictionary

print(dict)                                                    “Printing dictionary values

print(dict.keys())                                          “Printing key values

print(dict.values())                                       “Printing values associated to keys

OUTPUT:

1:’Pyhton’ , 2:’Java’, 3:’C++’

dict_keys([ 1, 2, 3])

dict_values([ ‘Python’ , ‘Java’ , ‘C++’])

 

Set

A set is an unordered collection of data created using the built-in function set(). It contains various data types separated by commas.

Example, set1 = {‘Ram’, ‘25’ , ‘Python’}                      “Creating set

print(set1)                                                   “Printing value of set

add.set(‘Employee’)                                   “Adding element to set

print(set1)

remove.set(25)                                           “Removing value from set

print(set1)

OUTPUT:

‘Ram’, 25 , ‘Python’

‘Ram’, 25 , ‘Python’ , ‘Employee’

‘Ram’, ‘Python’ , ‘Employee’

Sequence Type

  • Strings: A string is the sequence of characters enclosed under quotation mark (“ “).

Note: We can use ‘ + ‘ for string concatenation, and ‘ * ‘ for string repetition.

Example, str = “This is a string”

  • List: A list is the collection of objects of different data types. It is same as arrays in C.

Example, lis = {‘Python’ , 2 }

Note: We can use ‘ + ‘ for list concatenation, and ‘ * ‘ for list repetition.

  • Tuple: Similar to list, tuple is the collection of objects of different data types. In tuples, we can’t modify the values and the size of the tuple.

Example, tup = {‘Python’ , 2 }

 

Note: We can use ‘ + ‘ for tuple concatenation, and ‘ * ‘ for tuple repetition. But tuple does not support item assignment.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.