Learn Data Types in Python

Learn Data Types in Python

Published at - Jan 03, 2022

Python is a welcome change from JavaScript because it has a huge standard library and is simple to learn (especially if you have prior programming knowledge). Starting with data types, I’d want to highlight some of Python’s wonderful features as well as some noteworthy contrasts I discovered. So don’t anticipate a comprehensive guide or comparison of the two.

Start the Python REPL if you wish to test out the steps below. Run python in your terminal if you have Python installed. Any Python code that begins with >>> denotes that it was typed into a REPL.

Helper Functions

Python includes several functions that can assist you in defining and discovering types. There are three of them: type (), dir (), and help ().

>>> name = "Lennart"
>>> type(name)
<class 'str'>
>>> dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> help(str.title)
Help on method_descriptor:

title(self, /)
Return a version of the string where each word is titlecased.

More specifically, words start with uppercased characters and all remaining
cased characters have lower case.


For working with numbers, Python provides more explicit types. They are mashed together into numbers in JavaScript.

>>> x = 4
>>> y = 5.0
>>> z = 42j
>>> type(x)
<class 'int'>
>>> type(y)
<class 'float'>
>>> type(z)
<class 'complex'>


Tuples are immutable collections that may be used to track objects that are related but not identical. As a result, they’re excellent as immutable keys in set and dict. Unpacking is a technique for quickly extracting data from a tuple:

>>> favourite_food = ("Italian", "Pizza")
>>> cuisine, name = favourite_food
>>> cuisine
>>> name


A set is changeable and unsorted, and it stores immutable types. Other mutable types (such as lists, sets, or dicts) cannot be stored in it. Only unique things can be included in a set.

This is great for quickly deduplicating a list:

>>> names = ["Luke", "Leia", "Malak", "Luke"]
>>> set(names)
{'Leia', 'Luke', 'Malak'}

You can execute a couple of mutations on a set (.add or .discard etc.). But I definitely found it. This update is interesting since you can add multiple values at once.

>>> chars = {"James", "Naomi", "Amos"}
>>> addition = {"Alex", "Julie"}
>>> chars.update(addition)
>>> chars
{'Julie', 'James', 'Naomi', 'Alex', 'Amos'}

The set operations — union and intersection — are also interesting:

>>> chars = {"James", "Naomi", "Amos"}
>>> favourite_chars = {"James"}
>>> chars | addition
{'Julie', 'James', 'Naomi', 'Alex', 'Amos'}
>>> chars & favourite_chars


A dictionary is a changeable data structure that holds key-value pairs (keys can only be immutable types). As a result, looking for something is really quick. Aside from the usual stuff, there are three useful methods: keys, values, items, and so on.

>>> chars = { "expanse": "Holden", "star_wars": "Luke" }
>>> chars.keys()
dict_keys(['expanse', 'star_wars'])
>>> chars.values()
dict_values(['Holden', 'Luke'])
>>> chars.items()
dict_items([('expanse', 'Holden'), ('star_wars', 'Luke')])

While these functions aren’t particularly useful on their own, they may be extremely useful in for-loops.

>>> for franchise, char in chars.items():
... print(f"The char {char} exists in the franchise {franchise}")
The char Holden exists in the franchise expanse
The char Luke exists in the franchise star_wars

Because items() produce a list of tuples, tuple unpacking may be used to obtain both values.

Thank you for reading this article, don’t forget to follow me to read more articles like this. You can also share this story with your friends if you find it helpful for others.

By evolving a Medium partner, you can support me and your other favored writers! 👇

Join Medium with my referral link - Harendra Verma

More content at plainenglish.io. Sign up for our free weekly newsletter. Get exclusive access to writing opportunities and advice in our community Discord.

Learn Data Types in Python was originally published in Python in Plain English on Medium, where people are continuing the conversation by highlighting and responding to this story.

Follow Us

Follow us on facebook Click Here

Facebook QR
Scan from mobile
Join our telegram channel Click Here
Telegram QR
Scan from mobile