Python Data Types
January 13th 2020 523

Built-in Data Types
The data stored in memory can be of many types. For example, a person's age is stored as a numeric value and his or her address is stored as alphanumeric characters. Python has various built-in data types that are used to define the operations possible on them and the storage method for each of them.
- In programming, data type is an important concept.
- Variables can store data of different types, and different types can do different things.
Python has the following built-in data types :
Text Type: | str |
Numeric Types: | int , float , complex |
Sequence Types: | list , tuple , range |
Mapping Type: | dict |
Set Types: | set , frozenset |
Boolean Type: | bool |
Binary Types: | bytes , bytearray , memoryview |
Mutable and Immutable Objects
Not all python objects handle changes the same way. Some objects are mutable, meaning they can be altered. Others are immutable; they cannot be changed but rather return new objects when attempting to update. A mutable object can change its state or contents and immutable objects cannot.
Mutable objects:
- list
- dict
- set
- bytearray
- user-defined classes (unless specifically made immutable)
Immutable objects:
- int
- float
- complex
- bool
- string
- tuple
- range
- frozenset (immutable version of set)
- bytes
int (signed integers)
They are often called just integers or ints. They are positive or negative whole numbers with no decimal point. Integers in Python 3 are of unlimited size. Python 2 has two integer types - int and long. There is no 'long integer' in Python 3 anymore.
float (floating point real values)
Also called floats, they represent real numbers and are written with a decimal point dividing the integer and the fractional parts. Floats may also be in scientific notation, with E or e indicating the power of 10 (2.5e2 = 2.5 x 102 = 250).
complex (complex numbers)
Are of the form a + bJ, where a and b are floats and J (or j) represents the square root of -1 (which is an imaginary number). The real part of the number is a, and the imaginary part is b. Complex numbers are not used much in Python programming.
A complex number consists of an ordered pair of real floating-point numbers denoted by a + bj, where a is the real part and b is the imaginary part of the complex number.
Data type conversion
There are several built-in functions to perform conversion from one data type to another. These functions return a new object representing the converted value.
abs()
: Returns absolute value of a numberascii()
: Returns String Containing Printable Representationbin()
: Converts integer to binary stringbool()
: Coverts a Value to Booleanbytes()
: Returns immutable bytes objectchr()
: Returns a Character (a string) from an Integercomplex(real [,imag])
: Creates a complex numberdict(d)
: Creates a dictionary. d must be a sequence of (key,value) tuplesfloat(x)
: Converts x to a floating-point numberhash()
: Returns hash value of an objecthex()
: Converts to Integer to Hexadecimalint(x [,base])
: Converts x to an integer. The base specifies the base if x is a stringlist(s)
: Converts s to a listoct()
: Converts integer to octalrepr(x)
: Converts object x to an expression stringround()
: Rounds a floating point number to n digits places.set(s)
: Converts s to a setstr(x)
: Converts object x to a string representationtuple(s)
: Converts s to a tuple
Python Built-in Functions
all()
: Returns true when all elements in iterable is trueany()
: Checks if any Element of an Iterable is Truebytearray()
: Returns array of given byte sizecallable()
: Checks if the Object is Callableclassmethod()
: Returns class method for given functioncompile()
: Returns a Python code objectdelattr()
: Deletes Attribute From the Objectdir()
: Tries to Return Attributes of Objectdivmod()
: Returns a Tuple of Quotient and Remainderenumerate()
: Returns an Enumerate Objecteval()
: Runs Python Code Within Programexec()
: Executes Dynamically Created Programfilter()
: Constructs iterator from elements which are trueformat()
: Returns formatted representation of a valuefrozenset()
: Returns immutable frozenset objectgetattr()
: Returns value of named attribute of an objectglobals()
: Returns dictionary of current global symbol tablehasattr()
: Returns whether object has named attributehelp()
: Invokes the built-in Help Systemid()
: Returns Identify of an Objectinput()
: Reads and returns a line of stringisinstance()
: Checks if a Object is an Instance of Classissubclass()
: Checks if a Object is Subclass of a Classiter()
: Returns iterator for an objectlen()
: Returns Length of an Objectlocals()
: Returns dictionary of current local symbol tablemap()
: Applies Function and Returns a Listmax()
: Returns largest elementmemoryview()
: Returns memory view of an argumentmin()
: Returns smallest elementnext()
: Retrieves Next Element from Iteratorobject()
: Creates a Featureless Objectopen()
: Returns a File objectord()
: Returns Unicode code point for Unicode characterpow()
: Returns x to the power of yprint()
: Prints the Given Objectproperty()
: Returns a property attributerange()
: Return sequence of integers between start and stopreversed()
: Returns reversed iterator of a sequencesetattr()
: Sets value of an attribute of objectslice()
: Creates a slice object specified by range()sorted()
: Returns sorted list from a given iterablestaticmethod()
: Creates static method from a functionsum()
: Add items of an Iterablesuper()
: Allow you to Refer Parent Class by supertype()
: Returns Type of an Objectvars()
: Returns dict attribute of a classzip()
: Returns an Iterator of Tuples
Let's checkout the example:
We will see a program to convert a given number into its equivalent binary number in python.