GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — learn-python/src/data_types/test_sets.py at master · anandme/learn-python Expand file tree/ Copy pathtest_sets.pyMore file actions 70 lines (48 loc) · 2.27 KB / Copy pathtest_sets.pyFile metadata and controls70 lines (48 loc) · 2.27 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 """Sets. @see: https://www.w3schools.com/python/python_sets.asp @see: https://docs.python.org/3.7/tutorial/datastructures.html#sets A set is a collection which is unordered and unindexed. In Python sets are written with curly brackets. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference. """ def test_sets(): """Sets""" fruits_set = {"apple", "banana", "cherry"} assert isinstance(fruits_set, set) # It is also possible to use the set() constructor to make a set. # Note the double round-brackets fruits_set_via_constructor = set(("apple", "banana", "cherry")) assert isinstance(fruits_set_via_constructor, set) def test_set_methods(): """Set methods""" fruits_set = {"apple", "banana", "cherry"} # You may check if the item is in set by using "in" statement assert "apple" in fruits_set assert "pineapple" not in fruits_set # Use the len() method to return the number of items. assert len(fruits_set) == 3 # You can use the add() object method to add an item. fruits_set.add("pineapple") assert "pineapple" in fruits_set assert len(fruits_set) == 4 # Use remove() method to remove an item. fruits_set.remove("pineapple") assert "pineapple" not in fruits_set assert len(fruits_set) == 3 # Demonstrate set operations on unique letters from two word: first_char_set = set('abracadabra') second_char_set = set('alacazam') assert first_char_set == {'a', 'r', 'b', 'c', 'd'} # unique letters in first word assert second_char_set == {'a', 'l', 'c', 'z', 'm'} # unique letters in second word # Letters in first word but not in second. assert first_char_set - second_char_set == {'r', 'b', 'd'} # Letters in first word or second word or both. assert first_char_set | second_char_set == {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'} # Common letters in both words. assert first_char_set & second_char_set == {'a', 'c'} # Letters in first or second word but not both. assert first_char_set ^ second_char_set == {'r', 'd', 'b', 'm', 'z', 'l'} # Similarly to list comprehensions, set comprehensions are also supported: word = {char for char in 'abracadabra' if char not in 'abc'} assert word == {'r', 'd'} |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |