The Wayback Machine - https://web.archive.org/web/20240927030707/https://www.geeksforgeeks.org/python-sets/
Open In App

Python Sets

Last Updated : 21 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Python Set is an unordered collection of data types that is iterable, mutable, and has no duplicate elements. The order of elements in a set is undefined though it may consist of various elements. The major advantage of using a set, as opposed to a list, is that it has a highly optimized method for checking whether a specific element is contained in the set. Here, we will see what is a set in Python and also see different examples of set Python.

Creating a Set in Python

Python Sets can be created by using the built-in set() function with an iterable object or a sequence by placing the sequence inside curly braces, separated by a ‘comma’.

Note: A Python set cannot have mutable elements like a list or dictionary, as it is immutable. 

Python
# Creating a Set
set1 = set()
print("Initial blank Set: ")
print(set1)

# Creating a Set with the use of a String
set1 = set("GeeksForGeeks")
print("\nSet with the use of String: ")
print(set1)

String = 'GeeksForGeeks'
set1 = set(String)
print("\nSet with the use of an Object: ")
print(set1)

# Creating a Set with the use of a List
set1 = set(["Geeks", "For", "Geeks"])
print("\nSet with the use of List: ")
print(set1)

# Creating a Set with the use of a tuple
t = ("Geeks", "for", "Geeks")
print("\nSet with the use of Tuple: ")
print(set(t))

# Creating a Set with the use of a dictionary
d = {"Geeks": 1, "for": 2, "Geeks": 3}
print("\nSet with the use of Dictionary: ")
print(set(d))

Ouput

Initial blank Set: 
set()

Set with the use of String:
{'e', 'G', 's', 'F', 'o', 'r', 'k'}

Set with the use of an Object:
{'e', 'G', 's', 'F', 'o', 'r', 'k'}

Set with the use of List:
{'For', 'Geeks'}

Set with the use of Tuple:
{'for', 'Geeks'}

Set with the use of Dictionary:
{'for', 'Geeks'}

Time complexity: O(n), where n is the length of the input string, list, tuple or dictionary.
Auxiliary space: O(n), where n is the length of the input string, list, tuple or dictionary.

A Python set contains only unique elements but at the time of set creation, multiple duplicate values can also be passed. Order of elements in a Python set is undefined and is unchangeable. Type of elements in a set need not be the same, various mixed-up data type values can also be passed to the set. 

Python
# Creating a Set with a List of Numbers
# (Having duplicate values)
set1 = set([1, 2, 4, 4, 3, 3, 3, 6, 5])
print("\nSet with the use of Numbers: ")
print(set1)

# Creating a Set with a mixed type of values
# (Having numbers and strings)
set1 = set([1, 2, 'Geeks', 4, 'For', 6, 'Geeks'])
print("\nSet with the use of Mixed Values")
print(set1)

Output
Set with the use of Numbers: 
{1, 2, 3, 4, 5, 6}

Set with the use of Mixed Values
{1, 2, 4, 6, 'Geeks', 'For'}

Creating a Python Set with Another Method

In this example, a set is created by using curly braces {} notation, containing the number 1,2 and 3. Set data structure in Python or unordered set in Python are unordered collections of unique elements, making them suitable for tasks requiring uniqueness and set operations in Python.

Python
# Another Method to create sets in Python3
# Set containing numbers
my_set = {1, 2, 3}

print(my_set)

Output
{1, 2, 3}

Adding Elements to a Set in Python

Below are some of the approaches by which we can add elements to a set in Python:

  • Using add() Method
  • Using update() Method

Using add() Method

Elements can be added to the Sets in Python by using the built-in add() function. Only one element at a time can be added to the set by using add() method, loops are used to add multiple elements at a time with the use of add() method.

Note: Lists cannot be added to a set as elements because Lists are not hashable whereas Tuples can be added because tuples are immutable and hence Hashable. 

Python
# Creating a Set
set1 = set()
print("Initial blank Set: ")
print(set1)

# Adding element and tuple to the Set
set1.add(8)
set1.add(9)
set1.add((6, 7))
print("\nSet after Addition of Three elements: ")
print(set1)

# Adding elements to the Set
# using Iterator
for i in range(1, 6):
    set1.add(i)
print("\nSet after Addition of elements from 1-5: ")
print(set1)

Output
Initial blank Set: 
set()

Set after Addition of Three elements: 
{8, 9, (6, 7)}

Set after Addition of elements from 1-5: 
{1, 2, 3, (6, 7), 4, 5, 8, 9}

Using update() Method

For the addition of two or more elements, Update() method is used. The update() method accepts lists, strings, tuples as well as other Python hash set as its arguments. In all of these cases, duplicate elements are avoided.

Python
# Addition of elements to the Set
# using Update function
set1 = set([4, 5, (6, 7)])
set1.update([10, 11])
print("\nSet after Addition of elements using Update: ")
print(set1)

Output
Set after Addition of elements using Update: 
{4, 5, (6, 7), 10, 11}

Accessing a Set in Python

Set items cannot be accessed by referring to an index, since sets are unordered the items has no index. But you can loop through the Python hash set items using a for loop, or ask if a specified value is present in a set, by using the in keyword.

Python
# Creating a set
set1 = set(["Geeks", "For", "Geeks."])
print("\nInitial set")
print(set1)

# Accessing element using
# for loop
print("\nElements of set: ")
for i in set1:
    print(i, end=" ")

# Checking the element
# using in keyword
print("\n")
print("Geeks" in set1)

Output

Initial set
{'Geeks.', 'For', 'Geeks'}

Elements of set:
Geeks. For Geeks

True

Removing Elements from the Set in Python

Below are some of the ways by which we can remove elements from the set in Python:

  • Using remove() Method or discard() Method
  • Using pop() Method
  • Using clear() Method

Using remove() Method or discard() Method

Elements can be removed from the Sets in Python by using the built-in remove() function but a KeyError arises if the element doesn’t exist in the hashset Python. To remove elements from a set without KeyError, use discard(), if the element doesn’t exist in the set, it remains unchanged.

Python
# Creating a Set
set1 = set([1, 2, 3, 4, 5, 6,
            7, 8, 9, 10, 11, 12])
print("Initial Set: ")
print(set1)

# Removing elements from Set  using Remove() method
set1.remove(5)
set1.remove(6)
print("\nSet after Removal of two elements: ")
print(set1)

# Removing elements from Set using Discard() method
set1.discard(8)
set1.discard(9)
print("\nSet after Discarding two elements: ")
print(set1)

# Removing elements from Set using iterator method
for i in range(1, 5):
    set1.remove(i)
print("\nSet after Removing a range of elements: ")
print(set1)

Output
Initial Set: 
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

Set after Removal of two elements: 
{1, 2, 3, 4, 7, 8, 9, 10, 11, 12}

Set after Discarding two elements: 
{1, 2, 3, 4, 7, 10, 11, 12}

Set after Removing a range of elements: 
{7, 10, 11, 12}

Using pop() Method

Pop() function can also be used to remove and return an element from the set, but it removes only the last element of the set. 

Note: If the set is unordered then there’s no such way to determine which element is popped by using the pop() function. 

Python
# Python program to demonstrate
# Deletion of elements in a Set

# Creating a Set
set1 = set([1, 2, 3, 4, 5, 6,
            7, 8, 9, 10, 11, 12])
print("Initial Set: ")
print(set1)

# Removing element from the
# Set using the pop() method
set1.pop()
print("\nSet after popping an element: ")
print(set1)

Output
Initial Set: 
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

Set after popping an element: 
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

Using clear() Method

To remove all the elements from the set, clear() function is used. 

Python
#Creating a set
set1 = set([1,2,3,4,5])
print("\n Initial set: ")
print(set1)


# Removing all the elements from 
# Set using clear() method
set1.clear()
print("\nSet after clearing all the elements: ")
print(set1)

Output
 Initial set: 
{1, 2, 3, 4, 5}

Set after clearing all the elements: 
set()

Frozen Sets in Python

Frozen sets in Python are immutable objects that only support methods and operators that produce a result without affecting the frozen set or sets to which they are applied. While elements of a set can be modified at any time, elements of the frozen set remain the same after creation. 

If no parameters are passed, it returns an empty frozenset.  

Python
# Python program to demonstrate
# working of a FrozenSet

# Creating a Set
String = ('G', 'e', 'e', 'k', 's', 'F', 'o', 'r')

Fset1 = frozenset(String)
print("The FrozenSet is: ")
print(Fset1)

# To print Empty Frozen Set
# No parameter is passed
print("\nEmpty FrozenSet: ")
print(frozenset())

Output
The FrozenSet is: 
frozenset({'F', 's', 'o', 'G', 'r', 'e', 'k'})

Empty FrozenSet: 
frozenset()

Typecasting Objects into Sets

In this example, lists, strings and dictionaries are converted into sets using the set() constructor, eliminating duplicates in lists and extracting unique elements in strings and dictionary keys.

Python
# Typecasting list into set
my_list = [1, 2, 3, 3, 4, 5, 5, 6, 2]
my_set = set(my_list)
print("my_list as a set: ", my_set)

# Typecasting string into set
my_str = "GeeksforGeeks"
my_set1 = set(my_str)
print("my_str as a set: ", my_set1)

# Typecasting dictionary into set
my_dict = {1: "One", 2: "Two", 3: "Three"}
my_set2 = set(my_dict)
print("my_dict as a set: ", my_set2)

Output
my_list as a set:  {1, 2, 3, 4, 5, 6}
my_str as a set:  {'G', 'f', 'r', 'e', 'k', 'o', 's'}
my_dict as a set:  {1, 2, 3}

Example: Implementing All Functions

In this example, a series of functions demonstrate common operations on sets in Python. These include creating a set, adding and removing elements, clearing the set, performing set union, intersection, difference, symmetric difference, subset, and superset operations.

Python
def create_set():
    my_set = {1, 2, 3, 4, 5}
    print(my_set)


def add_element():
    my_set = {1, 2, 3, 4, 5}
    my_set.add(6)
    print(my_set)


def remove_element():
    my_set = {1, 2, 3, 4, 5}
    my_set.remove(3)
    print(my_set)


def clear_set():
    my_set = {1, 2, 3, 4, 5}
    my_set.clear()
    print(my_set)


def set_union():
    set1 = {1, 2, 3}
    set2 = {4, 5, 6}
    my_set = set1.union(set2)
    print(my_set)


def set_intersection():
    set1 = {1, 2, 3, 4, 5}
    set2 = {4, 5, 6, 7, 8}
    my_set = set1.intersection(set2)
    print(my_set)


def set_difference():
    set1 = {1, 2, 3, 4, 5}
    set2 = {4, 5, 6, 7, 8}
    my_set = set1.difference(set2)
    print(my_set)


def set_symmetric_difference():
    set1 = {1, 2, 3, 4, 5}
    set2 = {4, 5, 6, 7, 8}
    my_set = set1.symmetric_difference(set2)
    print(my_set)


def set_subset():
    set1 = {1, 2, 3, 4, 5}
    set2 = {2, 3, 4}
    subset = set2.issubset(set1)
    print(subset)


def set_superset():
    set1 = {1, 2, 3, 4, 5}
    set2 = {2, 3, 4}
    superset = set1.issuperset(set2)
    print(superset)


if __name__ == '__main__':
    create_set()
    add_element()
    remove_element()
    clear_set()
    set_union()
    set_intersection()
    set_difference()
    set_symmetric_difference()
    set_subset()
    set_superset()

Output
{1, 2, 3, 4, 5}
{1, 2, 3, 4, 5, 6}
{1, 2, 4, 5}
set()
{1, 2, 3, 4, 5, 6}
{4, 5}
{1, 2, 3}
{1, 2, 3, 6, 7, 8}
True
True

Advantages of Set in Python

  • Unique Elements: Sets can only contain unique elements, so they can be useful for removing duplicates from a collection of data.
  • Fast Membership Testing: Sets are optimized for fast membership testing, so they can be useful for determining whether a value is in a collection or not.
  • Mathematical Set Operations: Sets support mathematical set operations like union, intersection, and difference, which can be useful for working with sets of data.
  • Mutable: Sets are mutable, which means that you can add or remove elements from a set after it has been created.

Disadvantages of Sets in Python

  • Unordered: Sets are unordered, which means that you cannot rely on the order of the data in the set. This can make it difficult to access or process data in a specific order.
  • Limited Functionality: Sets have limited functionality compared to lists, as they do not support methods like append() or pop(). This can make it more difficult to modify or manipulate data stored in a set.
  • Memory Usage: Sets can consume more memory than lists, especially for small datasets. This is because each element in a set requires additional memory to store a hash value.
  • Less Commonly Used: Sets are less commonly used than lists and dictionaries in Python, which means that there may be fewer resources or libraries available for working with them. This can make it more difficult to find solutions to problems or to get help with debugging.

Overall, sets can be a useful data structure in Python, especially for removing duplicates or for fast membership testing. However, their lack of ordering and limited functionality can also make them less versatile than lists or dictionaries, so it is important to carefully consider the advantages and disadvantages of using sets when deciding which data structure to use in your Python program.

Set Methods in Python

FunctionDescription
add()Adds an element to a set
remove()Removes an element from a set. If the element is not present in the set, raise a KeyError
clear()Removes all elements form a set
copy()Returns a shallow copy of a set
pop()Removes and returns an arbitrary set element. Raise KeyError if the set is empty
update()Updates a set with the union of itself and others
union()Returns the union of sets in a new set
difference()Returns the difference of two or more sets as a new set
difference_update()Removes all elements of another set from this set
discard()Removes an element from set if it is a member. (Do nothing if the element is not in set)
intersection()Returns the intersection of two sets as a new set
intersection_update()Updates the set with the intersection of itself and another
isdisjoint()Returns True if two sets have a null intersection
issubset()Returns True if another set contains this set
issuperset()Returns True if this set contains another set
symmetric_difference()Returns the symmetric difference of two sets as a new set
symmetric_difference_update()Updates a set with the symmetric difference of itself and another

Recent Articles on Python Sets

Set Programs

Useful Links

Python Sets – FAQs

What are Sets in Python?

Sets in Python are a data type that store unordered collections of unique elements. They are useful for performing mathematical set operations like union, intersection, difference, and symmetric difference. Sets automatically remove any duplicate entries and do not maintain any order of the elements.

Example:

my_set = {1, 2, 3, 3, 2}
print(my_set) # Output: {1, 2, 3}

How to Input Set in Python?

To input a set in Python, you can take input as a string and convert it into a set using the set() function. If you’re taking multiple values, you might consider splitting the string and then converting each element.

Example:

input_string = input("Enter elements separated by space: ")
input_list = input_string.split() # Split string into a list of elements
input_set = set(input_list) # Convert list to set to remove duplicates
print(input_set)

How Do You Define a Set?

You can define a set by listing its elements between curly braces {}. If you need to define an empty set, you must use set() because {} creates an empty dictionary.

Example:

# Define a set with elements
a_set = {1, 2, 3}

# Define an empty set
empty_set = set()

Is a Set a List in Python?

No, a set is not a list in Python. Although both are collections, they serve different purposes and have different properties. Lists are ordered collections that can contain duplicates, while sets are unordered collections that do not contain duplicates.

What is the Difference Between a List and a Set?

The key differences between a list and a set in Python are:

  • Order: Lists maintain the order of elements, meaning you can access elements by their position. Sets are unordered, and elements do not have a fixed position.
  • Mutability: Both lists and sets are mutable, meaning you can add or remove elements. However, the elements of a set must be immutable (e.g., numbers, strings, tuples).
  • Duplicates: Lists can contain duplicate elements, making them suitable for use cases where data ordering and repetition are important. Sets automatically remove duplicate elements and are useful for membership testing, removing duplicates, and mathematical operations like set unions and intersections.
  • Syntax: Lists are defined using square brackets [], whereas sets use curly braces {} or the set() constructor for an empty set or if initializing from an iterable.

These distinctions make sets particularly useful for operations involving membership tests and unique collections, while lists are more suited for ordered data that may include repeated elements.



Previous Article
Next Article

Similar Reads

Output of Python Programs | Set 24 (Sets)
Prerequisite: Python-Sets 1. What is the output of the code shown below? sets = {1, 2, 3, 4, 4} print(sets) Options: {1, 2, 3} {1, 2, 3, 4} {1, 2, 3, 4, 4} Error Output: 2. {1, 2, 3, 4} Explanation : Duplicate values are not allowed in sets. Hence, the output of the code shown above will be a set containing the duplicate value only once. Hence outp
2 min read
Python Set | Pairs of complete strings in two sets
Two strings are said to be complete if on concatenation, they contain all the 26 English alphabets. For example, “abcdefghi” and “jklmnopqrstuvwxyz” are complete as they together have all characters from ‘a’ to ‘z’. We are given two sets of sizes n and m respectively and we need to find the number of pairs that are complete on concatenating each st
2 min read
Python | sympy.sets.open() method
With the help of sympy.sets.open() method, we can make a set of values by setting interval values like right open or left open that means a set has right open bracket and left open brackets by using sympy.sets.open() method. Syntax : sympy.sets.open(val1, val2) Return : Return set of values with right and left open set. Example #1 : In this example
1 min read
Python | sympy.sets.Ropen() method
With the help of sympy.sets.Ropen() method, we can make a set of values by setting interval values like right open that means a set has right open bracket and left close one by using sympy.sets.Ropen() method. Syntax : sympy.sets.Ropen(val1, val2) Return : Return set of values with right open set. Example #1 : In this example we can see that by usi
1 min read
Python | sympy.sets.Lopen() method
With the help of sympy.sets.Lopen() method, we can make a set of values by setting interval values like left open that means a set has left open bracket and right close one by using sympy.sets.Lopen() method. Syntax : sympy.sets.Lopen(val1, val2) Return : Return set of values with left open set. Example #1 : In this example we can see that by using
1 min read
Python Tokens and Character Sets
Python is a general-purpose, high-level programming language. It was designed with an emphasis on code readability, and its syntax allows programmers to express their concepts in fewer lines of code, and these codes are known as scripts. These scripts contain character sets, tokens, and identifiers. In this article, we will learn about these charac
6 min read
Python | remove() and discard() in Sets
In this article, we will see how to remove an element in a set, using the discard() and remove() method. We will also learn the difference between the two methods, although they produce the same results. Example Input: set = ([10, 20, 26, 41, 54, 20]) Output: {41, 10, 26, 54} Input: set = (["ram", "aakash", "kaushik", "anand", "prashant"]) Output:
3 min read
Sets in Python
A Set in Python programming is an unordered collection data type that is iterable and has no duplicate elements. While sets are mutable, meaning you can add or remove elements after their creation, the individual elements within the set must be immutable and cannot be changed directly. Set are represented by { } (values enclosed in curly braces) Th
9 min read
Important differences between Python 2.x and Python 3.x with examples
In this article, we will see some important differences between Python 2.x and Python 3.x with the help of some examples. Differences between Python 2.x and Python 3.x Here, we will see the differences in the following libraries and modules: Division operatorprint functionUnicodexrangeError Handling_future_ modulePython Division operatorIf we are p
5 min read
Reading Python File-Like Objects from C | Python
Writing C extension code that consumes data from any Python file-like object (e.g., normal files, StringIO objects, etc.). read() method has to be repeatedly invoke to consume data on a file-like object and take steps to properly decode the resulting data. Given below is a C extension function that merely consumes all of the data on a file-like obj
3 min read
Python | Add Logging to a Python Script
In this article, we will learn how to have scripts and simple programs to write diagnostic information to log files. Code #1 : Using the logging module to add logging to a simple program import logging def main(): # Configure the logging system logging.basicConfig(filename ='app.log', level = logging.ERROR) # Variables (to make the calls that follo
2 min read
Python | Add Logging to Python Libraries
In this article, we will learn how to add a logging capability to a library, but don’t want it to interfere with programs that don’t use logging. For libraries that want to perform logging, create a dedicated logger object, and initially configure it as shown in the code below - Code #1 : C/C++ Code # abc.py import logging log = logging.getLogger(_
2 min read
Python | Index of Non-Zero elements in Python list
Sometimes, while working with python list, we can have a problem in which we need to find positions of all the integers other than 0. This can have application in day-day programming or competitive programming. Let's discuss a shorthand by which we can perform this particular task. Method : Using enumerate() + list comprehension This method can be
6 min read
MySQL-Connector-Python module in Python
MySQL is a Relational Database Management System (RDBMS) whereas the structured Query Language (SQL) is the language used for handling the RDBMS using commands i.e Creating, Inserting, Updating and Deleting the data from the databases. SQL commands are case insensitive i.e CREATE and create signify the same command. In this article, we will be disc
2 min read
Python - Read blob object in python using wand library
BLOB stands for Binary Large OBject. A blob is a data type that can store binary data. This is different than most other data types used in databases, such as integers, floating point numbers, characters, and strings, which store letters and numbers. BLOB is a large complex collection of binary data which is stored in Database. Basically BLOB is us
2 min read
twitter-text-python (ttp) module - Python
twitter-text-python is a Tweet parser and formatter for Python. Amongst many things, the tasks that can be performed by this module are : reply : The username of the handle to which the tweet is being replied to. users : All the usernames mentioned in the tweet. tags : All the hashtags mentioned in the tweet. urls : All the URLs mentioned in the tw
3 min read
Reusable piece of python functionality for wrapping arbitrary blocks of code : Python Context Managers
Context Managers are the tools for wrapping around arbitrary (free-form) blocks of code. One of the primary reasons to use a context manager is resource cleanliness. Context Manager ensures that the process performs steadily upon entering and on exit, it releases the resource. Even when the wrapped code raises an exception, the context manager guar
7 min read
Creating and updating PowerPoint Presentations in Python using python - pptx
python-pptx is library used to create/edit a PowerPoint (.pptx) files. This won't work on MS office 2003 and previous versions. We can add shapes, paragraphs, texts and slides and much more thing using this library. Installation: Open the command prompt on your system and write given below command: pip install python-pptx Let's see some of its usag
4 min read
Python Debugger – Python pdb
Debugging in Python is facilitated by pdb module (python debugger) which comes built-in to the Python standard library. It is actually defined as the class Pdb which internally makes use of bdb(basic debugger functions) and cmd (support for line-oriented command interpreters) modules. The major advantage of pdb is it runs purely in the command line
5 min read
Filter Python list by Predicate in Python
In this article, we will discuss how to filter a python list by using predicate. Filter function is used to filter the elements in the given list of elements with the help of a predicate. A predicate is a function that always returns True or False by performing some condition operations in a filter method Syntax: filter(predicate, list) where, list
2 min read
Python: Iterating With Python Lambda
In Python, the lambda function is an anonymous function. This one expression is evaluated and returned. Thus, We can use lambda functions as a function object. In this article, we will learn how to iterate with lambda in python. Syntax: lambda variable : expression Where, variable is used in the expressionexpression can be an mathematical expressio
2 min read
Python Value Error :Math Domain Error in Python
Errors are the problems in a program due to which the program will stop the execution. One of the errors is 'ValueError: math domain error' in Python. In this article, you will learn why this error occurs and how to fix it with examples. What is 'ValueError: math domain error' in Python?In mathematics, we have certain operations that we consider un
4 min read
Creating Your Own Python IDE in Python
In this article, we are able to embark on an adventure to create your personal Python Integrated Development Environment (IDE) the usage of Python itself, with the assistance of the PyQt library. What is Python IDE?Python IDEs provide a characteristic-rich environment for coding, debugging, and going for walks in Python packages. While there are ma
3 min read
GeeksforGeeks Python Foundation Course - Learn Python in Hindi!
Python - it is not just an ordinary programming language but the doorway or basic prerequisite for getting into numerous tech domains including web development, machine learning, data science, and several others. Though there's no doubt that the alternatives of Python in each of these respective areas are available - but the dominance and popularit
5 min read
Python math.sqrt() function | Find Square Root in Python
sqrt() function returns square root of any number. It is an inbuilt function in Python programming language. In this article, we will learn more about the Python Program to Find the Square Root. sqrt() Function We can calculate square root in Python using the sqrt() function from the math module. In this example, we are calculating the square root
3 min read
Python Image Editor Using Python
You can create a simple image editor using Python by using libraries like Pillow(PIL) which will provide a wide range of image-processing capabilities. In this article, we will learn How to create a simple image editor that can perform various operations like opening an image, resizing it, blurring the image, flipping and rotating the image, and so
13 min read
Python | Set 4 (Dictionary, Keywords in Python)
In the previous two articles (Set 2 and Set 3), we discussed the basics of python. In this article, we will learn more about python and feel the power of python. Dictionary in Python In python, the dictionary is similar to hash or maps in other languages. It consists of key-value pairs. The value can be accessed by a unique key in the dictionary. (
5 min read
Learn DSA with Python | Python Data Structures and Algorithms
This tutorial is a beginner-friendly guide for learning data structures and algorithms using Python. In this article, we will discuss the in-built data structures such as lists, tuples, dictionaries, etc, and some user-defined data structures such as linked lists, trees, graphs, etc, and traversal as well as searching and sorting algorithms with th
15+ min read
Why we write #!/usr/bin/env python on the first line of a Python script?
The shebang line or hashbang line is recognized as the line #!/usr/bin/env python. This helps to point out the location of the interpreter intended for executing a script, especially in Unix-like operating systems. For example, Linux and macOS are Unix-like operating systems whose executable files normally start with a shebang followed by a path to
2 min read
Setting up ROS with Python 3 and Python OpenCV
Setting up a Robot Operating System (ROS) with Python 3 and OpenCV can be a powerful combination for robotics development, enabling you to leverage ROS's robotics middleware with the flexibility and ease of Python programming language along with the computer vision capabilities provided by OpenCV. Here's a step-by-step guide to help you set up ROS
3 min read