Skip to main content

Python Sets

 

Python Sets


myset = {"apple""banana""cherry"}

Set

Sets are used to store multiple items in a single variable.

Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are ListTuple, and Dictionary, all with different qualities and usage.

A set is a collection which is both unordered and unindexed.

Sets are written with curly brackets.

Example

Create a Set:

thisset = {"apple""banana""cherry"}
print(thisset)

Note: Sets are unordered, so you cannot be sure in which order the items will appear.


Set Items

Set items are unordered, unchangeable, and do not allow duplicate values.


Unordered

Unordered means that the items in a set do not have a defined order.

Set items can appear in a different order every time you use them, and cannot be referred to by index or key.


Unchangeable

Sets are unchangeable, meaning that we cannot change the items after the set has been created.

Once a set is created, you cannot change its items, but you can add new items.


Duplicates Not Allowed

Sets cannot have two items with the same value.

Example

Duplicate values will be ignored:

thisset = {"apple""banana""cherry""apple"}

print(thisset)



Get the Length of a Set

To determine how many items a set has, use the len() method.

Example

Get the number of items in a set:

thisset = {"apple""banana""cherry"}

print(len(thisset))


Set Items - Data Types

Set items can be of any data type:

Example

String, int and boolean data types:

set1 = {"apple""banana""cherry"}
set2 = {15793}
set3 = {TrueFalseFalse}

A set can contain different data types:

Example

A set with strings, integers and boolean values:

set1 = {"abc"34True40"male"}


type()

From Python's perspective, sets are defined as objects with the data type 'set':

<class 'set'>

Example

What is the data type of a set?

myset = {"apple""banana""cherry"}
print(type(myset))


The set() Constructor

It is also possible to use the set() constructor to make a set.

Example

Using the set() constructor to make a set:

thisset = set(("apple""banana""cherry")) # note the double round-brackets
print(thisset)


Python Collections (Arrays)

There are four collection data types in the Python programming language:

  • List is a collection which is ordered and changeable. Allows duplicate members.
  • Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
  • Set is a collection which is unordered and unindexed. No duplicate members.
  • Dictionary is a collection which is ordered* and changeable. No  members.

*As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.

When choosing a collection type, it is useful to understand the properties of that type. Choosing the right type for a particular data set could mean retention of meaning, and, it could mean an increase in efficiency or security.

Comments

Popular posts from this blog

Python Modules

  Python  Modules What is a Module? Consider a module to be the same as a code library. A file containing a set of functions you want to include in your application. Create a Module To create a module just save the code you want in a file with the file extension  .py : Example Save this code in a file named  mymodule.py def  greeting(name):    print ( "Hello, "  + name) Use a Module Now we can use the module we just created, by using the  import  statement: Example Import the module named mymodule, and call the greeting function: import  mymodule mymodule.greeting( "Jonathan" ) Note:  When using a function from a module, use the syntax:  module_name.function_name . Variables in Module The module can contain functions, as already described, but also variables of all types (arrays, dictionaries, objects etc): Example Save this code in the file  mymodule.py person1 = {    "name" :  "John" ,    "age" :  36 ,    "country" :  "

Python Getting Started

  Python  Getting Started Python Install Many PCs and Macs will have python already installed. To check if you have python installed on a Windows PC, search in the start bar for Python or run the following on the Command Line (cmd.exe): C:\Users\ Your Name >python --version To check if you have python installed on a Linux or Mac, then on linux open the command line or on Mac open the Terminal and type: python --version If you find that you do not have python installed on your computer, then you can download it for free from the following website:  https://www.python.org/ Python Quickstart Python is an interpreted programming language, this means that as a developer you write Python (.py) files in a text editor and then put those files into the python interpreter to be executed. The way to run a python file is like this on the command line: C:\Users\ Your Name >python helloworld.py Where "helloworld.py" is the name of your python file. Let's write our first Python fi

Python Introduction

  Python  Introduction What is Python? Python is a popular programming language. It was created by Guido van Rossum, and released in 1991. It is used for: web development (server-side), software development, mathematics, system scripting. What can Python do? Python can be used on a server to create web applications. Python can be used alongside software to create workflows. Python can connect to database systems. It can also read and modify files. Python can be used to handle big data and perform complex mathematics. Python can be used for rapid prototyping, or for production-ready software development. Why Python? Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc). Python has a simple syntax similar to the English language. Python has syntax that allows developers to write programs with fewer lines than some other programming languages. Python runs on an interpreter system, meaning that code can be executed as soon as it is written. This means that prototypin