Category Archives: Library

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 »

11.8 Copy Lists

Copy a List You cannot copy a list simply by typing list2 = list1, because: list2 will only be a reference to list1, and changes made in list1 will automatically also be made in list2. There are ways to make a copy, one way is to use the built-in List method copy(). Example Make a copy of a list with the copy() method:thislist = [“apple”, “banana”, “cherry”]mylist = thislist.copy()print(mylist) Output:[‘apple’,… Read More »

11.7 Sort Lists

Sort List Alphanumerically List objects have a sort() method that will sort the list alphanumerically, ascending, by default: Example Sort the list alphabetically:thislist = [“orange”, “mango”, “kiwi”, “pineapple”, “banana”]thislist.sort()print(thislist) Output:[‘banana’, ‘kiwi’, ‘mango’, ‘orange’, ‘pineapple’] Example Sort the list numerically:thislist = [100, 50, 65, 82, 23]thislist.sort()print(thislist) Output:[23, 50, 65, 82, 100] Sort Descending To sort descending, use the keyword argument reverse = True: Example Sort the list… Read More »

11.6 List Comprehension

List Comprehension List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list. Example: Based on a list of fruits, you want a new list, containing only the fruits with the letter “a” in the name. Without list comprehension you will have to write… Read More »

11.5 Loop Lists

Loop Through a List You can loop through the list items by using a for loop: Example Print all items in the list, one by one:thislist = [“apple”, “banana”, “cherry”]for x in thislist:  print(x) Output:applebananacherry Loop Through the Index Numbers You can also loop through the list items by referring to their index number. Use the range() and len() functions to create a suitable iterable. Example Print… Read More »