Python Assignment – Intro to Functions and Lists
Here’s what I accomplished:
-
Wrote a function that prints a 3-line greeting using someone’s name on each line.
-
Stored three names in a list and used a loop to greet them all.
-
Created a function that prints a nicely formatted full name from first and last name.
-
Built an addition calculator that prints results, then rewrote it to return the result instead.
-
Learned that
pow(16, 1/2)
works to get the square root of 16 (returns4.0
). -
Played with list and tuple syntax:
-
Found out that
3 * x
repeats a list. -
You can do
x + y
(joins the lists) but can’t dox - y
. -
Lists are mutable, but tuples can’t be changed.
-
I didn’t run into any issues using the online Python compiler, and I’m already seeing how functions and data structures come together in Python. Looking forward to the next challenge.
The Output:
Hello, Alice Smith! Hope you're having a great day.
Alice Smith, just wanted to say you're awesome!
Take care, Alice Smith!
---
Hello, Bob Johnson! Hope you're having a great day.
Bob Johnson, just wanted to say you're awesome!
Take care, Bob Johnson!
---
Hello, Carla Gomez! Hope you're having a great day.
Carla Gomez, just wanted to say you're awesome!
Take care, Carla Gomez!
---
Hello, Alice Smith!
Hello, Bob Johnson!
Hello, Carla Gomez!
5 + 3 = 8
10 + 4 = 14
2 + 9 = 11
Result 1: 8
Result 2: 14
Result 3: 11
Using pow(16, (1/2)): 4.0
Better version: pow(16, 0.5): 4.0
3*x = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
x+y = [1, 2, 3, 4, 5, 11, 12, 13, 14, 15]
x[1] = 2
x[0] = 1
x[-1] = 5
x[:] = [1, 2, 3, 4, 5]
x[2:4] = [3, 4]
x[1:4:2] = [2, 4]
x[:2] = [1, 2]
x[::2] = [1, 3, 5]
After x[3]=8, x = [1, 2, 3, 8, 5]
=== Code Execution Successful ===
My Code:
# Part 1: Greeting Function
def greet(name):
print(f"Hello, {name}! Hope you're having a great day.")
print(f"{name}, just wanted to say you're awesome!")
print(f"Take care, {name}!")
# Greet three people using a loop
people = ["Alice Smith", "Bob Johnson", "Carla Gomez"]
for person in people:
greet(person)
print("---")
# Full Name Function
def full_name(first, last):
print(f"Hello, {first} {last}!")
full_name("Alice", "Smith")
full_name("Bob", "Johnson")
full_name("Carla", "Gomez")
# Addition Calculator
def add_numbers(a, b):
print(f"{a} + {b} = {a + b}")
add_numbers(5, 3)
add_numbers(10, 4)
add_numbers(2, 9)
# Return Calculator
def add_and_return(a, b):
return a + b
result1 = add_and_return(5, 3)
print(f"Result 1: {result1}")
result2 = add_and_return(10, 4)
print(f"Result 2: {result2}")
result3 = add_and_return(2, 9)
print(f"Result 3: {result3}")
# Part 2: pow(16, (1/2))
print("Using pow(16, (1/2)):", pow(16, (1/2)))
print("Better version: pow(16, 0.5):", pow(16, 0.5))
# Part 3: List and Tuple operations
x = [1, 2, 3, 4, 5]
y = [11, 12, 13, 14, 15]
z = (21, 22, 23, 24, 25)
print("3*x =", 3*x)
print("x+y =", x + y)
# print("x-y =", x - y) # not supported
print("x[1] =", x[1])
print("x[0] =", x[0])
print("x[-1] =", x[-1])
print("x[:] =", x[:])
print("x[2:4] =", x[2:4])
print("x[1:4:2] =", x[1:4:2])
print("x[:2] =", x[:2])
print("x[::2] =", x[::2])
x[3] = 8
print("After x[3]=8, x =", x)
# z[3] = 8 # This would give an error: tuples can't be modified
Comments
Post a Comment