You are on page 1of 7

Data Types

Every value in python has a data type. Everything is a object in


python programming. Datatypes are actually classes and variables
are objects of these classes. There are various datatypes in python.
Some of the important types are listed below.
i. Numbers.
ii. Strings.
iii. List.
iv. Tuple.
v. Dictionary.

Python Numbers :
Integers, Floating point numbers, Complex
numbers and Boolean belong to Number category. They are defined
as int, float, complex and bool class in Python. We can use the type()
function to know which class a variable or a value belongs to and the
isinstance() function to check if an object belongs to a particular
class.

Int : Integers can be of any length, it is only limited by the memory


available. Integers are numbers without decimal values.
Ex :

Float : A floating point number is accurate up to 15 decimal places.


Integer and floating points are separated by decimal points. 1 is
integer but 1.0 is floating point number.
Ex :

Complex : Complex numbers are written in the form, a + bj, where


a is the real part and b is the imaginary part. Here are some
examples.
Ex :

Boolean : The bool() method converts a value to Boolean (True or


False) using the standard truth testing procedure.
Bool() returns :
- False if the value is omitted or false
- True if the value is true

The following values are considered false in Python:


 None

 False

 Zero of any Numeric type(int, float, complex etc…). For example,  0 ,  0.0 ,  0j
 Empty Sequence(list, tuple ,string etc…). For example,  () ,  [] ,  '' .
 Empty Dictionary. For example,  {}
 objects of Classes which has  __bool__()  or  __len()__  method which
returns  0  or  False
All other values except these values are considered true.

Ex :

Python Strings :
String is sequence of characters. strings, which
can be expressed in several ways. They can be enclosed in single
quotes ('...') or double quotes ("...") with the same result. Strings are
immutable, so we cannot modify string values. Characters are stored
into a string by using Indexing. Like list and tuple, slicing operator []
can be used with string.
The string data type has methods, some of the methods of its
objects:
string.find(str) : Return the lowest index of string character
string.index(Str) : returns the index of string character
string.count(str) : Return the number of occurrences of substring
string.upper(s) : Return a copy of s, but with lower case letters
converted to upper case.
string.lower(s) : Return a copy of s, but with upper case letters
converted to lower case.
Ex : Program Output

Python List :
List is an ordered sequence of items, which can be
written as comma-separated values (items) between square
brackets. Lists might contain items of different types, but usually the
items all have the same type.
Like strings (and all other built-in sequence type), Lists can be
indexed and sliced :

The list data type has methods, some of the methods of list objects:
list.append(x) : Adds an item to the end of the list. .
list.insert(i, x) : Inserts an item at a given position.
list.remove(x) : Removes the first item from the list whose
value is x. It is an error if there is no such item.
list.clear() : Remove all items from the list.
list.count(x) : Return the number of times x appears in the list.
list.sort() : Sort the items of the list in place.
list.reverse() : Reverse the elements of the list in place.

Ex : Program Output

Python Tuple:
Tuples are immutable sequences, Tuple is made up
of data items with comma separated values enclosed within
parentheses. Tuple is typically used to store collections of
heterogeneous data or homogeneous data items. Like strings (and all
other built-in sequence type), tuple can be indexed and sliced.

Various methods in tuple are :


len(tuple) : Gives the total length of the tuple.
max(tuple) : Returns item from the tuple with max value.
min(tuple) : Returns item from the tuple with min value.
tuple(seq) : Converts a list into tuple.
Ex :

Program :

Output :
Dictionaries :
an unordered set of key: value pairs, with the
requirement that the keys are unique (within one dictionary).
A pair of braces creates an empty dictionary: {}. Placing a
comma-separated list of key:value pairs within the braces
adds initial key:value pairs to the dictionary.
this is also the way dictionaries are written on output.
Ex :
Program :
Output :

You might also like