diff --git a/CONTRIBUTERS b/CONTRIBUTORS similarity index 67% rename from CONTRIBUTERS rename to CONTRIBUTORS index 27f9ec5..21ef6ae 100644 --- a/CONTRIBUTERS +++ b/CONTRIBUTORS @@ -1,2 +1,4 @@ - brennerm - agumonkey +- obeleh +- Prooffreader \ No newline at end of file diff --git a/README.md b/README.md index 64c68f6..494012e 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,10 @@ Creating a knowledge base of unpopular Python built-in features to save a lot of ## Contribute Feel free to use the PyTrickBase.txt as a starting point. -1. Pull request -Send a pull request with your PyTrick, containing example code and a documentation one-liner. Be sure to add yourself to the contributers. +1. Pull request: -2. Issue comment -Add your Python snippet and your documentation as a comment on Issue#1. I will take care of adding your PyTrick and you as a contributer. + Send a pull request with your PyTrick, containing example code and a documentation one-liner. Be sure to add yourself to the contributors. + +2. Issue comment: + + Add your Python snippet and your documentation as a comment on Issue#1. I will take care of adding your PyTrick and you as a contributor. diff --git a/concatenatestrings.py b/concatenatestrings.py new file mode 100644 index 0000000..fac8d55 --- /dev/null +++ b/concatenatestrings.py @@ -0,0 +1,7 @@ +#! /usr/bin/env python3 +"""Concatenate long strings elegantly +across line breaks in code""" + +my_long_text = ("We are no longer the knights who say Ni! " + "We are now the knights who say ekki-ekki-" + "ekki-p'tang-zoom-boing-z'nourrwringmm!") diff --git a/copylist.py b/copylist.py new file mode 100755 index 0000000..031048a --- /dev/null +++ b/copylist.py @@ -0,0 +1,5 @@ +#! /usr/bin/env python3 +"""a fast way to copy a list""" + +a = [1, 2, 3, 4, 5] +print(a[:]) diff --git a/dictswapkeysvalues.py b/dictswapkeysvalues.py new file mode 100644 index 0000000..73d2e9d --- /dev/null +++ b/dictswapkeysvalues.py @@ -0,0 +1,7 @@ +#! /usr/bin/env python3 +"""Swaps keys and values in a dict""" + +_dict = {"one": 1, "two": 2} +# make sure all of dict's values are unique +assert len(_dict) == len(set(_dict.values())) +reversed_dict = {v: k for k, v in _dict.items()} \ No newline at end of file diff --git a/loopoverlappingdicts.py b/loopoverlappingdicts.py new file mode 100755 index 0000000..ec9d93c --- /dev/null +++ b/loopoverlappingdicts.py @@ -0,0 +1,8 @@ +#! /usr/bin/env python3 +"""loop over dicts that share (some) keys""" + +dctA = {'a': 1, 'b': 2, 'c': 3} +dctB = {'b': 4, 'c': 5, 'd': 6} + +for ky in set(dctA) & set(dctB): + print(ky) diff --git a/namedformatting.py b/namedformatting.py index 7924089..cc71077 100755 --- a/namedformatting.py +++ b/namedformatting.py @@ -2,4 +2,9 @@ """easy string formatting using dicts""" d = {'name': 'Jeff', 'age': 24} -print("My name is %(name)s and I'm %(age)i years old." % d) \ No newline at end of file +print("My name is %(name)s and I'm %(age)i years old." % d) + +"""for .format, use this method""" + +d = {'name': 'Jeff', 'age': 24} +print("My name is {name} and I'm {age} years old.".format(**d))