Python Data Types
January 13th 2020 278
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 :
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.
- user-defined classes (unless specifically made immutable)
- frozenset (immutable version of set)
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 number
ascii(): Returns String Containing Printable Representation
bin(): Converts integer to binary string
bool(): Coverts a Value to Boolean
bytes(): Returns immutable bytes object
chr(): Returns a Character (a string) from an Integer
complex(real [,imag]): Creates a complex number
dict(d): Creates a dictionary. d must be a sequence of (key,value) tuples
float(x): Converts x to a floating-point number
hash(): Returns hash value of an object
hex(): Converts to Integer to Hexadecimal
int(x [,base]): Converts x to an integer. The base specifies the base if x is a string
list(s): Converts s to a list
oct(): Converts integer to octal
repr(x): Converts object x to an expression string
round(): Rounds a floating point number to n digits places.
set(s): Converts s to a set
str(x): Converts object x to a string representation
tuple(s): Converts s to a tuple
Python Built-in Functions
all(): Returns true when all elements in iterable is true
any(): Checks if any Element of an Iterable is True
bytearray(): Returns array of given byte size
callable(): Checks if the Object is Callable
classmethod(): Returns class method for given function
compile(): Returns a Python code object
delattr(): Deletes Attribute From the Object
dir(): Tries to Return Attributes of Object
divmod(): Returns a Tuple of Quotient and Remainder
enumerate(): Returns an Enumerate Object
eval(): Runs Python Code Within Program
exec(): Executes Dynamically Created Program
filter(): Constructs iterator from elements which are true
format(): Returns formatted representation of a value
frozenset(): Returns immutable frozenset object
getattr(): Returns value of named attribute of an object
globals(): Returns dictionary of current global symbol table
hasattr(): Returns whether object has named attribute
help(): Invokes the built-in Help System
id(): Returns Identify of an Object
input(): Reads and returns a line of string
isinstance(): Checks if a Object is an Instance of Class
issubclass(): Checks if a Object is Subclass of a Class
iter(): Returns iterator for an object
len(): Returns Length of an Object
locals(): Returns dictionary of current local symbol table
map(): Applies Function and Returns a List
max(): Returns largest element
memoryview(): Returns memory view of an argument
min(): Returns smallest element
next(): Retrieves Next Element from Iterator
object(): Creates a Featureless Object
open(): Returns a File object
ord(): Returns Unicode code point for Unicode character
pow(): Returns x to the power of y
print(): Prints the Given Object
property(): Returns a property attribute
range(): Return sequence of integers between start and stop
reversed(): Returns reversed iterator of a sequence
setattr(): Sets value of an attribute of object
slice(): Creates a slice object specified by range()
sorted(): Returns sorted list from a given iterable
staticmethod(): Creates static method from a function
sum(): Add items of an Iterable
super(): Allow you to Refer Parent Class by super
type(): Returns Type of an Object
vars(): Returns dict attribute of a class
zip(): 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.