Data Structures in Python (With Examples):

basic-data-structures-in-python

Data structures are an important concept in Python programming, and Python provides a rich set of built-in data structure that makes it easy to work with and manipulate data.

The most common data structure in Python are lists, tuples, sets, and dictionaries. These data structures can be combined and nested to create more complex data structures.

If you want to get more information about Python Fundamentals you can visit here.

Here are some basic data structures in Python with a brief explanation:

Lists: 

A list is an ordered collection of values, which can be of any type. Lists are mutable, which means you can add, remove, and modify elements in the list.

my_list = [1, 2, 3, "four", 5.0]
my_list.append("six") #Here append function is used to enter element to list
my_list.remove(2)
print(my_list)
# Output: [1, 3, 'four', 5.0, 'six']

In the above code new element “six” is appended to the list and “2” is removed from the list.

Tuples:

A tuple is an ordered collection of values, similar to a list. However, tuples are immutable, which means you cannot add, remove, or modify elements in the tuple.

my_tuple = (1, 2, 3, "four", 5.0)
print(my_tuple)
# Output: (1, 2, 3, 'four', 5.0)

Sets: 

A set is an unordered collection of unique values. Sets are mutable, which means you can add and remove elements from the set.

As here you can see.

my_set = {1, 2, 3, "four", 5.0}
my_set.add("six")
my_set.remove(2)
print(my_set)
# Output: {1, 3, 'four', 5.0, 'six'}

Dictionaries:

A dictionary is an unordered collection of key-value pairs. Each key in the dictionary must be unique. Dictionaries are mutable, which means you can add, remove, and modify key-value pairs in the dictionary.

my_dict = {"name": "Alice", "age": 30, "city": "New York"}
my_dict["state"] = "NY"
del my_dict["city"] # del function is used to delete elements
print(my_dict)
# Output: {'name': 'Alice', 'age': 30, 'state': 'NY'}

These are some of the basic data structures in Python. There are other data structures available in Python, such as queues, stacks, and trees, which can be created using classes and objects.

If you want to learn more about python then subscribe to us to get the more latest information about Python.

Was this helpful?
YesNo

Related Articles:

Recent Articles:

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x