top of page
Writer's pictureHui Wang

91. Machine learning: Python Data Structures


Python has several built-in data structures that can be used to store and manipulate different types of data. Some of the most commonly used data structures in Python include:


1. Numbers


Python has several built-in numerical types, including integers (int) and floating-point numbers (float). Numbers are immutable, which means they cannot be modified after they are created.


Examples

# defining an int
a = 5

# defining a float
b = 5.5

# performing arithmetic operations
c = a + b
print(c)  # output: 10.5

2. Strings


A string is a sequence of characters. Strings can be defined using single or double quotes (' or "). Strings are immutable, which means they cannot be modified after they are created.


Examples

# defining a string
a = "hello"
b = 'world'

# string concatenation
c = a + " " + b
print(c)  # output: "hello world"

# accessing a specific character in a string
print(a[1])  # output: 'e'

# accessing a substring
print(c[2:5])  # output: "llo"

3. Lists


A list is an ordered collection of items. Lists are defined using square brackets []. Lists are mutable, which means they can be modified after they are created.


Examples

# defining a list
a = [1, 2, 3, 4, 5]

# accessing an item in a list
print(a[2])  # output: 3

# modifying an item in a list
a[2] = 6
print(a)  # output: [1, 2, 6, 4, 5]

# adding an item to a list
a.append(7)
print(a)  # output: [1, 2, 6, 4, 5, 7]

# removing an item from a list
a.remove(4)
print(a)  # output: [1, 2, 6, 5, 7]

4. Dictionaries


A dictionary is a collection of key-value pairs. Dictionaries are defined using curly braces {}. Dictionaries are mutable, which means they can be modified after they are created.


Examples

# defining a dictionary
a = {"key1": "value1", "key2": "value2"}

# accessing a value in a dictionary
print(a["key1"])  # output: "value1"

# modifying a value in a dictionary
a["key1"] = "new value"
print(a)  # output: {"key1": "new value", "key2": "value2"}

# adding an item to a dictionary
a["key3"] = "value3"
print(a)  # output: {"key1": "new value", "key2": "value2", "key3": "value3"}

# removing an item from a dictionary
del a["key1"]
print(a)  # output: {"key2": "value2", "key3": "value3"}

5. Tuples


A tuple is an ordered collection of items, similar to a list. However, unlike lists, tuples are immutable and cannot be modified after they are created. Tuples are defined using round parentheses ().


Examples

# defining a tuple
a = (1, 2, 3, 4, 5)

# accessing an item in a tuple
print(a[2])  # output: 3

# trying to modify an item in a tuple
a[2] = 6 
# This will cause an error 'tuple' object does not support item assignment 

# concatenating two tuples
b = (6, 7, 8)
c = a + b
print(c)  # output: (1, 2, 3, 4, 5, 6, 7, 8)# defining a tuple
a = (1, 2, 3, 4, 5)

# accessing an item in a tuple
print(a[2])  # output: 3

# trying to modify an item in a tuple
# This will cause an error 'tuple' object does not support item assignment 
a[2] = 6

# concatenating two tuples
b = (6, 7, 8)
c = a + b
print(c)  # output: (1, 2, 3, 4, 5, 6, 7, 8)

# number of occurances of an element
print(c.count(2)) # 1

# find index of the element
print(c.index(6)) # 5

6. Sets


A set is an unordered collection of unique items. Sets are defined using curly braces {} or the set() constructor. Sets are mutable, which means they can be modified after they are created.


Examples

# defining a set
a = {1, 2, 3, 4, 5}

# adding an item to a set
a.add(6)
print(a)  # output: {1, 2, 3, 4, 5, 6}

# removing an item from a set
a.remove(3)
print(a)  # output: {1, 2, 4, 5, 6}

# checking if an item is in a set
print(4 in a)  # output: True

# set union
b = {6, 7, 8}
c = a.union(b)
print(c) # output: {1, 2, 4, 5, 6, 7, 8}

# set intersection
d = a.intersection(b)
print(d) # output: {6}

# set difference
e = a.difference(b)
print(e) # output: {1, 2, 4, 5}

# check if a set is a subset or superset of another set
f = {1, 2}
print(f.issubset(c)) #True
print(c.issuperset(f)) #True

# Clear all the elements from the set
a.clear()
print(a) # set()

Each of these data structures has its specific use cases and advantages. For example, lists are useful for storing multiple items of the same type, while dictionaries are useful for storing data that needs to be accessed using keys rather than index numbers.


Keep in mind that these are just basic examples to showcase the usage of the basic data structures.

 

Follow me on:

Recent Posts

See All

Comments


bottom of page