Skip to content

Commit b118d30

Browse files
authored
Merge pull request mouredev#7448 from Hyromy/main
#12 - Python
2 parents 2addfb1 + d40823a commit b118d30

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import os
2+
import xml.etree.ElementTree as ET
3+
import json
4+
5+
# ---- xml ----
6+
root = ET.Element("root") # Crear el nodo raíz
7+
8+
nombre = ET.SubElement(root, "nombre") # Crear un nodo hijo
9+
nombre.text = "Hyromy" # Asignar un valor al nodo hijo
10+
11+
edad = ET.SubElement(root, "edad")
12+
edad.text = "20"
13+
14+
fecha_nacimiento = ET.SubElement(root, "fecha_nacimiento")
15+
fecha_nacimiento.text = "2004-12-04"
16+
17+
lenguajes = ET.SubElement(root, "lenguajes")
18+
lenguajes_list = ["Java", "PHP", "C#", "Python", "JavaScript"]
19+
for lenguaje in lenguajes_list:
20+
lenguaje_node = ET.SubElement(lenguajes, "item")
21+
lenguaje_node.text = lenguaje
22+
23+
xml_file_name = __file__[:-3] + ".xml"
24+
tree = ET.ElementTree(root)
25+
tree.write(xml_file_name, encoding = "utf-8", xml_declaration = True)
26+
del tree
27+
del root
28+
29+
tree = ET.parse(xml_file_name)
30+
root = tree.getroot()
31+
32+
def read_xml(root : ET.Element, ident = 0):
33+
for child in root:
34+
if not len(child):
35+
print(f"{'\t' * ident}<{child.tag}>{child.text}</{child.tag}>")
36+
else:
37+
print(f"{'\t' * ident}<{child.tag}>")
38+
read_xml(child, ident + 1)
39+
print(f"{'\t' * ident}</{child.tag}>")
40+
41+
read_xml(root)
42+
os.remove(xml_file_name)
43+
44+
print("\n\n")
45+
46+
# ---- json ----
47+
json_data = {
48+
"nombre": "Hyromy",
49+
"edad": 20,
50+
"fecha_nacimiento": "2004-12-04",
51+
"lenguajes": [
52+
"Java",
53+
"PHP",
54+
"C#",
55+
"Python",
56+
"JavaScript"
57+
],
58+
}
59+
60+
json_file_name = __file__[:-3] + ".json"
61+
62+
with open(json_file_name, "w", encoding = "utf-8") as f:
63+
json.dump(json_data, f, indent = 4)
64+
del json_data
65+
66+
with open(json_file_name, "r", encoding = "utf-8") as f:
67+
json_data = json.load(f)
68+
print(json_data)
69+
70+
os.remove(json_file_name)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
conjunto = [1, 2, 3, 4, 5] # ya se que es una lista y no un conjunto
2+
print(conjunto)
3+
4+
conjunto.append("A")
5+
print(conjunto)
6+
7+
conjunto.insert(0, "b")
8+
print(conjunto)
9+
10+
conjunto.extend([-1, -2, -3])
11+
print(conjunto)
12+
13+
block = ["x", "y", "z"]
14+
for i, item in enumerate(block):
15+
conjunto.insert(i + 3, item)
16+
print(conjunto)
17+
18+
conjunto.pop(5)
19+
print(conjunto)
20+
21+
conjunto[7] = "C"
22+
print(conjunto)
23+
24+
print(-1 in conjunto)
25+
print(5 in conjunto)
26+
27+
conjunto.clear()
28+
29+
# ---- DIFUCULTAD EXTRA ----
30+
conjunto_a = {1, 2, 3, 4, 5}
31+
conjunto_b = {4, 5, 6, 7, 8}
32+
33+
print(conjunto_a.union(conjunto_b)) # unir ambos conjuntos
34+
print(conjunto_a.intersection(conjunto_b)) # elementos comunes
35+
print(conjunto_a.difference(conjunto_b)) # elementos en A que no estan en B
36+
print(conjunto_b.difference(conjunto_a)) # elementos en B que no estan en A
37+
print(conjunto_a.symmetric_difference(conjunto_b)) # insertesecion inversa

0 commit comments

Comments
 (0)