Data types are variables that are used to store values. In Python, variables do not need a pre declaration; the declaration of variables in Python happens automatically when we assign any value to a variable.

Just like variables, data types in Python also do not need any declaration. They will be declared automatically whenever we will assign any value or values to any data type. Let’s go through all the data types in Python.

String Data Types

String are a contiguous group of characters represented within quotation marks. They are immutable data types, so whenever you make any changes to a string, a new string object will be created. Let’s see how we can work with a String:

a_str = 'Hello World'
print(a_str) #output will be whole string. Hello World
print(a_str[0]) #output will be first character. H
print(a_str[0:5]) 

Hello World

H

Hello

Set Data Type

Sets are unordered collections of distinct objects. They are mutable, and new elements can be added in the set, which is already defined. Sets are created within curly brackets are each element in the set is separated by commas. Let’s see how to work with sets:

basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(basket) 

{‘orange’, ‘banana’, ‘apple’, ‘pear’}

a = set('abracadabra')
print(a) 

{‘d’, ‘b’, ‘a’, ‘c’, ‘r’}

a.add('z')
print(a)

{‘d’, ‘b’, ‘a’, ‘c’, ‘r’, ‘z’}

#by aman kharwal #python

Data Types in Python | Data Science | Machine Learning | Python
1.10 GEEK