The Zen of Python
It is a collection of 20 software principles that influence the design of the Python Programming Language, 19 of which were written by Tim Peters in June 1999.
Python Zen is written as the 20th informative entry in the Python Enhancement Proposals (PEP), and can be found on the official Python website.
The principles are:
- Beautiful is better than ugly.
- Explicit is better than implicit.
- Simple is better than complex.
- Complex is better than complicated.
- Flat is better than nested.
- Spaced is better than dense.
- Readability is important.
- Special cases are not special enough to break the rules.
- However, practicality trumps purity.
- Mistakes should never happen silently.
- Unless explicitly silenced.
- In the face of ambiguity, avoid the temptation to guess.
- There should be one, and preferably only one, obvious way to do this.
- Although that may not be obvious at first unless you are Dutch.
- Now is better than ever.
- Even though it is never many times better than *right now*.
- If the implementation is difficult to explain, it's a bad idea.
- If the implementation is easy to explain, it may be a good idea.
- Namespaces are a great idea, let's have more of those!
Sets
Sets are used to store several elements in a single variable.
The Sets are one of the 4 built-in Python data types used to store collections of data, the other 3 being List, Tuple y Dictionaryall with different qualities and uses.
A set is a collection disordered, unalterable (Set items cannot be modified, but you can delete items and add new items) and not indexed.
Sets:
- You can modify
- They do not have an order
- Cannot have duplicate elements
set_countries = {"col", "mex", "bol", "col"}
# in the console so we have repeats inside the variable it will not appear
print(set_countries)
print(type(set_countries))
set_numbers = {1, 2, 3, 443, 23}
print(set_numbers)
set_types = {1, "hello", False, 12.12}
#el order does not matter
print(set_types)
set_from_string = set("hello")
print(set_from_string)
set_from_string = set("hoola")
print(set_from_string)
set_from_tuples = set(("abc", "cbv", "as", "abc"))
print(set_from_tuples)
numbers = [1,2,3,1,2,3,4]
# the list is converted to a set
set_numbers = set(numbers)
print(set_numbers)
# since we have the unique numbers we return them to their list form
unique_numbers = list(set_numbers)
print(unique_numbers)
~/Python-102$ python 01_set.py
{'col', 'mex', 'bol'}
{1, 2, 3, 23, 443}
{False, 1, 12.12, 'hello'}
{'l', 'a', 'h', 'o'}
{'l', 'a', 'h', 'o'}
{'as', 'cbv', 'abc'}
{1, 2, 3, 4}
[1, 2, 3, 4]
~/Python-102$
Modifying Sets
Set functions:
- add(): Add an element.
- update(): Add any type of iterable object such as: lists, tuples.
- discard(): It deletes an element and if it already exists, it does not throw any error.
- remove(): Deletes an element and if it does not exist throws the error "keyError".
- pop(): It returns a random element and eliminates it and if the set is empty it throws the error "key error".
- clear(): Removes the entire contents of the set.
set_countries = {"col", "mex", "bol", "col"}
size = len(set_countries)
print(size)
print("col" in set_countries)
print("pe" in set_countries)
# add
set_countries.add("pe")
print(set_countries)
set_countries.add("pe")
print(set_countries)
# update
set_countries.update({"ar", "ecua", "pe"})
print(set_countries)
#remove
set_countries.remove("col")
print(set_countries)
set_countries.remove("ar")
# if an element is not found you can use discard to keep python running despite the error
set_countries.discard("arg")
print(set_countries)
set_countries.add("arg")
print(set_countries)
#with clear we clear the whole set, nothing remains
set_countries.clear()
print(set_countries)
print(len(set_countries))
~/Python-102$ python 02_crud_set.py
3
True
False
{'col', 'pe', 'mex', 'bol'}
{'col', 'pe', 'mex', 'bol'}
{'col', 'pe', 'mex', 'ar', 'ecua', 'bol'}
{'pe', 'mex', 'ar', 'ecua', 'bol'}
{'pe', 'mex', 'ecua', 'bol'}
{'pe', 'mex', 'arg', 'ecua', 'bol'}
set()
0
~/Python-102$
Set operations
- Union(set): Perform the operation "union" between two sets. The union between two sets is to add the elements of these without repeating elements. This operation can also be performed with the sign "|" (pipe): set_a | set_b.
- Intersection(set): Performs the operation "intersection" between two sets. The intersection between two sets is to take only the common elements of the sets. This operation can also be performed with the sign "&": set_a & set_b.
- Difference(set): Perform the operation "difference" between two sets. The difference between two sets is to subtract the elements of the second set from the first set. This operation can also be performed with the sign "–": set_a - set_b.
- Symmetric_difference(set): Performs the operation "symmetric_difference" between two sets. The symmetric difference between two sets consists of subtracting all the elements of both except the common element. This operation can also be performed with the sign "^": set_a ^ set_b. It only unites the elements that they do not have in common.
NOTE: Operations cannot be performed with other data collections, only between sets.
set_a = {"col", "mex", "bol"}
set_b = {"pe", "bol"}
set_c = set_a.union(set_b)
print(set_c)
print(set_a | set_b)
set_c = set_a.intersection(set_b)
print(set_c)
print(set_a & set_b)
set_c = set_a.difference(set_b)
print(set_c)
print(set_a - set_b)
set_c = set_a.symmetric_difference(set_b)
print(set_c)
print(set_a ^ set_b)
~/Python-102$ python 03_operations.py
{'col', 'pe', 'mex', 'bol'}
{'col', 'pe', 'mex', 'bol'}
{'bol'}
{'bol'}
{'col', 'mex'}
{'cabbage', 'mex'}
{'col', 'pe', 'mex'}
{'col', 'pe', 'mex'}
~/Python-102$