Data Types and Structures in Python

Understanding the Building Blocks of Python Programming

Learn the fundamental data types and structures in Python, including numbers, strings, lists, dictionaries, sets, and tuples. This guide provides clear examples to help you master these essential concepts.

Programming
Author
Affiliation
Published

February 5, 2024

Modified

March 11, 2025

Keywords

Python data types, Python data structures, learn Python, Python numbers, Python strings, Python lists, Python dictionaries, Python sets, Python tuples

Introduction

In Python, understanding data types and structures is essential for writing effective and efficient code. Data types determine the kind of data a variable can hold, while data structures allow you to organize and manage that data efficiently. In this tutorial, we will explore the basic data types such as numbers and strings, and the fundamental data structures including lists, dictionaries, sets, and tuples. Clear code examples are provided to help you grasp these concepts and apply them in your programs.



Data Types in Python

Python supports several built-in data types. The most common include:

  • Numbers:
    Represent numerical values and can be integers or floating-point numbers.
  • Strings:
    Represent sequences of characters, used for text.
  • Booleans:
    Represent truth values, either True or False.

Data Structures in Python

Data structures allow you to group related data together. The primary data structures in Python include:

  • Lists:
    Ordered, mutable collections that can hold items of different types.
  • Tuples:
    Ordered, immutable collections.
  • Dictionaries:
    Unordered, mutable collections of key-value pairs.
  • Sets:
    Unordered collections of unique elements.

Practical Examples

Below, you can see examples of Python code that demonstrate these data types and structures, alongside equivalent examples in R for comparison.

# Numbers and Strings
integer_num = 42
float_num = 3.14159
greeting = "Hello, World!"

# List: Mutable and ordered collection
fruits = ["apple", "banana", "cherry"]

# Tuple: Immutable and ordered collection
dimensions = (1920, 1080)

# Dictionary: Unordered collection of key-value pairs
person = {"name": "Alice", "age": 30, "city": "New York"}

# Set: Unordered collection of unique items
unique_numbers = {1, 2, 3, 2, 1}

print("Integer:", integer_num)
print("Float:", float_num)
print("Greeting:", greeting)
print("Fruits:", fruits)
print("Dimensions:", dimensions)
print("Person:", person)
print("Unique Numbers:", unique_numbers)
# Numbers and Strings
integer_num <- 42
float_num <- 3.14159
greeting <- "Hello, World!"

# List: In R, lists are ordered and can contain elements of different types (similar to Python lists)
fruits <- list("apple", "banana", "cherry")

# Tuple: R doesn't have a direct equivalent to tuples, but vectors can be used when immutability is desired.
dimensions <- c(1920, 1080)

# Dictionary: In R, a named list or environment can serve as a dictionary
person <- list(name = "Alice", age = 30, city = "New York")

# Set: R does not have a native set type, but unique() can be used on vectors
numbers <- c(1, 2, 3, 2, 1)
unique_numbers <- unique(numbers)

print(paste("Integer:", integer_num))
print(paste("Float:", float_num))
print(paste("Greeting:", greeting))
print("Fruits:")
print(fruits)
print("Dimensions:")
print(dimensions)
print("Person:")
print(person)
print("Unique Numbers:")
print(unique_numbers)

Conclusion

Understanding data types and structures is a critical step in learning Python. By mastering these concepts, you gain the ability to store, manipulate, and organize data effectively—an essential skill for any programmer. Practice these examples and experiment with your own variations to build a strong foundation in Python programming.

Further Reading

Happy coding, and enjoy your journey into Python programming!

Back to top

Reuse

Citation

BibTeX citation:
@online{kassambara2024,
  author = {Kassambara, Alboukadel},
  title = {Data {Types} and {Structures} in {Python}},
  date = {2024-02-05},
  url = {https://www.datanovia.com/learn/programming/python/basics/data-types-and-structures.html},
  langid = {en}
}
For attribution, please cite this work as:
Kassambara, Alboukadel. 2024. “Data Types and Structures in Python.” February 5, 2024. https://www.datanovia.com/learn/programming/python/basics/data-types-and-structures.html.