What Are Data Types in Python With Examples?
The foundation of any Python program is understanding how data is stored, represented, and manipulated. One important concept is the sequence data type, which refers to ordered collections that can store multiple values efficiently. These data types represent data in various forms, such as numbers, text, and collections.
Here, we’ll delve into Python data types, the essential building blocks defining the kind of information your variables can hold. From numbers to text and even collections of data, this article will explore the most common types with clear examples, giving you a strong grasp of how to structure your Python code effectively.
Introduction to Data Types
Python is a high-level, interpreted programming language that supports various built-in data types. These data types are the foundation of any Python program, and understanding them is essential for effective programming. One such data type is the python dictionary data type, which is an ordered collection of items stored in key/value pairs.
In Python, data types define the kind of data a variable can hold and the operations that can be performed on that data. From numeric values to text and complex data structures, Python’s built-in data types provide the tools needed to handle a wide range of programming tasks. In this section, we will introduce the concept of data types in Python and explore the different types of data that can be used in Python programming.
What are Python Data Types?
In Python, data types refer to classifying data objects based on their characteristics and operations. In Python, data types are implemented as Python objects, each with its own set of properties and methods. They define the types of values variables can hold and the kinds of operations you can perform on them. Common sequence operations are fundamental for manipulating sequences, including tasks like indexing, slicing, and determining lengths, and are consistent across different sequence types. Python supports various popular data types like strings, numbers, and boolean. Let’s have a deeper look at these data types with examples:
Primitive and Non-primitive Data Types
In Python, data types can be broadly categorized into primitive and non-primitive types. Primitive data types are the most basic types of data that Python can handle. These include integers, floating point numbers, strings, and boolean values. They are the building blocks for data manipulation and are used to create more complex data structures.
Primitive Data Types:
- Integer (int): Represents whole numbers, both positive and negative. They are used for counting, indexing, and performing arithmetic operations.``` age = 30 # Integer value representing age
- **Floating-Point Numbers (float):** Represents real numbers with a decimal point. They are used for precise calculations involving fractions, commonly in scientific computations and financial applications.```
pi = 3.14159 # Floating point number representing the value of pi
- String (str): Represents sequences of characters. They are used for storing and manipulating textual data.``` name = "Alice" # String value representing a name
- **Boolean (bool):** Represents logical values, either True or False. They are used in conditional statements and logical operations.```
is_active = True # Boolean value indicating active status
Non-primitive Data Types: Non-primitive data types, also known as composite data types, store collections of values. They include lists, tuples, dictionaries, and sets. These types allow for more complex data structures and operations.
- List: An ordered collection of items, defined using square brackets. Lists can hold different data types and allow for easy access, modification, and iteration.``` numbers = [1, 2, 3, 4, 5] # List of integer values
- **Tuple:** An ordered collection of items that cannot be changed after creation. Tuples are defined using parentheses.```
colors = ("red", "green", "blue") # Tuple of color names
- Dictionary: A collection of key-value pairs, defined using curly braces. Each key is unique and maps to a value.``` student = {"name": "Alice", "age": 18} # Dictionary representing student information
- **Set:** An unordered collection of unique items, defined using curly braces. Sets are used for membership testing and eliminating duplicate entries.```
fruits = {"apple", "banana", "cherry"} # Set of fruit names
Understanding the difference between primitive and non-primitive data types is essential in Python programming, as it helps in choosing the right data type for a particular problem.
Python String Data Type
Strings in Python are fundamental data types used to represent sequences of characters. A string literal is a sequence of characters enclosed in quotes. They are versatile and widely used for storing and manipulating textual data. You can create strings using either single quotes (‘), double quotes (“), or triple quotes (‘’’ or “””).
Here is an example of a string in Python:
Create strings using different quotes
message1 = ‘Hello, world!’ # Single quotes
message2 = “This is a string with double quotes.”
message3 = “””This string spans multiple lines using triple quotes.”””
Print on separate lines
print(“Messages:”, message1, message2, message3, sep=”\n”)
Access characters using index
first_letter = message1[0] # first_letter will be ‘H’
last_word = message2[-4:] # last_word will be “quotes.” (slice up to, but not including, index -4)
print(“First letter:”, first_letter)
print(“Last word:”, last_word)
String representation is crucial in Python for converting various data types into user-friendly formats using functions like str() and repr().
String Slicing and Concatenation
greeting = message1[:5] # greeting will be “Hello”
place = “world” #place will be “world”
full_message = greeting + “, “ + place + “!”
print(“Full message:”, full_message)
Operations like indexing and slicing can be performed on an existing string to retrieve specific characters or substrings.
String methods - upper and lower case
uppercase_message = message1.upper() # uppercase_message will be “HELLO, WORLD!”
lowercase_message = message2.lower()
print(“Uppercase:”, uppercase_message)
print(“Lowercase:”, lowercase_message)
Find and replace
replaced_message = message3.replace(“string”, “sentence”) # Replace first occurrence
print(“Replaced string:”, replaced_message)
Formatted string (f-string)
name = “Bob”
age = 25
greeting_fstring = f”Hi, {name}. You are {age} years old.”
print(greeting_fstring)
Python Numerical Data Type
Numbers or Numeric data types in Python are used for representing numerical values and performing calculations. Here’s a breakdown of the three main numeric data types:
- Integers (int): Integers are the data types that represent the whole numbers (positive, negative, or zero). In Python, integers generally have unlimited precision, meaning they can store large or small whole numbers. Various numerical concepts, such as binary representation and byte order, define the ways an integer is represented.
Example: age = 30population = 1234567
- Floating-Point Numbers (float): Float represent real numbers with a decimal point. The range of float is approximately 3.4E-38 to 3.4E+38. A float instance in Python can represent real numbers with a decimal point and supports various methods for numerical operations. It is important to test and handle large decimal integer constants carefully to avoid issues during code parsing.
Example: pi = 3.14159 # Approximation of pigravity = 9.81 # Meters per second squared
In scientific notation, the exponent can be a positive or negative integer, allowing for concise representation of very large or very small numbers.
- Complex Numbers: Complex numbers show the values with real and imaginary parts. The imaginary value is denoted by a letter and real through a number. The .hex() and .fromhex() methods facilitate conversion between floating-point values and their hexadecimal string representations.
Example: z = 3 + 2j # Real part is 3, imaginary part is 2
Python Boolean Data Type
The Boolean data type, also known as the bool data type, represents logical values, specifically True or False, which are used to determine the truth value of expressions. It’s fundamental for making decisions and controlling the flow of your program based on certain conditions.
Boolean values can represent only two possible values: True and False.
Below is an example of a Boolean data type:
is_loggedIn = True # Assigning True to a variable
age = 25
is_adult = age >= 18 # Assigning True/False based on a comparison
if is_loggedIn:
print(“Welcome back!”) # Code executes if is_loggedIn is True
if is_adult:
print(“You are eligible to vote.”)
else:
print(“You are not yet eligible to vote.”) # Code executes based on is_adult value
However, in addition to these data types, Python also supports mutable data types such as dictionaries, sets, and lists and immutable data types like tuples. The below section will give you a detailed description of these data types with examples.
Complex Number Data Type
A complex number is a number that can be expressed in the form a + bj, where a and b are real numbers, and j is the imaginary unit. In Python, complex numbers are represented using the ``` complex
data type. Complex numbers can be created using the ```
complex()
function or by using the ``` j
suffix. For example, ```
3 + 4j
is a complex number where 3 is the real part and 4j is the imaginary part. Here’s an example of how to work with complex numbers in Python:
# Creating complex numbers
z1 = 3 + 4j
z2 = complex(2, -3)
# Accessing real and imaginary parts
real_part = z1.real # real_part will be 3.0
imaginary_part = z1.imag # imaginary_part will be 4.0
# Performing arithmetic operations
sum_z = z1 + z2 # sum_z will be (5-1j)
product_z = z1 * z2 # product_z will be (18-1j)
print("Complex Number 1:", z1)
print("Complex Number 2:", z2)
print("Sum:", sum_z)
print("Product:", product_z)
Complex numbers are particularly useful in fields such as engineering and physics, where they are used to represent and manipulate waveforms, electrical circuits, and other phenomena involving oscillations. Additionally, understanding the binary representation of integers is crucial when dealing with complex numbers, as it helps in various computational methods.
Mutable Data Types
Mutable data types in Python are those whose data values can be modified after creation. One such example is the 'bytes object', which can be created using the built-in bytes() function and allows for manipulation of byte data. Mutable data types can be modified by iterating over an iterable object to add, change, or remove elements. You can add, change, or remove elements within the data structure. Mutable data types are further divided into three parts.
Dictionaries
A Python dictionary is a collection of elements. However, it stores the items in a key-value format. Here, the key is a unique identifier for its associated value, while the value can be any type of data you want to store in a dictionary. Two common ways to create a dictionary are by ‘{}’ separated by a comma or using a dict() function.
Here is an example showcasing various dictionary operations:
# Create a dictionary to store student information
student = {
"name": "Alice",
"age": 18,
"course": "Computer Science",
"grades": [85, 90, 78] # List as a value
}
# Accessing values using keys
name = student["name"]
age = student["age"]
print(f"Student Name: {name}")
print(f"Student Age: {age}")
# Modifying a value
student["course"] = "Data Science" # Change course
# Adding a new key-value pair
student["scholarship"] = True
# Iterating through key-value pairs
for key, value in student.items():
print(f"{key}: {value}")
# Checking if a key exists
if "age" in student:
print("Age key exists in the dictionary")
# Accessing a non-existent key (safer method)
grade = student.get("grade", "Not Found") # Returns "Not Found" if key doesn't exist
#Assuming grades is a list
print(f"Average Grade: {sum(student['grades']) / len(student['grades'])}")
Sets
Sets in Python are the unordered collection of unique data elements. Sets are defined by values separated by commas within braces, and they store unique data elements. You can modify the overall collection of elements in a set but cannot change the individual items stored in it. For instance, if you add a string ‘apple’ to a set, you cannot change it to ‘orange’ later within the set.
The example below demonstrates creation, modification, membership checking, and finding intersections with other sets. When checking for membership, you verify if a specified value is present in the set, emphasizing that sets are unordered and do not support indexing.
Create a set of fruits
fruits = {“apple”, “banana”, “cherry”}
Print the set (order may differ each time)
print(“Original fruits:”, fruits)
Check if an element exists (membership checking)
if “mango” in fruits:
print(“Mango is in the set”)
else:
print(“Mango is not in the set”)
Add a new element
fruits.add(“mango”)
print(“Fruits after adding mango:”, fruits)
Remove an element
fruits.remove(“cherry”)
print(“Fruits after removing cherry:”, fruits)
Find the intersection with another set (common elements)
vegetables = {“potato”, “tomato”, “carrot”}
common_items = fruits.intersection(vegetables)
print(“Common items between fruits and vegetables:”, common_items)
Lists
Lists in Python are suitable for storing collections of items in a specific order, defined using square brackets. You can access these elements based on their position (index) in the list. Lists are heterogeneous, as they can hold different data types (like strings, numbers, or even other lists) within the same list.
When performing operations on Python strings, a new string object is produced rather than mutating the existing string.
Here is an example of creation and modification in a list:
Create a list of numbers
numbers = [1, 5, 8, 2]
Print the list
print(“Original numbers:”, numbers)
Access an element using index
second_number = numbers[1] # second_number will be 5
print(“Second element:”, second_number)
Add an element to the end
numbers.append(10)
print(“Numbers after adding 10:”, numbers)
Insert an element at a specific index
numbers.insert(2, 3) # Insert 3 at index 2
print(“Numbers after inserting 3:”, numbers)
Remove the first occurrence of an element
numbers.remove(2)
print(“Numbers after removing first 2:”, numbers)
Remove and get the last element
last_item = numbers.pop()
print(“List after popping:”, numbers)
print(“Popped item:”, last_item) # last_item will be 10
Sort the list in ascending order
numbers.sort()
print(“Numbers sorted in ascending order:”, numbers)
Reverse the order of elements
numbers.reverse()
print(“Numbers in reversed order:”, numbers)
Immutable Data Types
Immutable data types are those whose elements cannot be changed once created, making them a type of immutable sequence. Any attempt to modify an immutable object will always create a new object.
For example, in Python, when you write X = 5, then it means, X = X + 1. You’re not modifying the original value when you add a new value to the variable X. Instead, you create a new value and add it to the variable.
Tuples is a common immutable data type in Python.
Tuples
A Python tuple is an ordered collection of data items that cannot be changed after creation. It can be declared within parentheses with a comma in between. The example below demonstrates how you can work with tuples, considering their ordered and immutable nature.
Create a tuple of colors
colors = (“red”, “green”, “blue”)
Print the tuple
print(“Colors:”, colors)
Access an element using index
first_color = colors[0] # first_color will be “red”
print(“First color:”, first_color)
You can access tuple items using their index, similar to lists
Trying to modify a tuple element (results in an error)
colors[1] = “yellow” # This will cause an error
You can create a new tuple with modifications
modified_colors = colors + (“yellow”,) # Notice the comma after “yellow” for a single-element tuple
print(“Modified colors:”, modified_colors)
Slicing works with tuples for extracting a portion
summer_colors = colors[0:2] # summer_colors will be (“red”, “green”)
print(“Summer colors:”, summer_colors)
Looping through elements
for color in colors:
print(“Color:”, color)
Data Type Conversion
Data type conversion is the process of changing the data type of a value from one type to another. In Python, data type conversion can be performed using built-in functions such as int(), float(), str(), and bool(). These functions can be used to convert between different data types, provided the conversion is valid. For example, int(3.5) converts the floating-point number 3.5 to an integer 3.
In Python, strings are essentially arrays of bytes representing Unicode characters, while integers can also be converted to an array of bytes, taking into account factors like byte order and signedness.
Here are some examples of data type conversion in Python:
Converting float to int
float_value = 3.7 int_value = int(float_value) # int_value will be 3
Converting int to float
int_value = 5 float_value = float(int_value) # float_value will be 5.0
Converting int to string
int_value = 10 str_value = str(int_value) # str_value will be "10"
Converting string to bool
str_value = "True" bool_value = bool(str_value) # bool_value will be True
print("Integer from float:", int_value) print("Float from integer:", float_value) print("String from integer:", str_value) print("Boolean from string:", bool_value)
Understanding data type conversion is crucial for ensuring that your Python code handles data correctly and efficiently, especially when dealing with user input, file I/O, or data from external sources.
How to Check a Data Type in Python?
Now that you have a basic understanding of data types let’s explore different ways to check an object’s data type in Python.
The int class represents the integer data type, showcasing its significance in determining the nature of data that can be stored in variables.
Understanding the internal representation of data types can help in debugging and optimizing your code.
Method 1: Using the type() Function
Python type() is a built-in function that allows you to find the class type of the variable given as input. You have to place the variable name inside the type() function, which outputs the data type.
Here is an example:
a = 60
b = “Hello”
c = 72.34
d = [4, 5, 6]
print(type(a)) # Output: < class ‘int’>
print(type(b)) # Output: < class ‘str’>
print(type(c)) # Output: < class ‘float’>
print(type(d)) # Output: < class ‘list’>
The type() function can also be used to ensure that variables are of the same type before performing operations on them.
Method 2: Using the isinstance() Function
The isinstance() function in Python checks if an object belongs to a specific data type or class. The isinstance() function is an instance method that checks if an object belongs to a specific data type or class. It returns a boolean value, True or False, based on whether the object matches the specified type.
Here is an example:
a = 97
b = 22.65
c = “Python”
print(isinstance(a, int)) # Output: True
print(isinstance(b, float)) # Output: True
print(isinstance(c, str)) # Output: True
Method 3: Using the class attribute
In Python, the class attribute provides information about the class type of an object. Here’s an example code that demonstrates how to use the class attribute to obtain the type of a variable:
a = 25
print(a.class)
#< type ‘int’>
b = “Data”
print(b.class)
#< type ‘str’>
For example, the class attribute of a string object will show that it belongs to the str class.
c = 83.45
print(c.class)
#< type ‘float’>
Output
< class ‘int’>
< class ‘str’>
< class ‘float’>
Here are some relevant resources to help you learn more about data types in Python:
Type Annotation Types
Type annotation types in Python are used to specify the expected type of a variable, function parameter, or return value. These annotations are not enforced by Python but can be used by third-party tools such as type checkers and IDEs to provide better code completion and error detection.
Type annotations can be used with both primitive and non-primitive data types. For example, you can use the ``` int
type annotation to specify that a variable should hold an integer value, or the ```
List[int]
annotation to specify a list of integers.
Here are some examples of type annotations in Python:
- Primitive Data Types:
def add(a: int, b: int) -> int:
return a + b
age: int = 25 # Integer value
pi: float = 3.14159 # Floating point number
name: str = "Alice" # String value
is_active: bool = True # Boolean value
- Non-primitive Data Types
from typing import List, Tuple, Dict
def get_student_info() -> Dict[str, int]:
return {"name": "Alice", "age": 18}
numbers: List[int] = [1, 2, 3, 4, 5] # List of integers
colors: Tuple[str, str, str] = ("red", "green", "blue") # Tuple of strings
Type annotations help in making the code more readable and maintainable by clearly indicating the expected types of variables and function parameters. They also assist in catching type-related errors during development, leading to more robust and error-free code
Modules and Classes
Modules and classes are essential components of Python programming. A module is a file that contains a collection of related functions, classes, and variables. Modules help in organizing code into manageable sections and promoting code reuse. You can use the ``` import
statement to import modules and access their classes and functions.
For example, you can import the ```
math
module to access its classes and functions for mathematical operations:
import math
result = math.sqrt(16) # Using the sqrt function from the math module
print(result) # Output: 4.0
Classes, on the other hand, are templates for creating objects that contain data and functions that operate on that data. Classes allow for the creation of custom data types and encapsulation of related properties and methods.
Here’s an example of defining and using a class in Python:
class Person:
def __init__(self, name: str, age: int):
self.name = name
self.age = age
def greet(self):
print(f"Hello, my name is {self.name} and I am {self.age} years old.")
# Creating an instance of the Person class
person = Person("Alice", 30)
person.greet() # Output: Hello, my name is Alice and I am 30 years old.
By using modules and classes, you can organize your code more effectively, promote code reuse, and create custom data types that suit your specific needs in Python programming.
Functions and Methods
Functions and methods are blocks of code that perform specific tasks. Functions are standalone blocks of code that can be called from anywhere in the program, while methods are functions that are associated with a particular class or object.
In Python, you can define functions using the ``` def
keyword. Functions can take arguments and return values, making them versatile for a wide range of tasks.
Here’s an example of defining and using a function:
def calculate_area(length: float, width: float) -> float: return length * width
area = calculate_area(5.0, 3.0) print(f"Area: {area}") # Output: Area: 15.0
Methods are defined inside a class definition and operate on the data contained within the class. Here’s an example of defining and using a method:
class Rectangle: def init(self, length: float, width: float): self.length = length self.width = width
def area(self) -> float:
return self.length * self.width
Creating an instance of the Rectangle class
rectangle = Rectangle(5.0, 3.0) print(f"Area: {rectangle.area()}") # Output: Area: 15.0
Python also provides built-in functions like ```
int()
, ``` float()
, and ```
str()
to convert between different data types, and methods like ``` upper()
, ```
lower()
, and ``` split()
to process strings. Additionally, you can use the ```
bool()
function to convert a value to a boolean value, and the ``` isinstance()
function to check if an object is an instance of a particular class.
By understanding and utilizing functions and methods, you can write modular, reusable, and efficient code in Python.
Data Type Use Cases
Data types have various use cases in Python programming. Each data type serves a specific purpose and is suited for particular tasks. Here are some common use cases for different data types in Python:
- Integer Data Type: Used to represent whole numbers, both positive and negative. Ideal for counting, indexing, and performing arithmetic operations.
age = 30 # Integer value representing age
These data types hold numeric values, which include integers, floating-point numbers, and complex numbers.
- Floating-Point Numbers: Used to represent decimal numbers and perform precise calculations involving fractions. Commonly used in scientific computations and financial applications.
pi = 3.14159 # Floating point number representing the value of pi
- String Data Type: Used to represent text and perform string processing operations such as concatenation, slicing, and formatting.
name = "Alice" # String value representing a name
- Boolean Data Type: Used to represent true or false values, often used in conditional statements and logical operations.
is_active = True # Boolean value indicating active status
- Dictionary Data Type: Used to represent key-value pairs, making it ideal for storing and retrieving data based on unique keys.
student = {"name": "Alice", "age": 18} # Dictionary representing student information
- List Data Type: Used to represent ordered collections of values, allowing for easy access, modification, and iteration.
numbers = [1, 2, 3, 4, 5] # List of integer values
By understanding the use cases for different data types, you can choose the most appropriate data type for your specific programming needs, ensuring efficient and effective code.
Data Type Best Practices
When working with data types in Python, it’s essential to follow best practices to ensure efficient and effective programming. Here are some best practices to keep in mind:
- Use the correct data type for the task at hand: For example, use ``` int
for whole numbers and ```
float
for decimal numbers. This ensures that your code is both efficient and easy to understand.
count = 10 # Use int for whole numbers
temperature = 36.6 # Use float for decimal numbers
- Avoid using unnecessary data type conversions: Unnecessary conversions can impact performance and make your code harder to read. Only convert data types when absolutely necessary.
# Avoid unnecessary conversion
value = 5
result = float(value) # Only convert if needed for a specific operation
- Use meaningful variable names: Choose variable names that indicate the data type and purpose of the variable. This makes your code more readable and maintainable.
user_age = 25 # Meaningful variable name indicating an integer value
- Use comments to document the data type of a variable: Especially when working with complex data types, comments can help clarify the purpose and type of a variable.
# List of student names
students = ["Alice", "Bob", "Charlie"]
- Use built-in functions and methods: Leverage Python’s built-in functions and methods for data type conversions and operations, rather than relying on manual implementations.
# Using built-in function to convert string to integer
str_value = "123"
int_value = int(str_value)
Streamlining Data Flows with Airbyte
Similar to how Python can effortlessly manage various data types like integers and strings, Airbyte can handle diverse data sources adeptly. No matter if it’s structured information from a database or unstructured content from social media, Airbyte can ingest and integrate data from various sources seamlessly into your data workflows.
The above code demonstrates how Airbyte can be integrated into your data management process, showcasing its versatility and efficiency.
In addition to its user-friendly interface, Airbyte allows you to incorporate it into your Python workflows using PyAirbyte or API. This allows you to programmatically automate data movement between multiple systems, reduce the need for manual data extraction, and streamline your data management process.
Whether you’re dealing with flat files or cloud storage systems, Airbyte simplifies the process, allowing you to focus on extracting insights.
Here are some of the unique features of Airbyte:
- Airbyte offers 600+ pre-built connectors for various source and destination systems. This extensive library ensures you to effortlessly transfer data from one platform to another.
- Airbyte’s PyAirbyte is a Python library that packs most Airbyte connectors into a single code. This streamlines data integration by allowing you to leverage a wide range of connectors for various sources with Python programming.
- If the pre-built list doesn’t cover a specific connector, Airbyte Connector Development Kit (CDK) allows you to build custom connectors using Python.
- You can specify how Airbyte must handle the source schema changes for each connection. This process helps ensure accurate and efficient data syncs, minimize errors, and save time and effort in handling your data pipelines.
Build Smarter Python Workflows — Backed by Clean, Integrated Data
Mastering data types in Python is more than a coding fundamental — it’s the gateway to writing cleaner logic, minimizing bugs, and building programs that scale. But understanding your data is only half the story.
Understanding how Python represents different data types is crucial for integrating them into your workflows.
With Airbyte, you can seamlessly move that data into your Python workflows — no matter the format or source. Whether you’re dealing with APIs, databases, or flat files, Airbyte’s 600+ connectors and PyAirbyte library let you ingest and sync data programmatically, directly into your analysis or automation pipelines.
Let Python do what it does best —and let Airbyte handle the heavy lifting.