AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

Python Sets


Python Tutorial » Python Sets

A set is an unordered collection of elements. Every set element is unique (without duplicates) and it cannot be changed, but, a set itself can be change (is mutable). We can add or remove elements from it.
Sets can be used to perform mathematical set operations like intersection, union, symmetric difference, etc.

A set are written by placing all the elements inside curly braces "{}" without quotes, separated by comma, or by using the built-in set() function.
Example: How to create Python Sets:
my_set = {"John", "Chris", "Tom", "Nick"}
print(my_set)
# Note: usually, set list is unordered (items will appear in a random order)

Output:
{'Tom', 'John', 'Nick', 'Chris'}


Python Sets NOT Allow Duplicates - examples



Example: Duplicate item will be ignored
my_set = {"John", "Ann", "Chris", "Tom", "John", "Tom"}
print(my_set)
Output:
{'Tom', 'Chris', 'Ann', 'John'}


How to Get the Length of a Set


Example: how many elements this set has, useing len() method
my_set = {"John", "Ann", "Chris", "Tom"}
print(len(my_set))
Output:
4


Set Items - Data Types


Elements can be of any data type.

Example: elements can be any data type:
my_set1 = {"John", "Ann", "Chris", "Tom"}
my_set2 = {9, 22, 31, 12, 87, 3}
my_set3 = {True, False, False}

print(my_set1)
print(my_set2)
print(my_set3)
Output:
{'John', 'Ann', 'Chris', 'Tom'}
{9, 22, 31, 12, 87, 3}
{True, False, False}




Python Sets methods, difference, vs lists, pop, operation, update, documentation, index, intersection, are mutable or immutable
Python Sets - python

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.