Python – Data Types

In this article, we will discuss data types in Python in detail

In-built Data types:

There are 14 in-built data-types in Python as listed below,

  1. int
  2. float
  3. complex
  4. bool
  5. str
  6. bytes
  7. bytearray
  8. range
  9. list
  10. tuple
  11. set
  12. frozenset
  13. dict
  14. None

Python provides few in-built function :

  • print() – to print to terminal
  • type() – to understand data-type of value assigned to variable
  • id() – to identify address of a variable

1. int data-type :

  • The number without decimal point value or whole number is known as integral value
  • int data-type is used to represent these integral values
  • Programmer can represent/write integral values in 4 different forms as mentioned below 1.1
  • But Python always display integral values in decimal form only
  • Unlike other programming languages, Python doesn’t reserve size or range for int data-type, as everything in Python is Object and size of object in any programming language is not fixed
  • int data-type is immutable

1.1 Integer values can be represented in 4 forms

  1. Decimal form (base=10)
    0 to 9
    Eg.; a = 1089
  2. Binary form (base=2)
    0 and 1
    Eg.; b = oB1111 or 0b1111 (Zero followed upper/lower case B)
  3. Octal form (base=8)
    0 to 7
    Eg.; c = 0O7541 or 0o7541 (Zero followed upper/lower case O)
  4. Hexa-decimal form (base=16)
    0 to 9 and A to F or 0 to 9 and a to f
    Eg.; d = oX8087 or 0xA158 (Zero followed upper/lower case X)

1.2 Base conversion of integral values:

There are few in-built function available for conversion of different bases of integral values, as mentioned below,

  1. bin(val)
  2. oct(val)
  3. hex(val)

2. float data-type :

  • The number with decimal value is known as floating point value
  • float data-type is used to represent these floating point values
  • exponential value like 1.2e3 can be represented using float data-type
  • float data-type is immutable

3. complex data-type :

  • Addition of real part and imaginary part is known as complex number
  • Alphabet ‘j‘ is always attached to imaginary part at the end and its value is Square Root of -1
  • For example, (a + bj) is a complex number where ‘a‘ real part and ‘b‘ is imaginary part
  • Real part can be represented in decimal, binary, octal or hexa-decimal forms
  • But imaginary part can be represented in decimal form only
  • Operation like addition, subtraction, etc. are allowed
  • There are in-built attributes available to retrieve real & imaginary parts from complex number
  • For example, a = 10+20j is a complex number then we can retrieve real & imaginary part like a.real and a.imag which retrieves 10 & 20 respectively
  • complex data-type is immutable

4. bool data-type :

  • Allowed values for boolean data-type are True and False
  • Internally, True & False values are represented as 1 & 0 respectively
  • So, True + True returns value as 2 and True + False returns value as 1
  • bool data-type is immutable

5. str data-type :

  • String is any sequence of characters
  • str data-type is used to represent it
  • We can use either single-quotes or double-quotes to represent string literal in single line in Python like s=’Bench’ or s=”Resources” (recommended to use single-quotes which is Python’s default)
  • To represent multi-line string literals in Python, we can use triple single-quotes or triple double-quotes
  • st[index] is used to access specific character at that particular index-position where st is any string and index is valid number within range
  • If index is out of range then IndexError is thrown stating “string index out of range
  • Both positive & negative index are possible in Python for str data-type
  • str data-type is immutable

5.1 Slice operator :

  • Slice is basically a piece or part from whole like piece of apple is called as slice
  • Slice operator in Python is very similar to substring in Java
  • This is very commonly used operator across Python programs for various operations and it is not restricted to string
  • Syntax : st[begin:end] which returns partial string from begin index to end-1 index
  • Both begin & end is optional and in this case complete string will be returned
  • st[begin:], st[:end] and st[:] all are valid to use and no error will be thrown
  • st[begin:] returns string from begin index to end of string
  • st[:end] returns string from start of string to end-1 index
  • st[:] returns complete string
  • If begin-index or end-index is out of range then there will be no IndexError is thrown
  • In addition to begin-index & end-index, we can provide step parameter for how many characters to jump and Python’s default is 1 for step parameter
  • Syntax : st[begin:end:step]
  • For example, st = ‘Malayalam’ then st[0:9:2] returns Mlylm

Trivia :

  • Data-types such as int, float, complex, bool, str discussed so far are Python’s fundamental data-types
  • These fundamental data-types are immutable
  • char data-type in other prominent language is represented as str data-type only in Python
  • Likewise, long data-type is represented as int data-type only in Python

6. Type-casting or Type-Coersion :

There are few in-built function available in Python for type-conversion, as listed below,

  • int()
  • float()
  • complex()
  • bool()
  • str()

6.1 int() function :

  • Type conversation from other fundamental data-types to int data-type is possible using int() function
  • float to int conversion –> int(123.456) ==> 123
  • complex to int conversion results in TypeError stating “can’t convert complex to int
  • bool to int conversion –> int(True) ==> 1 and int(False) ==> 0
  • str to int conversion –> int(’10’) ==> 10
  • While doing string to int conversion, value provided inside parenthesis should be in int value of base-10 form, otherwise ValueError will be thrown stating “invalid literal for int() with base 10
  • Other form of int value in octal, binary & hexa-decimal aren’t allowed and ValueError will be thrown for trying those conversions

6.2 float() function :

  • Type conversation from other fundamental data-types to float data-type is possible using float() function
  • int to float conversion –> float(123) ==> 123.0
  • complex to float conversion results in TypeError stating “can’t convert complex to float
  • bool to float conversion –> float(True) ==> 1.0 and float(False) ==> 0.0
  • str to float conversion –> float(’10’) ==> 10.0 and float(‘10.5’) ==> 10.5
  • While doing string to float conversion, value provided inside parenthesis should be in decimal form of base-10, otherwise ValueError will be thrown stating “could not convert string to float:
  • float(‘ten’) results in ValueError stating above reason

6.3 complex() function :

  • Type conversation from other fundamental data-types to complex data-type is possible using complex() function
  • There are only 2 forms are available for complex() function viz.; complex(x) and complex(x, y)
  • In 1st form, x is considered as real part and it is equivalent to x+0j
  • In 2nd form, x is real part & y is imaginary part and it is equivalent to x+yj
  • int to complex conversion –> complex(10) returns (10 + 0j)
  • float to complex conversion –>complex(10.5) returns (10.5 + 0j)
  • bool to complex conversion –>complex(True) returns (1 + 0j) and complex(False) returns 0j
  • str to complex conversion –> complex(‘10.5’) returns (10.5 + 0j)
  • non-numeric str to complex conversion –> complex(‘ten’) throws ValueError stating “complex() arg is a malformed string
  • str number in binary form to complex conversion –> complex(‘0B1111’) throws ValueError stating “complex() arg is a malformed string
  • Example to understand complex() conversion
    • complex(10, 20) returns (10 + 20j)
    • complex(10, 20.5) returns (10 + 20.5j)
    • complex(’10’, ’20’) throws TypeError stating “complex() can’t take second arg if first is a string

6.4 bool() function :

  • Type conversation from other fundamental data-types to bool data-type is possible using bool() function
  • int to bool conversion –> bool(0) returns False and bool(10) returns True
    • For int to bool conversion, if input passed is ZERO then it is False and for non-ZERO value it is True
  • float to bool conversion –> bool(0.0) returns False and bool(0.8) returns True
    • For float to bool conversion, if input passed is ZERO considering decimal values then it is False otherwise it is True
  • complex to bool conversion –> bool(0 + 0j) returns False and bool(10 + 20j) returns True
    • For complex to bool conversion, if both real part & imaginary part is ZERO then it is False and if either of the real part or imaginary part is non-ZERO then it is True
  • str to bool conversion –> bool(”) returns False and bool(‘Bench’) returns True
    • For str to bool conversion, if input is empty string i.e.; 2 continuous single-quotes or 2 continuous double-quotes then it is False otherwise it is True even with empty string with spaces in between them

6.5 str() function :

  • Type conversation from other fundamental data-types to str data-type is possible using str() function
  • Unlike other 4 function discussed above, there are no restriction while converting other data-types to string data-type
  • For example, str(10 + 20j) returns ‘(10+20j)’, notice single-quotes which is Python’s default convention to represent string

Immutable v/s Fundamental data-types :

  • Any object whose value cannot be changed after creating it is known as Immutable object
  • All fundamental data-types like int, float, complex, bool, str are Immutable objects
  • Immutable objects utilizes memory well thereby improving performance of the application
  • If there are 3 objects with same value content like,
    • c1=’Mum’
    • c2=’Mum’
    • c3=’Mum’
    • Then there will be only one object created in the memory called ‘Mum‘ and 3 references viz.; c1, c2, c3 referencing the same object ‘Mum
  • In other way, same value content is referenced by many objects and change in value will bring inconsistency, so to overcome this scenario we can go for immutable object where value content will never be changed
  • To verify the address of all references we can use Python’s in-built function id(variableName) like id(c1), id(c2) & id(c3)
  • To check whether 2 references pointing to same object, we can use ‘is‘ operator like (x is y) which returns Boolean value True or False depending upon whether both references pointing to same object or not
  • For str data-type, same string values share same memory location utilizing memory which improves performance of the application
  • For int data-type, whenever we define 2 objects with same value and it is within a range of 0 to 256, then these values stored in the same memory location and (x is y) returns True and anything outside this range returns False. This is created at the time of starting Python Virtual machine itself with 257 values from 0 to 256.
  • For bool data-type also, either it is True or False. So, whenever we define bool variable then same value contents stored in same memory location. Similar to int data-type, this is also created at the time of starting Python Virtual machine itself as there are only 2 values viz.; True(1) & False(0).
  • For other fundamental data-types like float & complex, concept of sharing same memory location for storing same value content is not applicable

7. bytes data-type :

  • This represents a group of byte numbers, just like an array
  • To create bytes in Python, x=[10, 20, 30, 40] and then b=bytes(x)
  • To know the type, we can use Python’s in-built function type(b) which prints <class ‘bytes’>
  • To print bytes values to Python’s terminal, we can use for-loop like for x in b: print(x)
  • It is commonly used to represent binary data like images & videos, etc.

7.1 Important points w.r.t bytes data-type :

  • Values in the bytes data-type should be in the range of 0 to 256 otherwise ValueError is thrown stating “bytes must be in range(0, 256)” as it is frequently used
  • bytes data-type is immutable means values once assigned cannot be in its lifetime of program execution. Trying to change value throws TypeError stating “‘bytes’ object does not support item assignment
  • bytes data-type & bytearray are one and same except bytearray is mutable in nature whose value can changed during program execution

8. list data-type :

  • If we want to represent group of values/objects as a single entity where insertion order is preserved as well as duplicates are allowed then we can use list data-type
  • To represent list in Python, we can use values separated by comma’s within square-brackets like l=[]
  • We can represent heterogeneous objects in list as well like int & float & str objects can be grouped together in list object like l = [10, 20, 30, 10, ‘Bench’]
  • Based on our requirement more objects can be added to list i.e.; growable or objects can also removed from list i.e.; shrinkable
  • We can access/iterate through list using slice-operator or for-loop
  • Slice-operator : l[0] returns 10 and 1[1:5] returns [20, 30, 10, ‘Bench’]
  • Star or Asterisk operator is used to multiply same list object into multiple copies in same list object like s=[10, 20.5, ‘Bench’, True] then s1=s*2 returns [10, 20.5, ‘Bench, True, 10, 20.5, ‘Bench’, True]
  • To know data-type of above list s or s1 then type(s) or type(s1) returns <class ‘list’>
  • list object is mutable

9. tuple data-type :

tuple is very similar to list object like

  • Insertion order is preserved
  • Duplicates are allowed
  • Heterogeneous objects allowed to exists
  • but it is immutable where values assigned cannot be changed
  • For example, t=(10, 20, 30) is an example of tuple to represent in Python where round-brackets are used whereas square-brackets are used to represent list object
  • We can access elements from tuple using t[index]
  • Similarly, slice operator can be applied to retrieve partial elements based on parameters passed like t[0:2] which returns (10, 20)
  • Likewise in list object, Star or Asterisk operator can be applied to get multiple copies in tuple as well

10. range data-type :

  • range data-type represent a sequence of values
  • range is immutable which means range can be used to iterate through items but iterating values can be changed
  • There are multiple forms of range data-type
  • Form-1: range(end) which represent value from 0 to end-1
    • For example, we can iterate through range(10) using for-loop like for i in r: print(i) which prints 0, 1, 2….9
    • We can access particular element using index-position like r[4] and trying to assign new value to this element throws TypeError stating “‘range’ object does not support item assignment
  • Form-2: range(start, end) which represents value from start to end-1
    • For example, we can iterate through range(10, 30) using for-loop like for i in r: print(i) which prints 10, 11, 12….29
  • Form-3: range(start, end, stepIncrement) which represents value from start to end-1 with step increment
    • For example, we can iterate through range(10, 50, 5) using for-loop like for i in r: print(i) which prints 10, 15, 20….45
  • range data-type is applicable to iterate through int values only, trying to iterate through float values (or data-types) throws TypeError stating “‘float’ object cannot be interpreted as an integer

11. set data-type :

  • If we want to represent group of values/objects as a single entity where insertion order is not preserved and duplicates are not allowed then we can use set data-type
  • Adding duplicate values to set object does not throw any error but it will be added to set only once
  • To represent set in Python, we can use values separated by comma’s within curly-brackets like s=set{}
  • We can represent heterogeneous objects in set as well like int & float & str objects can be grouped together in set object like s = {10, 20, 30, ‘Bench’}
  • set object is mutable, based on our requirement more objects can be added to set i.e.; growable or objects can also removed from set i.e.; shrinkable
  • Accessing set object based on index-position is not applicable as insertion order are not preserved
    • For example, trying to access s[0] throws TypeError stating “‘set’ object does not support indexing
  • Slice operator is not applicable
    • For example, s[1:2] trying to do slice with set object throws TypeError stating “‘set’ object is not subscriptable

12. frozenset data-type :

  • frozenset is very similar to set object except that it is immutable
  • Other things are same like insertion order is not preserved and duplicates are not allowed
  • For example, fs=frozenset{10, 20, 30, 40}

13. dict data-type :

  • dict is a short-form for dictionary
  • If we want to represent group of Key-Value pairs as a single entity then we can use dict data-type where duplicates are not allowed for Keys but duplicates are allowed for Values
  • To represent dict in Python, we can use Key-Value pairs separated by comma’s within curly-brackets like d={}
    • For example, d={100:’Delhi’, 200:’Mumbai’, 300:’Chennai’, 400:’Kolkata’}
  • To know data-type of dict ‘d’, we can use type(d) which returns <class ‘dict’>
  • If there is empty curly-braces like d={} then it is considered as dictionary
  • dictionary is mutable as more Key-Value pairs can be added
  • If we add another Key which is already present in the dict then its old Value is replaced with new value and there will be no error thrown

14. None data-type :

  • None means no value associated
  • This is very similar to null or void in Java

Happy Coding !!
Happy Learning !!

Python Identifiers and Reserved keywords