Python Data Types

Python provides different data types in order to store values. Every data type in Python is actually an instance of classes. Python provides the following data types.

  • Numbers
  • Boolean
  • String
  • List
  • Tuple
  • Set
  • Dictionary
  • Binary Types

Python Numbers

Python numbers are consist of two main types named integer, floating-point, and complex numbers.

Integer numbers can store any decimal numbers which can be positive or negative. Integer numbers can be any length which is limited with the available memory.

a = 5

b = 10

c = a + b

Python Integer Data Type

Floating point numbers are useful to store floating-point values.

a = 1.2

b = 2.8

c = a + b

Python Floating-Point Data Type

Complex numbers are mainly used by mathematicians and data professional which are consist of real and imaginary parts.

a = 1+2j

b = 2+1j

Python Boolean

Python Boolean values will store basic logic True or False .Boolean values also used for different boolean operations like AND, OR, XOR, NOT, etc.

a = True

b = False

c = a | b

#Output will be True

c = a & b

#Output will be False
Python Boolean Data Type

Python Strings

Python string data type is a sequence of single or multiple Unicode characters. In order to provide the string value single or, double quotes can be used. For multi-line strings triple quotes should be used.

name ="ismail"

myname = 'ismail'

#Output will beismail

#Output will be ismail

#Output will be s
Python String Data Type

Python List

Python List data type of store a sequence of single or multiple objects or other data types. The list is also called as Array in other programming languages. The list is very flexible where multiple types of data can be stored inside a single list.

mylist = [ 1 , "two" , 3 , "four" , 5.0 ]

#Output will 5 becuase there are 5 elements in this list
Python List Data Type

We can see that a first item is a number where second item is a string and the last item is a floating-point number.

LEARN MORE  Python List pop() Function To Return and Remove Item Tutorial with Examples

Python Tuple

Python Tuple is the very same as the Python List. Except tuple is read-only which means it can not be updated or changed after creation. But on the other side, a list can be updated or changed after creation. In order to update a tuple, a new tuple should be created with the old items.

mytuple = ( 1 , "two" , 3 , "four" , 5.0 )

#Output will be 5
Python List Data Type

Python Set

Python Set data type provides the features of the mathematical sets where every item is unique. Even elements defined multiple times only a single instance will be stored inside the set. Element items do not support indexing which means elements can not be accessed with index numbers.

myset = { 1 , 1, 1,  "two" , 3 , "four" , 5.0 }

#Output will be 5

#The output will be{1, 3, 5.0, 'two', 'four'}
Python Set Data Type

We can see that 1 is defined 3 times in the set named myset but stored only single 1 and the length of the myset is 5 even the definition provides 8 items.

Python Dictionary

Python Dictionary is a data type which will store key-value pairs or items. It is called a dictionary because its structure is similar to a dictionary. Every key is related to value. Dictionary is optimized for retrieving values via using keys.

mydict = { 1:'value' , 'key':2 , 'mykey':'myvalue' , 'name':'ismail'}

#The output will be 4
Python Dictionary Data Type

We can see in the example that we can access the values with their keys. The keys can be an integer, a string where values can be too.

Python Binary Types

Python Byte data type is used to store given data in a byte format. The byte data type is defined with the b before the value like below.

mybyte= b'Hello'
Python Byte Data Type

When we print the bytes one by one we can see that the string characters ASCII values are stores as a byte.

Find Python Data Type

Python programming language data type is specified while setting initial value into a variable. so there is no specific sign about the variable data type. This makes finding a data type a bit complicated but type() function can be used to get the data type of the provided variable or structure.

a = 1

b = 2.0

c = 1 + 2j

myname = "İsmail Baydan"

mylist = [ 1 , "two" , 3 , "four" , 5.0 ]

mytuple = ( 1 , "two" , 3 , "four" , 5.0 )

myset = { 1 , 1, 1, "two" , 3 , "four" , 5.0 }

mydict = { 1:'value' , 'key':2 , 'mykey':'myvalue' , 'name':'ismail'}

mybyte= b'Hello'

#The output will <class 'int'>

#The output will <class 'float'>

#The output will <class 'complex'>

#The output will <class 'str'>

#The output will <class 'list'>

#The output will <class 'tuple'>

#The output will <class 'set'>

#The output will <class 'dict'>

#The output will <class 'bytes'>
Python Type Function

Leave a Comment