Data Types in Python

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])
Code language: Python (python)

Hello World

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)
Code language: Python (python)

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

a = set('abracadabra') print(a)
Code language: Python (python)

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

a.add('z') print(a)
Code language: Python (python)

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

List Data Types

A list is a collection of items that are separated by commas. A list is created within square brackets. The lists in Python are very similar to arrays in other programming languages. The difference between a list and array is that a list can store items of different data type, whereas an array cannot. Let’s see how we can work with lists:

list = [123,'abcd',10.2,'d'] #can be an array of any data type or single data type. list1 = ['hello','world'] print(list) #will output whole list. [123,'abcd',10.2,'d'] print(list[0:2]) #will output first two element of list. [123,'abcd'] print(list1 * 2) #will gave list1 two times. ['hello','world','hello','world'] print(list + list1)
Code language: Python (python)

[123, ‘abcd’, 10.2, ‘d’]
[123, ‘abcd’]
[‘hello’, ‘world’, ‘hello’, ‘world’]
[123, ‘abcd’, 10.2, ‘d’, ‘hello’, ‘world’]

Dictionary Data Type

Dictionary is a collection of key and value pairs. Commas separate each pair, and a dictionary is enclosed within curly brackets. The values in the dictionary can we assigned and accessed by using square brackets. Let’s see how we can work with a Dictionary:

dic={'name':'red','age':10} print(dic) #will output all the key-value pairs. {'name':'red','age':10} print(dic['name']) #will output only value with 'name' key. 'red' print(dic.values()) #will output list of values in dic. ['red',10] print(dic.keys())
Code language: Python (python)

{‘name’: ‘red’, ‘age’: 10}
dict_values([‘red’, 10])
dict_keys([‘name’, ‘age’])

Tuple Data Types

Tuples are the same as lists, the difference between them are that Lists are enclosed in square brackets [ ], and the elements and size of a list can be modified, but tuples are enclosed in parentheses ( ) and cannot be modified. They are immutable. Let’s see how we can work with a Tuple:

tuple = (123,'hello') tuple1 = ('world') print(tuple) #will output whole tuple. (123,'hello') print(tuple[0]) #will output first value. (123) tuple[1]='update' #this will give error
Code language: Python (python)

(123, ‘hello’)

Also, Read: Anomaly Detection with Machine Learning.

I hope you liked this article on data types in Python. Feel free to ask your questions in the comment section below. You can also follow me on Medium if you want to learn Machine Learning for free.

Follow Us:

Default image
Aman Kharwal

I am a programmer from India, and I am here to guide you with Data Science, Machine Learning, Python, and C++ for free. I hope you will learn a lot in your journey towards Coding, Machine Learning and Artificial Intelligence with me.

Leave a Reply

Do NOT follow this link or you will be banned from the site!