From d98f46ce647846b0aa30b2e16a30fd4e152a1bf5 Mon Sep 17 00:00:00 2001 From: Carlos Maiolino Date: Thu, 10 Jul 2025 22:55:07 +0200 Subject: Add new code Signed-off-by: Carlos Maiolino --- mit/data_structs.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 mit/data_structs.py (limited to 'mit/data_structs.py') diff --git a/mit/data_structs.py b/mit/data_structs.py new file mode 100644 index 0000000..378e2c4 --- /dev/null +++ b/mit/data_structs.py @@ -0,0 +1,53 @@ +#!/usr/bin/python3 + +# Tuples - Immutable - between () + +my_str = "XUXUBELEZA" +my_tuple = (2, "one", "three", 3.5) +tuple2 = (9, 10) +tuple3 = ("one",) # notice the extra comma +print(my_tuple + tuple2) + +# Lists - looks like a lot like a tuple... between [] +# Usually homogeneous (but we can have different types) +# - Big difference is that lists are mutable + +a_list = [] +b_list = [2, 'a', 3, 'b', True] +L = [2, 1, 3] +L2 = ['a', 'b', 'c'] +L3 = L + L2 + +######################################### + +i = 0 +new_tup = () +while i < len(my_tuple): + print(my_tuple[i]) + new_tup += (my_tuple[i],) + i+=1 + +print(my_tuple[0:-1]) + +print("#####################3") + +#print(L) +L.append(10) +#print(L) +#L.extend(L2) +print(L3) +#print(L) + +del(L3[3]) # Delete a specific inded +print(L3) +L3.pop() # Delete the last item of the list +print(L3) +L3.remove('b') # Delete the 1st appearance of that item +print(L3) +my_list = list(my_str) +print(my_list) +print(my_str.split('B')) # Split a string on a specific character +print("_".join(my_list)) # Join string elements in a list to a string +print(sorted(L)) # Sort but not mutate the string +L.sort() # Sort the list in place +print(L) -- cgit v1.2.3