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 »

12.3 Unpack Tuples

Unpacking a Tuple When we create a tuple, we normally assign values to it. This is called “packing” a tuple: Example Packing a tuple:fruits = (“apple”, “banana”, “cherry”) Output:(‘apple’, ‘banana’, ‘cherry’) But, in Python, we are also allowed to extract the values back into variables. This is called “unpacking”: Example Unpacking a tuple:fruits = (“apple”, “banana”, “cherry”)(green, yellow, red)… Read More »

12.2 Update Tuples

Tuples are unchangeable, meaning that you cannot change, add, or remove items once the tuple is created. But there are some workarounds. Change Tuple Values Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called. But there is a workaround. You can convert the tuple into a list,… Read More »

12.1 Access Tuple Items

Access Tuple Items You can access tuple items by referring to the index number, inside square brackets: Example Print the second item in the tuple:thistuple = (“apple”, “banana”, “cherry”)print(thistuple[1]) Output:banana Note: The first item has index 0. Negative Indexing Negative indexing means start from the end. -1 refers to the last item, -2 refers to the second last item etc. Example… Read More »

12. Tuples

mytuple = (“apple”, “banana”, “cherry”) Tuple Tuples are used to store multiple items in a single variable. Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage. A tuple is a collection which is ordered and unchangeable. Tuples are written with… Read More »

11.10 List Methods

List Methods Example: Add an element to the fruits list:fruits = [‘apple’, ‘banana’, ‘cherry’]fruits.append(“orange”) Output:[‘apple’, ‘banana’, ‘cherry’, ‘orange’] Python has a set of built-in methods that you can use on lists. Method Syntax Description append() list.append(elmnt) Adds an element at the end of the list clear() list.clear() Removes all the elements from the list copy() list.copy() Returns a copy… Read More »

11.9 Join Lists

Join Two Lists There are several ways to join, or concatenate, two or more lists in Python. One of the easiest ways are by using the + operator. Example Join two list:list1 = [“a”, “b”, “c”]list2 = [1, 2, 3]list3 = list1 + list2print(list3) Output:[‘a’, ‘b’, ‘c’, 1, 2, 3] Another way to join two lists is by appending all the… Read More »