Python Programming — Data Types

Nabil Makarim
4 min readDec 24, 2021

Python has a data type that is quite unique when we compare it with other programming.

A data type or simply type is a classification of data which tells the compiler or interpreter how the programmer intends to use the data.

Here are the data types from the Python programming :

String Data Type

String data type is a data type used to store values in the form of strings or characters or letters. String data type must be flanked by quotation marks, both quotation marks one (‘…’) or quotation marks two (“…”) or quotation marks three (“‘…’’’).

Example :

print('Learning Python Is So Easy')
print("I Love Python")
print("2 + 2")
print('2'+'2')

Output :

Learning Python Is So Easy
I Love Python
2 + 2
22

Integer Data Type

Integer data type is a data type that has an round numbers. So that every variable that has a positive or negative round number value, it will be categorized as an integer.

Example :

print(100)
print(7 - 2)
print(4 + 11)
print(5 * 10)
print(20 / 10)

Output :

100
5
15
50
2

Float Data Type

Float data type is a data type that has fractional or decimal values. At decimal number values separated using dots (.) .

Example :

print(3.14)
print(5.5 + 15.5)
print(20.5 - 5.6)
print(20.5 * 5)
print(20.5 / 5)

Output :

3.14
21.0
14.9
102.5
4.1

Boolean Data Type

Boolean data type is a data type that has only two values, True and False. True values for statements are true, and false for representing statements that are false.

Example :

print(1 < 2)
print(2 ==(9 - 7))
print(5 > 20)

Output :

True
True
False

List Data Type

List data type is a collection data type that is ordered and also changable. This data type we can define with square brackets [ ] in Python.

Example :

myList = [1, 20, 'ab', -32, 789, 9.5]#print all items
print(myList)
#print the first item
print(myList[0])
#print the last item
print(myList[-1])
# print from the index 2-4
print(myList[2:5])
#print from the index 0-3
print(myList[:4])
#add items by inserting a specific index
myList.insert(3,'cd')
print(myList)
#add an item at the end of a list
myList.append('i like python')
print(myList)
#delete items by knowing a specific index
del myList[2:4]
print(myList)
#delete items by knowing specific value data
myList.remove('i like python')
print(myList)
#sort items on list data types
myList.sort()
print(myList)

Output :

[1, 20, 'ab', -32, 789, 9.5]
1
9.5
['ab', -32, 789]
[1, 20, 'ab', -32]
[1, 20, 'ab', 'cd', -32, 789, 9.5]
[1, 20, 'ab', 'cd', -32, 789, 9.5, 'i like python']
[1, 20, -32, 789, 9.5, 'i like python']
[1, 20, -32, 789, 9.5]
[-32, 1, 9.5, 20, 789]

Tuple Data Type

Tuple data type is a data type that serves to store more than one value in one variable at once.

Tuple data types are ordered and also unchangable. Ordered means that the data can be accessed using the index, and unchangeable means the data can never be changed after first defining it. In python, tuple data types are defined by parentheses ( ).

Example :

myTuple =(5,10,15,5,25,50,35,45,30,20,5,40)
#print all items
print(myTuple)
#print the first item
print(myTuple[0])
#print the last item
print(myTuple[-1])
#print from the index 5-7
print(myTuple[5:8])
#print from the index 2-12
print(myTuple[2:])
#Calculate the number of specific items
print(myTuple.count(5))
#search for index on item
print(myTuple.index(50))
#Count all items on the tuple
print(len(myTuple))
#searching for maximum value
print(max(myTuple))
#searching for a minimum value
print(min(myTuple))
#calculate the sum of all items
print(sum(myTuple))

Output :

(5, 10, 15, 5, 25, 50, 35, 45, 30, 20, 5, 40)
5
40
(50, 35, 45)
(15, 5, 25, 50, 35, 45, 30, 20, 5, 40)
3
5
12
50
5
285

Set Data Type

Set data type is one of the data types that are not sequential (unordered). The data set type has unique data or values, no duplication. If you enter the same data or value in the set, it will automatically eliminate one of them.

The data set type is defined using curly brackets { }. We can also create a set from a list by entering a list with the function set ( ).

Example :

#set integer
mySet1 = {1,2,3}
print(mySet1)
#mixed data set
mySet = {5, 5.1,'python', (3,4,6)}
print(mySet)
#if we fill in duplication, the set will remove one
mySet = {10,25,25,25,30,30,30,30}
print(mySet)
#Count all items
print(len(mySet))
#searching for maximum value
print(max(mySet))
#searching for a minimum value
print(min(mySet))
#calculate the sum of all items
print(sum(mySet))
#combine the values of two data set types
print(mySet.union(mySet1))
#delete all items
print((mySet.clear))

Output :

{1, 2, 3}
{(3, 4, 6), 5.1, 'python', 5}
{25, 10, 30}
3
30
10
65{1, 2, 3, 25, 10, 30}
{}

Dictionary Data Type

Dictionary data type is a data type in which each member always consists of a pair (key-value).

Dictionary data type is defined by using parentheses, and the data in the dictionary between keywords and values ​​is separated by a colon ( : ), and to separate between keyword members and values ​​are separated by commas (,).

Example :

a = {1:'one', 2:'two', 'three':3, 'name':'Nabil Makarim'} print(a[1]) 
print(a['three'])
print(a['name'])

Output :

one
3
Nabil Makarim

--

--