Skip to main content

Python Datatypes

Every variable are need some values. and every value are has an type this is called data type. python are provide various list of native data type. This every data are certain rules to access and defined. In this post we are learning about that of basic information of those data type.

Number

Number is awesome to perform of mathematical and relational operation. python are support both types of integers and floating points numbers.

print(type(10)) #int data type
print(type(10.10)) #float data type
Output
<class 'int'>
<class 'float'>

We can check the size of an object(variables) in following way.

import sys #include module
data=10 
print(sys.getsizeof(data)) 
data=10.10
print(sys.getsizeof(data))
Output
28
24

Python LISTS

list is collection of data values. that collection values are access by particular index. list are capable to store similar and multiple data. and inside a list is are possible to create new another list. for example.

#python list example

#Case 1 : Simple and similar data list
list1=[1,2,3,4,5,6] 

#Case 2: Simple and multiple data list
list2=["C","Cpp","Python",100,True,786.786] 

#Case : Multiple and multiple data list
list3=["multilist",[1,2,3,4],[5,6,"Seven",8.10]] 

#display element of list
print(list1)
print(list2)
print(list3)
h5>Output
[1, 2, 3, 4, 5, 6]
['C', 'Cpp', 'Python', 100, True, 786.786]
['multilist', [1, 2, 3, 4], [5, 6, 'Seven', 8.1]]
Python list Examples

List element index are start in zero. that is similar index concept of c and cpp array. so we can access this element by its index. for example.

#python list example

#Case 1 : Simple and similar data list
list1=[1,2,3,4,5,6] 

#Case 2: Simple and multiple data list
list2=["C","Cpp","Python",100,True,786.786] 

#Case : Multiple and multiple data list
list3=["multilist",[1,2,3,4],[5,6,"Seven",8.10]] 

#display index value
print(list1[3]) #4
print(list2[2])#python
print(list3[1][2]) #print 3
Output
4
Python
3

Learn more about the list concept in Python List post.

Python Tuple

Tuple is a container which are store single and group of elements. This element are exists and defined within parentheses and separated of (,)comma. for example.

#python Tuples example

#Case 1: simple tuples
tuple1=(1,2,3,4,5)

#Case 2: simple and multiple data
tuple2=(True,False,'Status',123,10.3)

#Case 3: Multiple tuples and multiple data
tuple3=((4,3,2,1),('A','B','C'),(1,2,3),(1,(9,8,7.7,6.6)))

#Display elements
print(tuple1)
print(tuple2)
print(tuple3)
Output
(1, 2, 3, 4, 5)
(True, False, 'Status', 123, 10.3)
((4, 3, 2, 1), ('A', 'B', 'C'), (1, 2, 3), (1, (9, 8, 7.7, 6.6)))
Python Tuple Examples

Tuples are similar to list there is index are start with zero. and similar way to access tuple element but only difference of tuple and list. list data element can be modified but tuple element are cannot be modfied. for example

listData=[1,2,3]
print("Before List",listData)
#modified index 1 element
listData[1]=30
print("After List",listData)

tupleData=(1,2,3)
print("Before tuple",tupleData)
tupleData[1]=30
print("After tuple",tupleData)
Output
Before List [1, 2, 3]
After List [1, 30, 3]
Before tuple (1, 2, 3)
Traceback (most recent call last):
  File "test.py", line 9, in <module>
    tupleData[1]=30
TypeError: 'tuple' object does not support item assignment

Because tuple are immutable. if are try to add and modified existing element that first we need to convert tuple to list and update its element. see this example.

#python Tuples example

tuple1=(1,2,3,4,5)
print(tuple1)
tuple1=list(tuple1)

#modified data
tuple1[2]=30
tuple1[3]=40

tuple1=tuple(tuple1)
print(tuple1)
Output
(1, 2, 3, 4, 5)
(1, 2, 30, 40, 5)

That is basic introductory part of tuple. learn more about Python Tuples in tuple post.

Python Sets

Set is collection of unique and unordered elements. That are capable to store any kind of value but there is not possible to use one set within of another set. that are contain a value of immutable type. sets are useful to perform standard operating within the two sets like union, set difference, interaction etc.

Let view an example how to create sets in python.

#set of unique element
set1={1,2,3,4}

#set of repeated element
set2={1,1,1,2,5,5,6} #{1,2,5,6}

print(set1)
print(set2)
Output
{1, 2, 3, 4}
{1, 2, 5, 6}
Python Sets Examples

Learn more about the Python Sets in set post

Python Dictionary

Dictionary is combination of key-value pairs. whenever insert a element of Dictionary also insert a value. order of this element are unordered list. but very useful to access and also can be modify its value by key.

let take one example how to create dictionary.

#simple dictionary
dictionary1={'language':'python','me':"Programmer"}

#dictionary with multiple data element
dictionary2={'lists':[1,2,3],
  "tuples":('a','b','c'),
  'sets':{1,1,2},
  'multi':{'check':1,'boolean':{True,False}}}

print(dictionary1)
print(dictionary2)  
Output
{'me': 'Programmer', 'language': 'python'}
{'multi': {'boolean': set([False, True]), 'check': 1}, 'tuples': ('a', 'b', 'c'), 'lists': [1, 2, 3], 'sets': set([1, 2])}
Python dictionary Example

Learn more about the Python dictionary in dictionary post





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment