The Different Types of Data in Python

In Python, data is everything. It’s what makes up our programs and tells the computer what to do with it. Because of this, it’s important to know the different types of data so that we can use them effectively in our programs. In this blog post, we’ll be discussing the different data types in Python and how to use them.

Python has many different data types that we can use in our programs. The most common data types are strings, integers, floats, booleans, and NoneType. Let’s take a closer look at each of these data types so that we can better understand how to use them.

The Different Types of Data in Python

String Data Type:

Strings are one of the most common data types in Python. A string is simply a sequence of characters, like “Hello world!” or ‘I am a programmer.’ We can create strings by enclosing them in quotation marks (either double or single).

We can also use the print function to print strings to the console.

a_string = 'Hello World' print(a_string) #Output: Hello World print(a_string[0]) #Output: H print(a_string[:]) #Output: Hello World print(a_string[0:5]) #output will be first five characters. Hello
Code language: Python (python)

Set Data Type:

A set is an unordered collection of unique items. There are two types of sets. Sets are useful for storing data that we want to be able to access quickly without having to worry about duplicates. We can create sets by using the set function or by using curly braces {}.

There are two types of sets:

  • Sets – They are mutable and new elements can be added once sets are defined
my_basket = {'Strawberry', 'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} print(my_basket) # duplicates will be removed #Output: {'orange', 'apple', 'pear', 'Strawberry', 'banana'} a = set('babracadabra') print(a) # unique letters in a #Output: {'b', 'c', 'r', 'a', 'd'} a.add('z') print(a) #Output: {'b', 'c', 'r', 'a', 'd', 'z'}
Code language: Python (python)
  • Frozen Sets – They are immutable and new elements cannot be added after its defined.
x = frozenset('jasdfagsae') print(x) #Output: frozenset({'j', 'e', 'f', 'g', 's', 'a', 'd'}) my_cities = frozenset(["Frankfurt", "Basel","Freiburg"]) print(my_cities) #Output: frozenset({'Frankfurt', 'Basel', 'Freiburg'})
Code language: Python (python)

Numbers Data Type:

Numbers are another common data type in Python. There are two main types of numbers: integers and floats. Integers are whole numbers like 1, 2, 3, 4, etc., while floats are decimal numbers like 1.0, 2.5, 3.14159, etc. We can create numbers by simply writing out the number (e.g., 42) or by using the int or float functions (e.g., int(“42”) or float(“42”)).

int_num = 10 #int value float_num = 10.2 #float value complex_num = 3.14j #complex value long_num = 1234567L #long value
Code language: Python (python)

List Data Type:

Lists are ordered collections of items that can be of any type (including other lists). We can create lists by using square brackets [] and separating each item with a comma, For example, [1, 2, 3] is a list with three items: 1, 2, and 3. Lists are very versatile and can be used for many different things such as storing user input or creating menus.

mylist = [1233,'abcdf',10.2,'d'] #can be an array of any data type or single data type. mylist1 = ['hello','world!'] print(list) #will output whole list. [1233,'abcdf', 10.2,'d'] print(list[0:2]) #will output first two element of list. [1233,'abcdf'] print(list1 * 2) #will gave list1 two times. ['hello','world','hello','world'] print(list + list1) #will gave concatenation of both the lists. [1233,'abcdf',10.2,'d','hello','world']
Code language: Python (python)

Dictionary Data Type:

Dictionaries are unordered collections of key-value pairs where each key is unique. We can create dictionaries by using curly braces {} and separating each key-value pair with a comma , . For example, {“name”: “Bob”, “age”: 42} is a dictionary with two key-value pairs: name -> Bob and age -> 42 . Dictionaries are often used for storing user information or configuration settings.

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

Tuple Data Type:

Tuples are ordered collections of items that cannot be changed (i.e., they are immutable). We can create tuples by using parentheses () and separating each item with a comma, For example, (1, 2, 3) is a tuple with three items: 1, 2, and 3. Tuples are often used for storing data that should not be changed such as coordinates or a list of words.

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


In conclusion, there are many different data types in Python that we can use to store information in our programs. Depending on what type of data we’re working with, we’ll want to use a different data type so that our program will run effectively. Thanks for reading!

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts