Module # 3 Assignment
This week’s assignment allowed me to deepen my understanding of Python’s functions and core data structures in a hands-on and rewarding way. I enjoyed writing custom functions with different types of arguments, practicing return values, and exploring how to call functions using loops and keyword inputs. What really stood out to me was the flexibility Python offers when working with lists, especially through slicing and combining data. I also learned that tuples cannot be modified like lists, which helped me better grasp the concept of immutability in programming. One of my biggest “aha” moments came from realizing why subtracting lists directly causes a TypeError. This detail taught me to think critically about how different data types are meant to behave. Overall, I appreciated the creative freedom in the assignment, and it made the coding experience feel less like homework and more like building a small, functional story in Python.
Here are the results from the assignment for this week:
# Module 3, Sardys Avile-Martinez
# Part1 working with functions
# Write a function that takes in a person's name, and prints out a greeting(3-line greeting).
def greeting_function(name):
print(f"Hi {name}, I hope you're having a great weekend!")
print(f"{name}, It is sunny out here, please don't forget to smile")
print(f"See you Monday, {name}!")
print() # This adds a blank line
### Bonus: List + Loop
people = ["Sardys", "Alon", "Rebecca"]
for person in people:
greeting_function(person)
Output
Hi Sardys, I hope you're having a great weekend!
Sardys, It is sunny out here, please don't forget to smile
See you Monday, Sardys!
Hi Alon, I hope you're having a great weekend!
Alon, It is sunny out here, please don't forget to smile
See you Monday, Alon!
Hi Rebecca, I hope you're having a great weekend!
Rebecca, It is sunny out here, please don't forget to smile
See you Monday, Rebecca!
# Full Names Function
def print_full_names(first_name, last_name):
full_names = f"{first_name} {last_name}"
print(f"Hola, {full_names}!")
print() # Adds space after each greeting
print_full_names("Sardys", "Martinez")
print_full_names("Alon", "Friedman")
print_full_names("Rebecca", "Marquez")
Output
Hola, Sardys Martinez!
Hola, Alon Friedman!
Hola, Rebecca Marquez!
# Addition Calculator
def adding_and_printing(num1, num2):
result = num1 + num2
print(f"Adding {num1} + {num2} = {result}")
print() # Adds space after each calculation
adding_and_printing(4, 5)
adding_and_printing(10, 7)
adding_and_printing(25, 30)
Output
Adding 4 + 5 = 9
Adding 10 + 7 = 17
Adding 25 + 30 = 55
# Return Calculator
def adding_and_returning(num1, num2):
return num1 + num2
result1 = adding_and_returning(3, 3)
print(f"The result is {result1}")
result2 = adding_and_returning(12, 8)
print(f"The result is {result2}")
result3 = adding_and_returning(100, 50)
print(f"The result is {result3}")
Output
The result is 6
The result is 20
The result is 150
# Part 2 working with pow()
# Consider the following expression, intended to print the square root of 16: pow(16,(1/2))
# in Python it does return 4.0, but only if I am using floating-point division (which I am here with 1/2).
print (pow(16,(1/2))) # Output: 4.0
# To safely get the square root of 16 using pow() in Python 3, let's use floating-point division like this
print(pow(16, 0.5)) # Output: 4.0 as the previous output
Output
4.0
4.0
# Part 3: Working with lists and tuples
print() # This adds a blank line
x = [1, 2, 3, 4, 5]
y = [11, 12, 13, 14, 15]
z = (21, 22, 23, 24, 25)
#(a) What is the value of 3*x?
print("a) 3 * x =", 3 * x)
# note: — list repeated 3 times, I can achieve the same by using a loop :)
Output
a) 3 * x = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
#(b) What is the value of x+y?
print("\nb) x + y =", x + y)
#The result will be lists concatenated
Output
b) x + y = [1, 2, 3, 4, 5, 11, 12, 13, 14, 15]
#(c) What is the value of x-y?
print("\nc) x - y = TypeError ❌ I cannot subtract lists in Python, TypeError: unsupported operand type(s) for -: 'list' and 'list' ")
#print("\nb) x - y =", x - y)
Output
c) x - y = TypeError ❌ I cannot subtract lists in Python, TypeError: unsupported operand type(s) for -: 'list' and 'list'
#(d) What is the value of x[1]?
print("\nd) x[1] =", x[1])
# 2, because indexing starts at 0
Output
d) x[1] = 2
#(e) What is the value of x[0]?
print("\ne) x[0] =", x[0])
# 1 , it returns the first item in the list
Output
e) x[0] = 1
#(f) What is the value of x[-1]?
print("\nf) x[-1] =", x[-1])
# 5 , it returns the last item in the list
Output
f) x[-1] = 5
# (g) What is the value of x[:]?
print("\ng) x[:] =", x[:])
# [1, 2, 3, 4, 5], returns a full copy of the list. super useful :)
Output
g) x[:] = [1, 2, 3, 4, 5]
# (h) What is the value of x[2:4]?
print("\nh) x[2:4] =", x[2:4])
# [3, 4] ,it returns items from index 2 up to (but not including) index 4.
Output
h) x[2:4] = [3, 4]
#(i) What is the value of x[1:4:2]?
print("\ni) x[1:4:2] =", x[1:4:2])
# [2, 4] , it returns items from index 1 to 3, by 2
Output
i) x[1:4:2] = [2, 4]
#(j) What is the value of x[:2]?
print("\nj) x[:2] =", x[:2])
# [1, 2] , it returns the first two elements
Output
j) x[:2] = [1, 2]
#(k) What is the value of x[::2]?
print("\nk) x[::2] =", x[::2])
# [1, 3, 5] ,it returns every other element starting from index 0
Output
k) x[::2] = [1, 3, 5]
#(l) What is the result of the following two expressions? x[3]=8 print x
print("\nl) After x[3] = 8, x becomes:")
x[3] = 8
print("x =", x)
# [1, 2, 3, 8, 5], I replaced the value at index 3, and now it is 8
Output
l) After x[3] = 8, x becomes:
x = [1, 2, 3, 8, 5]
# (m) What is the result of the above pair of expressions if the list x were
# replaced with the tuple z?
print("\nm) If we try z[3] = 8, it gives:")
try:
z[3] = 8
except TypeError as e:
print("Error:", e)
# I will get an error because the tuples are immutable. I can't change their elements
Output
m) If we try z[3] = 8, it gives:
Error: 'tuple' object does not support item assignment
### Process finished with exit code 0
Summary (Module 3 takeaways)
- Learned how to define and call functions with parameters, default values, *args, and return statements.
- Gained confidence using loops to pass multiple values through functions.
- Explored how to structure outputs using clean, readable formatting with print().
- Practiced creating and manipulating lists, tuples, and slices.
- Discovered that Python does not support list subtraction using -, and why this raises a TypeError.
- Reinforced the importance of writing clear, modular code for reuse and readability.
A Coding Tip from Sardys
When in doubt, print it out!
Adding print() With blank lines between sections of your code, not only improves readability, but it also helps you debug and reflect on your logic with a fresh perspective. And remember: tuples don’t like change, but lists? They’re ready for anything!
Do you have any other tips 💡 that you would like to share with me?
Please share it, I am here ready 2 learn ☺️