THE TUPLE
Let’s start talking about the topic “Tuple dictionaries and sets in Python”. We have four types of objects, tuples and lists which are simple assorted items, dictionaries which are key-value pairs, and sets which resemble the classic sets we know from mathematics.
Let’s see them in detail.
type = tuple
Tuples are a sequence like lists, but unlike lists, a tuple is immutable. This means that once the values that may be heterogeneous have been entered and initialized, they are no longer editable. All tuples are instances of a predefined type which is called a tuple. From a performance point of view, a tuple is much more efficient because Python can optimize its content as the tuple is not subject to change.
def Tuple(): colori=()#LE PRIME DUE ISTRUZIONI SIGNIFICANO LA STESSA COSA colori=tuple() colori='rosso','verde','blu' colori=('rosso','verde','blu') #TUPLE UNPACKING (SPACCHETTAMENTO DI UNA TUPLA) a,b,c = colori print(a) print(b) print(c) t1=(1,2) t2=(3,4) mylist=[t1,t2] print(mylist) mylist.append((5,6)) print(mylist) Tuple()
THE TUPLE DICTIONARIES AND SETS IN PYTHON: THE DICTIONARIES
Tuples dictionaries and sets in Python. Let’s start talking about dictionaries. In dictionaries, the order is not defined as in lists and tuples because dictionaries are not sequences.
key, value
Instead of a numeric index, dictionaries use unique keys associated with their respective values. A single element of a dictionary consists of two elements, a key and a value. The keys can be of any type as long as they are immutable, they cannot change once assigned. There are no restrictions for the values. The elements of a dictionary (the key, value pairs) are mutable so you can modify, delete, add elements, in this sense a dictionary is similar to a list.
type = dict
def Dizionari(): mydict = {} mydict = dict()#LE DUE DICHIARAZIONI SONO EQUIVALENTI mydict={"primo":20,"secondo":30,"terzo":40} #CREAZIONE E INIZIALIZZAZIONE mydict["quarto"]=50 #LA CHIAVE NON ESISTE. VIENE AGGIUNTO #L'ELEMENTO AL DIZIONARIO mydict["quarto"]=60 #MODIFICA DI UN VALORE del mydict["secondo"] #ELIMINAZIONE DI UN VALORE print(mydict) print("terzo" in mydict)#RITORNA TRUE mydict.clear()#RIMUOVE TUTTE LE CHIAVI E TUTTI I VALORI mydict={} #RIMUOVE TUTTE LE CHIAVI E TUTTI I VALORI Dizionari()
THE COPY FUNCTION
SET
We can consider a set as a sort of dictionary from which we are going to eliminate all the values to remain only the keys. This is because a set is made up of a series of elements which, however, must all be unique. We create it to check if certain elements belong to a certain set or not, we can use all the typical operations that are usually used in mathematics such as union intersection etc.
type = set
set is mutable, so we can modify the elements of a set with all the operations seen previously.
type = frozenset
frozenset allows us to create non-mutable together.
THE OPERATOR IN
INTERSECTION
UNION
DIFFERENCE
XOR
def Set(): myset = set()#CREIAMO UN INSIEME VUOTO myset = set([10,20,30,40])#CREIAMO UN INSIEME INIZIALIZZATO myset={10,20,30,40}#CREIAMO UN INSIEME INIZIALIZZATO #myset{} ERRORE STIAMO CREANDO UN DIZIONARIO VUOTO myset=set() myset.add(10) myset.add(20) myset.add(30) myset.add(40) myset=frozenset([10,20,30]) #myset.add(40) ERRORE print(30 in myset) #print(myset) myset=set([10,20,30,40]) myset2=set([30,40,50]) print(myset & myset2)#OPERAZIONE DI INTERSEZIONE print(myset | myset2)#OPERAZIONE DI UNIONE print(myset - myset2)#OPERAZIONE DIFFERENZA print(myset ^ myset2)#XOR Set()
IN-DEPTH STUDY LISTS, TUPLES AND DICTIONARIES
In Python, lists, tuples and dictionaries are three of the most commonly used data types for managing collections of elements. Here is an overview of each:
1. Lists
– Description: A list is an ordered, mutable collection of elements, which can be of any type. Elements in a list are enclosed in square brackets [] and separated by commas.
– Characteristics:
– Lists are ordered: elements have a defined order.
– Lists are mutable: you can edit, add or remove elements after the list has been created.
– Elements in a list can be of different types (e.g., integers, strings, other lists, etc.).
– Example:
list = [1, 2, 3, “hello,” [5, 6]]
print(list) # Output: [1, 2, 3, “hello”, [5, 6]]
2. Tuple
– Description: A tuple is an ordered, immutable collection of elements, which can be of any type. Elements in a tuple are enclosed in brackets () and separated by commas.
– Characteristics:
– Tuples are ordered: elements have a defined order.
– Tuples are immutable: once created, you cannot modify, add or remove elements.
– The elements of a tuple can be of different types, as in lists.
– Example:
tuple = (1, 2, 3, “hello,” (5, 6))
print(tuple) # Output: (1, 2, 3, “hello”, (5, 6))
3. Dictionaries
– Description: A dictionary is an unordered collection of key-value pairs. Keys must be unique and immutable (e.g., strings, numbers, tuples), while values can be of any type. Elements in a dictionary are enclosed in curly brackets {} and separated by commas, with each key-value pair separated by colon :.
– Characteristics:
– Dictionaries are unordered: key-value pairs have no defined order until Python 3.7 (in which the order of entry is preserved).
– Dictionaries are mutable: you can modify, add or remove key-value pairs after creation.
– Keys must be unique and immutable, but values can be mutable and duplicate.
– Example:
dictionary = {“name“: “Alice“, “age“: 25, “languages“: [“Python“, “JavaScript“]}
print(dictionary) # Output: {“name”: “Alice”, “age”: 25, “languages”: [“Python”, “JavaScript”]}
Common Operations
– Lists:
– Add an element: list.append(4)
– Remove an element: list.remove(“hello”)
– Accessing by index: list[0] (returns the first element)
– Slicing: list[1:3] (returns a sublist)
– Tuple:
– Accessed by index: tuple[0] (returns first element)
– Slicing: tuple[1:3] (returns a subtuple)
-Dictionaries:
– Add or modify a key-value pair: dictionary[“age”] = 26
– Remove a key-value pair: of dictionary[“age”]
– Access by key: dictionary[“name”] (returns “Alice”)
These data structures are fundamental in Python and are used for various purposes, from simple data handling to more complex structures.
Leave A Comment