Sitemap

Python Datatypes: Understanding the Different Types and their Examples

3 min readMar 29, 2023

--

Python has several built-in datatypes including numeric, sequence, boolean, set, and dictionary. Numeric datatype includes integers, floats, and complex numbers. The sequence datatype consists of strings, lists, and tuples. Booleans have two values: True and False. Sets are unordered, iterable, and mutable collections of datatypes that do not allow duplicate elements. Dictionaries are unordered collections of key-value pairs. Understanding the different datatypes in Python is essential for creating efficient and effective programs. Each datatype has unique characteristics and functions, making it useful for different purposes, improving program performance, and code readability.

Press enter or click to view image in full size

Python is a dynamically typed language, which means that the datatype of a variable is not determined until runtime. In Python, datatypes are classes, and variables are instances of these classes or objects. There are several built-in datatypes in Python, including numeric, sequence, boolean, set, and dictionary. Each datatype has its own unique characteristics and functions, making it useful for different purposes.

Numeric Datatype The numeric datatype in Python includes integers, floats, and complex numbers. Integers in Python have no limit and can be as large as the available memory allows. Floats are used for representing decimal values, and complex numbers are a combination of a real and imaginary part.

Example:
a = 10
b = 2.5
c = 3 + 4j

Sequence Datatype The sequence datatype in Python is an ordered collection of similar or different datatypes. There are three types of sequence datatypes in Python: strings, lists, and tuples. Strings are arrays of bytes representing Unicode characters and are immutable. Lists are ordered collections of datatypes that are mutable, meaning that their elements can be changed after creation. Tuples are similar to lists but are immutable, and if a tuple has only one element, it must be followed by a comma.

Get Chinthapooja’s stories in your inbox

Join Medium for free to get updates from this writer.

Example: string = “Hello, World!” list = [1, 2, 3, “apple”, “banana”] tuple = (4, 5, 6, “orange”)

Boolean Datatype The boolean datatype in Python consists of two values: True and False. These values are used to represent the truth value of an expression or statement.

Example: x = 5 y = 10 z = x > y print(z) # Output: False

Set Datatype The set datatype in Python is an unordered collection of datatypes that are iterable and mutable. Sets do not allow duplicate elements and are useful for performing mathematical operations like union, intersection, and difference.

Example: set1 = {1, 2, 3, 4, 5} set2 = {4, 5, 6, 7, 8} set3 = set1.union(set2) print(set3) # Output: {1, 2, 3, 4, 5, 6, 7, 8}

Dictionary Datatype The dictionary datatype in Python is an unordered collection of key-value pairs. Keys in a dictionary must be unique and are case-sensitive. Dictionaries are useful for storing and accessing data in a way that is fast and efficient.

Example: dict = {‘Name’: ‘John’, ‘Age’: 25, ‘Location’: ‘New York’} print(dict[‘Name’]) # Output: John print(dict.get(‘Age’)) # Output: 25

In conclusion, understanding the different datatypes in Python is important for creating efficient and effective programs. Each datatype has its own unique characteristics and functions, making it useful for different purposes. By using the appropriate datatype for your data, you can improve the performance of your programs and make your code more readable and maintainable.

--

--