Back to the basics
Tuesday, August 26, 2025
Tip
Use the little “Copy” icon in the top right corner of each code block to copy the code.
Python comes with a lot of built in fundamental data types (and libraries have even more!) that we need to understand in order to build our programs. Here are some of the most common that you should be familiar with.
# let's define a few strings
a = "hello"
b = " world"
# we can see how many characters are in a string with the len command
print(len(a), len(b))print("Indexing a string: ", a[3])
print("Slicing a string: ", a[0:3])
print("Reversing a string with slicing: ", a[::-1])int data types represent whole numbers.float data types represent decimal values.print(a + b, type(a + b)) # what would you expect? what did you actually get?
print(a - b, type(a - b)) # same as above?print(a + x, type(a + x))
print(a + z, type(a + z)) # what if the float is a whole number? 2 + 1 = 3, right?Warning
The distinction between int and float feels simple, but implementations can cause huge side effects.
print(float(2)) # that feels right, but what about the other way?
print(int(x)) # this will truncate the decimal
print(int(y)) # this will truncate the decimal
print(int(z)) # this will truncate the decimalNote
int(1.999) and int(1.001) both become 1. Python converts integers to floats by dropping the decimal completely.
list1 = ["e", "a", "d", "c", "b"] # explicitly defining a list of characters with []
list2 = list("eadcb") # casting to list
list3 = [1, 2, 4, 5, 3] # list of numbers
list4 = [1, "b", 3, "d", 5] # mixed listlist1.sort() # this happens in place; your original ordering has been lost
print("list1 after .sort() :", list1)
print("sorted() returns", sorted(list2))
print("Original list2 after calling sorted() on it:", list2)list5 = list1.copy() # now we can add a list to the list
new_list = ["f", "g", "h"]
list5.append(new_list)
print(list5)list6[0] = "1" # changing an element of the list
print(list6)
list6.remove("1") # deleting an element
print(list6)
list6.insert(0, "d") # add an element to a list in a specific spot
print(list6)
# like with strings we can also index and slice lists
print("Here is the 3rd element: ", list6[2])
print("Here are the first 3 elements: ", list6[0:3])simple_dict = {"a": "apple", "b": "banana", "c": "cantaloupe"}
print(simple_dict)
print(simple_dict["a"]) # we access values in the dictionary by using the key
keys = simple_dict.keys() # we can get a list of all of the keys
print(keys)
values = simple_dict.values() # also all of the values
k_v = simple_dict.items() # or a combination
print(values)
print(k_v)
# Can you figure out how to sort this dictionary into a different order?other_dict = {
"bob": {
"age": 25,
"profession": "plumber",
"hobbies": ["video games", "snowboarding", "knitting"],
},
"cassidy": {
"age": 42,
"profession": "programmer",
"hobbies": ["dancing", "cooking"],
},
"obi-wan": {
"age": 19,
"profession": "jedi",
"hobbies": ["hyper drive repair", "laser swords", "sith hunting"],
},
}sorted_tuple = sorted(tuple2) # this will turn the tuple into a list
print(sorted_tuple)
# if we want it to be a tuple again we will have to cast it back to a tuple
sorted_tuple = tuple(sorted_tuple)
print(sorted_tuple)