メインコンテンツにスキップ

Python Data Structures Tutorial

Get introduced to Python data structures: learn more about data types and primitive as well as non-primitive data structures, such as strings, lists, stacks, etc.
· 15 分 読む

AIで探求

Data structures are a way of organizing and storing data so that they can be accessed and worked with efficiently. They define the relationship between the data, and the operations that can be performed on the data. There are many various kinds of data structures defined that make it easier for data scientists and computer engineers alike to concentrate on the main picture of solving larger problems rather than getting lost in the details of data description and access.

In this tutorial, you'll learn about the various Python data structures and see how they are implemented:

  • Abstract Data Type and Data Structures
  • Primitive Data Structures
    • Integers
    • Float
    • Strings
    • Boolean
  • Non-Primitive Data Structures
    • Arrays
    • Lists
    • Tuples
    • Dictionary
    • Sets
    • Files

diagram

In our free Intro to Python for Data Science course, you can learn more about using Python specifically in the data science context. The course gives an introduction to the basic concepts of Python. With it, you'll discover methods, functions, and the NumPy package.

Abstract Data Type and Data Structures

As you read in the introduction, data structures help you to focus on the bigger picture rather than getting lost in the details. This is known as data abstraction.

Now, data structures are actually an implementation of Abstract Data Types or ADT. This implementation requires a physical view of data using some collection of programming constructs and basic data types.

Generally, data structures can be divided into two categories in computer science: primitive and non-primitive data structures. The former are the simplest forms of representing data, whereas the latter are more advanced: they contain primitive data structures within more complex data structures for special purposes.

Primitive Data Structures

These are the most primitive or basic data structures. They are the building blocks for data manipulation and contain pure, simple values of data. Python has four primitive variable types:

  • Integers
  • Float
  • Strings
  • Boolean

In the next sections, you'll learn more about them!

Integers

You can use an integer to represent numeric data and, more specifically, whole numbers from negative infinity to infinity, like 4, 5, or -1.

Float

"Float" stands for 'floating point number'. You can use it for rational numbers, usually ending with a decimal figure, such as 1.11 or 3.14.

Take a look at the following DataCamp Light Chunk and try out some of the integer and float operations!

# Floats
x = 4.0
y = 2.0

# Addition
print(x + y)

# Subtraction
print(x - y)

# Multiplication
print(x * y)

# Returns the quotient
print(x / y)

# Returns the remainder
print(x % y) 

# Absolute value
print(abs(x))

# x to the power y
print(x ** y)

Note that in Python, you do not have to explicitly state the type of the variable or your data. That is because it is a dynamically typed language. Dynamically typed languages are the languages where the type of data an object can store is mutable.

String

Strings are collections of alphabets, words, or other characters. In Python, you can create strings by enclosing a sequence of characters within a pair of single or double quotes. For example: 'cake', "cookie", etc.

You can also apply the + operations on two or more strings to concatenate them, just like in the example below:

x = 'Cake'
y = 'Cookie'
x + ' & ' + y
'Cake & Cookie'

Here are some other basic operations that you can perform with strings; For example, you can use * to repeat a string a certain number of times:

# Repeat
x * 2
'CakeCake'

You can also slice strings, which means that you select parts of strings:

# Range Slicing
z1 = x[2:]  
print(z1)
# Slicing
z2 = y[0] + y[1]
ke
Co

Note that strings can also be alpha-numeric characters, but that the + operation still is used to concatenate strings.

x = '4'
y = '2'
x + y
'42'

Python has many built-in methods or helper functions to manipulate strings. Replacing a substring, capitalising certain words in a paragraph, finding the position of a string within another string are some common string manipulations. Check out some of these:

Capitalize strings

str.capitalize('cookie')

'Cookie'

Retrieve the length of a string in characters. Note that the spaces also count towards the final result:

str1 = "Cake 4 U"
str2 = "404"
len(str1)
8

Check whether a string consists of only digits

str1.isdigit()
False
str2.isdigit()
True

Replace parts of strings with other strings

str1.replace('4 U', str2)
'Cake 404'

Find substrings in other strings; Returns the lowest index or position within the string at which the substring is found:

str1 = 'cookie'
str2 = 'cook'
str1.find(str2)
0
      The substring

'cook'

      is found at the start of

'cookie'

      . As a result, you refer to the position within

'cookie'

      at which you find that substring. In this case,

0

    is returned because you start counting positions from 0!
str1 = 'I got you a cookie'
str2 = 'cook'
str1.find(str2)
12
      Similarly, the substring

'cook'

      is found at position 12 within

'I got you a cookie'

    . Remember that you start counting from 0 and that spaces count towards the positions!

You can find an exhaustive list of string methods in Python here.

Boolean

This built-in data type can take up the values: True and False, which often makes them interchangeable with the integers 1 and 0. Booleans are useful in conditional and comparison expressions, just like in the following examples:

x = 4
y = 2
x == y
False
x > y
True
x = 4
y = 2
z = (x==y) # Comparison expression (Evaluates to false)
if z: # Conditional on truth/false value of 'z'
    print("Cookie")
else: print("No Cookie")