Resumen de Otros tipos de Datos (informativo)

Sitio: Facultad de Ingeniería U.Na.M.
Curso: Informática ET241/EM241/IC241/IN241/IM204
Libro: Resumen de Otros tipos de Datos (informativo)
Imprimido por: Invitado
Día: miércoles, 18 de diciembre de 2024, 05:05

Descripción


Tabla de contenidos

1. Datos

ListTupleSetDictionary
List is a non-homogeneous data structure which stores the elements in single row and multiple rows and columnsTuple is also a non-homogeneous data structure which stores single row and multiple rows and columnsSet data structure is also non-homogeneous data structure but stores in single rowDictionary is also a non-homogeneous data structure which stores key value pairs
List can be represented by [ ]

Tuple can be represented by  

( )

Set can be represented by { }Dictionary  can be represented by { }
List allows duplicate elementsTuple allows duplicate elementsSet will not allow duplicate elementsSet will not allow duplicate elements and dictionary doesn’t allow duplicate keys.
List can use nested among allTuple can use nested among allSet can use nested among allDictionary can use nested among all
Example: [1, 2, 3, 4, 5]Example: (1, 2, 3, 4, 5)Example: {1, 2, 3, 4, 5}Example: {1, 2, 3, 4, 5}
List can be created using list() functionTuple can be created using tuple() function.Set can be created using set() functionDictionary can be created using dict() function.
List is mutable i.e we can make any changes in list.Tuple  is immutable i.e we can not make any changes in tupleSet is mutable i.e we can make any changes in set. But elements are not duplicated.Dictionary is mutable. But Keys are not duplicated.
List is orderedTuple is orderedSet is unorderedDictionary is ordered

Creating an empty list

l=[]

Creating an empty Tuple

t=()



Creating a set

a=set()

b=set(a)

Creating an empty dictionary

d={}

2. Ejemplos

# Python3 program for explaining
# use of list, tuple, set and
# dictionary

# Lists
l = []
# Adding Element into list
l.append(5)
l.append(10)
print("Adding 5 and 10 in list", l)
# Popping Elements from list
l.pop()
print("Popped one element from list", l)
print()

# Set
s = set()
# Adding element into set
s.add(5)
s.add(10)
print("Adding 5 and 10 in set", s)
# Removing element from set
s.remove(5)
print("Removing 5 from set", s)
print()

# Tuple
t = tuple(l)
# Tuples are immutable
print("Tuple", t)
print()

# Dictionary
d = {}
# Adding the key value pair
d[5] = "Five"
d[10] = "Ten"
print("Dictionary", d)
# Removing key-value pair
del d[10]
print("Dictionary", d)