{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Cheat Sheet\n", "\n", "Basic cheatsheet for Python mostly based on the book written by Al Sweigart, [Automate the Boring Stuff with Python](https://automatetheboringstuff.com/) under the [Creative Commons license](https://creativecommons.org/licenses/by-nc-sa/3.0/) and many other sources.\n", "\n", "## Read It\n", "\n", "- [Website](https://www.pythoncheatsheet.org)\n", "- [Github](https://github.com/wilfredinni/python-cheatsheet)\n", "- [PDF](https://github.com/wilfredinni/Python-cheatsheet/raw/master/python_cheat_sheet.pdf)\n", "- [Jupyter Notebook](https://mybinder.org/v2/gh/wilfredinni/python-cheatsheet/master?filepath=jupyter_notebooks)\n", "\n", "## sets\n", "\n", "From the Python 3 [documentation](https://docs.python.org/3/tutorial/datastructures.html)\n", "\n", "> A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.\n", "\n", "### Initializing a set\n", "\n", "There are two ways to create sets: using curly braces `{}` and the bult-in function `set()`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = {1, 2, 3}\n", "s = set([1, 2, 3])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When creating an empty set, be sure to not use the curly braces `{}` or you will get an empty dictionary instead." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = {}\n", "type(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### sets: unordered collections of unique elements\n", "\n", "A set automatically remove all the duplicate values." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = {1, 2, 3, 2, 3, 4}\n", "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And as an unordered data type, they can't be indexed." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = {1, 2, 3}\n", "s(0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### set add and update\n", "\n", "Using the `add()` method we can add a single element to the set." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = {1, 2, 3}\n", "s.add(4)\n", "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And with `update()`, multiple ones ." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = {1, 2, 3}\n", "s.update([2, 3, 4, 5, 6])\n", "s # remember, sets automatically remove duplicates" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### set remove and discard\n", "\n", "Both methods will remove an element from the set, but `remove()` will raise a `key error` if the value doesn't exist." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = {1, 2, 3}\n", "s.remove(3)\n", "s" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s.remove(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`discard()` won't raise any errors." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = {1, 2, 3}\n", "s.discard(3)\n", "s" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s.discard(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### set union\n", "\n", "`union()` or `|` will create a new set that contains all the elements from the sets provided." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s1 = {1, 2, 3}\n", "s2 = {3, 4, 5}\n", "s1.union(s2) # or 's1 | s2'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### set intersection\n", "\n", "`intersection` or `&` will return a set containing only the elements that are common to all of them." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s1 = {1, 2, 3}\n", "s2 = {2, 3, 4}\n", "s3 = {3, 4, 5}\n", "s1.intersection(s2, s3) # or 's1 & s2 & s3'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### set difference\n", "\n", "`difference` or `-` will return only the elements that are in one of the sets." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s1 = {1, 2, 3}\n", "s2 = {2, 3, 4}\n", "s1.difference(s2) # or 's1 - s2'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### set symetric_difference\n", "\n", "`symetric_difference` or `^` will return all the elements that are not common between them." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s1 = {1, 2, 3}\n", "s2 = {2, 3, 4}\n", "s1.symmetric_difference(s2) # or 's1 ^ s2'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.0" } }, "nbformat": 4, "nbformat_minor": 2 }