Category Archives: Library

13.6 Set Methods

Python has a set of built-in methods that you can use on sets. Example Add an element to the fruits set:fruits = {“apple”, “banana”, “cherry”}fruits.add(“orange”)print(fruits) Output:{‘orange’, ‘banana’, ‘cherry’, ‘apple’} Method Syntax Description add() set.add(elmnt) Adds an element to the set clear() set.clear() Removes all the elements from the set copy() set.copy() Returns a copy of the set difference() set.difference(set) Returns… Read More »

13.5 Join Sets

Join Two Sets There are several ways to join two or more sets in Python. You can use the union() method that returns a new set containing all items from both sets, or the update() method that inserts all the items from one set into another: Example The union() method returns a new set with all items from both sets:set1 =… Read More »

13.4 Loop Sets

Loop Items You can loop through the set items by using a for loop: Example Loop through the set, and print the values:thisset = {“apple”, “banana”, “cherry”}for x in thisset: print(x) Output:bananacherryapple

13.3 Remove Set Items

Remove Item To remove an item in a set, use the remove(), or the discard() method. Example Remove “banana” by using the remove() method:thisset = {“apple”, “banana”, “cherry”}thisset.remove(“banana”)print(thisset) Output:{‘cherry’, ‘apple’} Note: If the item to remove does not exist, remove() will raise an error. Example Remove “banana” by using the discard() method:thisset = {“apple”, “banana”, “cherry”}thisset.discard(“banana”)print(thisset) Output:{‘cherry’, ‘apple’} Note: If the item to remove does not exist, discard() will NOT raise an error. You… Read More »

12.2 Add Set Items

Add Items Once a set is created, you cannot change its items, but you can add new items. To add one item to a set use the add() method. Example Add an item to a set, using the add() method:thisset = {“apple”, “banana”, “cherry”}thisset.add(“orange”)print(thisset) Output:{‘orange’, ‘apple’, ‘cherry’, ‘banana’} Add Sets To add items from another set into the current set, use… Read More »

12.1 Access Set Items

Access Items You cannot access items in a set by referring to an index or a key. But you can loop through the set items using a for loop, or ask if a specified value is present in a set, by using the keyword. Example Loop through the set, and print the values:thisset = {“apple”, “banana”, “cherry”}for x in thisset:print(x) Output:applecherrybanana Example… Read More »

13. 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 List, Tuple, and Dictionary, all with different qualities and usage. A set is a collection which is both unordered and unindexed. Sets are written with curly… Read More »

12.6 Tuple Methods

Example Return the number of times the value 5 appears in the tuple:thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)x = thistuple.count(5)print(x) Output:2 Definition and Usage The count() method returns the number of times a specified value appears in the tuple. Syntax tuple.count(value) Parameter Values Parameter Description value Required. The item to search for Example Search for the first occurrence of the… Read More »

12.5 Join Tuples

Join Two Tuples To join two or more tuples you can use the + operator: Example Join two tuples:tuple1 = (“a”, “b” , “c”)tuple2 = (1, 2, 3)tuple3 = tuple1 + tuple2print(tuple3) Output:(‘a’, ‘b’, ‘c’, 1, 2, 3) Multiply Tuples If you want to multiply the content of a tuple a given number of times, you can use the * operator: Example Multiply the… Read More »

12.4 Loop Tuples

Loop Through a Tuple You can loop through the tuple items by using a for loop. Example Iterate through the items and print the values:thistuple = (“apple”, “banana”, “cherry”)for x in thistuple:  print(x) Output:applebananacherry Learn more about for loops in our Python For Loops Chapter. Loop Through the Index Numbers You can also loop through the tuple items by referring to their index number. Use the range() and len() functions to… Read More »