• ¿Documento equivocado? Cámbialo gratis
  • Escrito por estudiantes que aprobaron
  • Inmediatamente disponible después del pago
  • Leer en línea o como PDF
Vender
¿Dónde estudias?
Tu idioma
Document preview thumbnail
Vista previa 2 fuera de 5 páginas
Otro

Data Structures in Python: A Comprehensive Guide with Examples

Document preview thumbnail
Vista previa 2 fuera de 5 páginas

This document explores data structures in Python, including lists, tuples, sets, dictionaries, and advanced structures like queues and stacks. Learn how to use Python’s built-in data structures with practical examples. Perfect for first-year Computer Science students.

Vista previa del contenido

Data Structures in Python

1. Lists
 A list is an ordered, mutable collection of items. Lists can contain elements
of different data types.

Creating Lists:

# Creating a list
my_list = [1, 2, 3, 4, 5]
mixed_list = [1, "Hello", 3.14, True]

Accessing Elements:

 Use indexing to access elements (starting from 0).

print(my_list[0]) # Output: 1

Common List Operations:

# Adding elements
my_list.append(6) # Adds 6 to the end
my_list.insert(2, "Hi") # Inserts "Hi" at index 2

# Removing elements
my_list.remove(3) # Removes the first occurrence of 3
my_list.pop() # Removes the last element

# Slicing
sub_list = my_list[1:4] # Gets elements from index 1 to 3

# Sorting
my_list.sort() # Sorts the list in ascending order
my_list.reverse() # Reverses the order of elements

, 2. Tuples
 A tuple is an ordered, immutable collection of items. Once created, the
elements cannot be changed.

Creating Tuples:

my_tuple = (1, 2, 3)
single_element_tuple = (5,) # Comma is required for single-element tuples

Accessing Elements:

print(my_tuple[1]) # Output: 2

Use Cases:

 Tuples are used when you want to ensure data cannot be changed, such as
storing coordinates.



3. Dictionaries
 A dictionary is an unordered collection of key-value pairs. Keys must be
unique and immutable (e.g., strings, numbers, tuples).

Creating Dictionaries:

my_dict = {
"name": "Alice",
"age": 25,
"city": "New York"
}

Accessing Elements:

print(my_dict["name"]) # Output: Alice

Información del documento

Subido en
24 de enero de 2025
Número de páginas
5
Escrito en
2024/2025
Tipo
Otro
Personaje
Desconocido
$7.39

¿Documento equivocado? Cámbialo gratis Dentro de los 14 días posteriores a la compra y antes de descargarlo, puedes elegir otro documento. Puedes gastar el importe de nuevo.
Escrito por estudiantes que aprobaron
Inmediatamente disponible después del pago
Leer en línea o como PDF

Vendido
0
Seguidores
0
Artículos
252
Última venta
-



Por qué los estudiantes eligen Stuvia

Creado por compañeros estudiantes, verificado por reseñas

Calidad en la que puedes confiar: escrito por estudiantes que aprobaron y evaluado por otros que han usado estos resúmenes.

¿No estás satisfecho? Elige otro documento

¡No te preocupes! Puedes elegir directamente otro documento que se ajuste mejor a lo que buscas.

Paga como quieras, empieza a estudiar al instante

Sin suscripción, sin compromisos. Paga como estés acostumbrado con tarjeta de crédito y descarga tu documento PDF inmediatamente.

Student with book image

“Comprado, descargado y aprobado. Así de fácil puede ser.”

Alisha Student

Preguntas frecuentes