Understanding Python Data Types:  A Comprehensive Overview

Understanding Python Data Types: A Comprehensive Overview

Demystifying data types in Python

Learning Data Types in any programming is essential for anyone to gain mastery. The data type defines how the interpreter treats values in a variable. In this article, we'll learn about data types and their examples in Python programming language.

In the previous article titled: Comments in Python: The Ultimate Guide to Writing Clean and Maintainable Code, we did a comprehensive guide on Python Comments.

Data Types

In simple terms, the kinds of data that are stored in a variable. Different programming languages and fields of study have different types of data (data types) which gives a guide on the nature of value to be used and the operations to be performed with or on it.

Data Types in Python

These are the types of data that value(s) can be identified with. Python data types guide the developer on how to define and use values within the context of how the interpreter will understand the values based on these built-in data types. The value type in Python is implicitly bound by the interpreter.

List of Data types in Python

  • Sequence of Characters or Text Data Type

This data type often referred to as string is used to define the sequence of letters which is enclosed with the double quote "" or single quote ''.

name = "Alemsbaja"

location = 'Earth'

print(name, location)
  • Output

The slice function can be applied to strings because it can be interpreted as a list in Python.

name = "Alemsbaja"
print(name[0:1])

#A
  • Multi-line strings

The triple quotation can be used to handle multi-line string in Python

description = """ Learning Python made easy
This is a project work.
Learning never ends
"""
print(description)

  • Numeric Data Types

This data type is used to identify numeric values like 0.5, 1, and 10e.

Integer(int): is a whole number (positive or negative). It is a signed integer of unlimited length. The type method in Python is used to get the data type of a value.

age = 10
print("the data type of the value stored in age is", type(age))
# interpreted as integer

Float: a number with one or more decimal points. They're floating precision numbers and it’s accurate up to 15 decimal places.

cgpa = 4.80
print("the data type of the value stored in cgpa is", type(cgpa))

complex: define to hold complex numbers. The genuine (x) and the non-existent parts (imaginary part)(y) an intricate numbers arranged in pairs. The complex numbers like 2.14j, 2.0 + 2.3j, etc.

money = 2+3j  
print("The type of money", type(money))  
print("money is a complex number", isinstance(2+3j,complex))

  • Sequence Types

Sequence Data Type is the collection of items with an index position starting from zero.

List: is a data type that can hold different values with their corresponding index numbers starting from 0 for easy retrieval. The slice operator [:] can be used to access the values of a list.

student_details = [12, "Grade IV", "Norway", 4.0]
print(student_details)
print(student_details[1])
#len method is used to get the number of items in the list
print(len(student_details))

Tuple: a collection of data that cannot be modified. It is immutable (write-protected) i.e. read-only. The collection in a tuple is enclosed in brackets () separated with commas. Tuple allows duplicate of values.

student_details = (12, "Grade IV", "Norway", 4.0, 4.0)
print(student_details)
print(student_details[1])
print(len(student_details))

The tuple constructor function can be used to create a tuple. The values are enclosed in a double string.

regions = tuple(("SE", "EU", "NE", "SE"))
print(regions)

  • Mapping Data Type

Dictionary: is similar to an associative array where a corresponding key can be defined for each value. Dictionaries are separated with commas and enclosed within a curly bracket {}. Python constructor function dict() can be used to create a dictionary.

student_details = {"age" :12, "student_class": "Grade IV", "location":"Norway", "student_grade":4.0}
print(student_details)
print(student_details["age"])
print(len(student_details))

Set Data Type:

Set: is an iterable, mutable (remove or add), unordered, unindexed collection in Python. It can hold different sets of values and the values can be defined when the set method is called in no particular order. It is similar to the Set defined in Mathematics. The Set constructor can be used to create a new set.

students = set()
students.add("Alemsbaja")
students.remove("Alemsbaja")
students.add("Baja")
print(students)

Returns only Baja since the remove method was used to modify the entries by removing Alemsbaja.

Sets can be written with curly brackets {}. Duplicates aren't allowed in the set.

student_id= {231, 431, 901}
print(student_id)

#output {231, 901, 431}

The values 1 and True are the same and will be flagged as duplicates.

student_id= {231, 431, 901, True, 1}
print(student_id)

#output {True, 231, 901, 431}

The values 0 and False are the same and will be interpreted by Python as duplicates in the set.

student_id= {231, 431, 901, False, 0}
print(student_id)

# {False, 231, 901, 431}

To get the type and length of a set data type

#set can hold different values
student_id= {231, 431, 901, 987, "elims12"}
print(type(student_id))
print(len(student_id))

Boolean Data Type:

In real-life scenarios, there are use cases for holding a value that is either true or false. This also happens when building applications and the Boolean data type makes provision for the interpreter to understand when to treat values as true or false.

print(type(True))  
print(type(False))  
print(False)

Defining Data Types in Python

Compared to some programming languages that define the data type of a variable by appending the type in the statement declaration, Python can infer the data type from the value.

Getting the Data type of a variable

The type method is used to get the data type of the value stored in a variable.

name = "alemsbaja"
print(type(name))

#is automatically detected as a String by the interpreter.
#<class 'str'>

Casting of Data types

Casting happens when the value needs to be defined for specificity or converted from one data type to another. The following are various Python construction functions used for converting the data type of a value stored in a variable.

age = str(5)
print(type(age))
#<class 'str'>

score = int("20")
print(type(score))
# <class 'int'>

cgpa = float("20.5") 
print(type(cgpa))
# <class 'float'>

com_numb = complex(1j)
print(com_numb)
# 1j

countries = list(("Ireland", "Turkey", "Nigeria")) 
print(type(countries))
# <class 'list'>


languages = tuple(("py", "java", "js"))
print(type(languages))
# <class 'tuple'>


about_dev = dict(user_name="Alemsbaja", fav_editor="VScode")
print(type(about_dev))
# <class 'dict'>


regions = set(("Stocklom", "London", "Ireland"))
print(type(regions))
#<class 'set'>

Typecasting a value is very handy in developing applications using Python. Always ensure to check and convert the data type to the suitable value for the program or return an error for the proper value to be entered by the user.

Conclusion

Python Data Types are the types defined in Python that describe how the interpreter will handle values which helps the programmer to know what type is suitable for any context of use when developing an application. Technological solutions built from programming are centered around data collection, processing, storage and dissemination. Hence, a need to understand how a programming language treats data to handle them properly.

If you're looking forward to developing applications in Python using the OOP (Object-oriented programming), the major concepts are simplified on my blog here.

Hashnode: Alemsbaja X: Alemsbaja | Youtube: Tech with Alemsbaja to stay updated on more articles

Find this helpful or resourceful?? kindly share and feel free to use the comment section for questions, answers, and contributions.

Did you find this article valuable?

Support Alemoh Rapheal Baja by becoming a sponsor. Any amount is appreciated!