Posts

Showing posts with the label data structures in python

Python Dictionary Explained for Beginners (With Simple Examples)

Python Dictionary Explained for Beginners (With Simple Examples) Python Dictionary Explained for Beginners (With Simple Examples) Introduction In the previous post, we learned about Python Sets . Now let’s learn one of the most powerful and widely used data structures in Python — the Dictionary . Dictionaries are used to store data in key-value pairs . --- What is a Dictionary in Python? A dictionary is a collection of data that: Stores data in key : value pairs Is unordered Is mutable (can be changed) Does not allow duplicate keys Each key in a dictionary must be unique. --- How to Create a Dictionary Dictionaries are created using curly brackets { } with key-value pairs. student = { "name": "Ayush", "age": 22, "course": "Python" } print(student) --- Accessing Dictionary Values You can access dictionary values using keys. student = { "name": "Ayush...

Python Sets Explained for Beginners (With Simple Examples)

Python Sets Explained for Beginners (With Simple Examples) Python Sets Explained for Beginners (With Simple Examples) Introduction In the previous post, we learned about Python Tuples . Now let’s explore another important data structure in Python called Set . Sets are used to store unique values and are very useful in real-world programming. --- What is a Set in Python? A set is a collection of items that is: Unordered Mutable (can be changed) Does not allow duplicate values Sets automatically remove duplicate values. --- How to Create a Set Sets are created using curly brackets { } . numbers = {1, 2, 3, 4} print(numbers) --- Duplicate Values in Sets Let’s see what happens when we add duplicate values. data = {1, 2, 2, 3, 4, 4} print(data) Duplicate values are removed automatically. --- Set with Different Data Types A set can store different data types. info = {"Ayush", 22, True, 75.5} print(info) --- ...

Python Tuples Explained for Beginners (With Simple Examples)

Python Tuples Explained for Beginners (With Simple Examples) Python Tuples Explained for Beginners (With Simple Examples) Introduction In the previous post, we learned about Python Lists . Now let’s learn another important data structure in Python called Tuple . Tuples are very similar to lists, but with one major difference — they cannot be changed . --- What is a Tuple? A tuple is a collection of items that is: Ordered Immutable (cannot be modified) Allows duplicate values Once a tuple is created, you cannot add, remove, or change its elements. --- How to Create a Tuple Tuples are created using round brackets ( ) . numbers = (10, 20, 30) print(numbers) --- Tuple with Different Data Types A tuple can store multiple data types together. data = ("Ayush", 22, True, 75.5) print(data) --- Accessing Tuple Elements You can access tuple elements using index numbers . colors = ("red", "green", ...