Category Python

Python – 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 + tuple2 print(tuple3)

Python – 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)

Python – 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”) But, in Python, we are also allowed to extract the values…

Python – 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…

Python – 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]) Note: The first item has index 0.

Python 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…

Python – 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,…

Python – 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. Use the copy() method You…